View.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', 'underscore', 'doT', '../../lib/@waca/core-client/js/core-client/ui/core/Events', '../../lib/@waca/core-client/js/core-client/utils/EventHelper'], function ($, _, dot, Events) {
  8. var View = null;
  9. var eventsRegex = /^(\S+)\s*(.*)$/;
  10. // View related attributes that are supported
  11. var viewAttributes = ['el', 'id', 'className', 'tagName', 'events'];
  12. /**
  13. * A Backbone style base View Class. UI Views should extend this class.
  14. *
  15. */
  16. View = Events.extend({
  17. templateString: null,
  18. id: null,
  19. init: function init(attributes) {
  20. View.inherited('init', this, arguments);
  21. this.viewId = _.uniqueId('view');
  22. this.dotTemplate = dot.template(this.templateString || '');
  23. _.extend(this, _.pick(attributes || {}, viewAttributes));
  24. this._initDomElement();
  25. this._attachEvents();
  26. },
  27. /**
  28. * The tag name given to the view's DOM element. By default it's a DIV.
  29. */
  30. tagName: 'div',
  31. /**
  32. * Initialize the DOM element for this view
  33. */
  34. _initDomElement: function _initDomElement() {
  35. if (!this.el) {
  36. var attrs = {};
  37. if (this.id) {
  38. attrs.id = _.result(this, 'id');
  39. }
  40. if (this.className) {
  41. attrs['class'] = _.result(this, 'className');
  42. }
  43. this.setElement(document.createElement(this.tagName));
  44. this.$el.attr(attrs);
  45. } else {
  46. this.setElement(_.result(this, 'el'));
  47. }
  48. },
  49. /**
  50. * Convenience function to use jQuery to find a DOM element within this view. This is
  51. * faster then doing a global lockup.
  52. */
  53. $: function $(selector) {
  54. return this.$el.find(selector);
  55. },
  56. /**
  57. * Hide the view
  58. */
  59. hide: function hide() {
  60. this.$el.hide();
  61. },
  62. /**
  63. * Show the view if hidden
  64. */
  65. show: function show() {
  66. this.$el.show();
  67. },
  68. /**
  69. * Make sure the target is the intended dom node, using CSS class to validate.
  70. * If not the right node, check out the parents.
  71. */
  72. getTarget: function getTarget(target, sClass) {
  73. var $t = $(target);
  74. if (!$t.hasClass(sClass)) {
  75. var parents = $t.parents('.' + sClass);
  76. if (parents.length > 0) {
  77. target = parents[0];
  78. }
  79. }
  80. return target;
  81. },
  82. /**
  83. * Render is the main function of the View. Views should implement (override) the render
  84. * method to populate this.el with the appropriate HTML. Render should always return this
  85. * to allow chaining of calls.
  86. */
  87. render: function render() {
  88. return this;
  89. },
  90. /**
  91. * Remove this view:
  92. * -Remove the element from the DOM
  93. * -Remove the event listeners
  94. */
  95. remove: function remove() {
  96. if (this.$el) {
  97. this.$el.remove();
  98. }
  99. if (this.off) {
  100. this.off();
  101. }
  102. return this;
  103. },
  104. /**
  105. * Set the view element to a new DOM element
  106. */
  107. setElement: function setElement(el) {
  108. this._detachEvents();
  109. this.$el = el instanceof $ ? el : $(el);
  110. this.el = this.$el[0];
  111. this._attachEvents();
  112. return this;
  113. },
  114. /**
  115. * Attaches the events in this.events to this.el for this view
  116. */
  117. _attachEvents: function _attachEvents() {
  118. var events = this.events;
  119. this._detachEvents();
  120. for (var key in events) {
  121. if (events.hasOwnProperty(key)) {
  122. var callback = events[key];
  123. if (!_.isFunction(callback)) {
  124. callback = this[events[key]];
  125. }
  126. if (!callback) {
  127. continue;
  128. }
  129. var match = key.match(eventsRegex);
  130. var eventName = match[1];
  131. var selector = match[2];
  132. // add the event to the element, with a namespace 'privateViewEvents'
  133. this.$el.on(eventName + '.privateViewEvents' + this.viewId, selector, callback.bind(this));
  134. }
  135. }
  136. return this;
  137. },
  138. /**
  139. * Detaches all the events from the element
  140. */
  141. _detachEvents: function _detachEvents() {
  142. if (this.$el) {
  143. this.$el.off('.privateViewEvents' + this.viewId);
  144. }
  145. return this;
  146. }
  147. });
  148. return View;
  149. });
  150. //# sourceMappingURL=View.js.map