WidgetView.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. /**
  3.  * Licensed Materials - Property of IBM
  4.  * IBM Cognos Products: admin
  5.  * Copyright IBM Corp. 2015, 2016
  6.  * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7.  */
  8. define(['jquery', 'underscore', 'bi/commons/ui/View'], function ($, _, View) {
  9. 'use strict'; //NOSONAR: meant to be strict
  10. var WidgetView = View.extend({
  11. widgetName: 'widget',
  12. widgetEventPrefix: "",
  13. _getCreateOptions: $.noop,
  14. options: {
  15. disabled: false
  16. },
  17. init: function init(attributes) {
  18. WidgetView.inherited('init', this, arguments);
  19. this.element = this.$el;
  20. this.eventNamespace = "." + this.widgetName + "-" + this.viewId;
  21. attributes = attributes || {};
  22. var options = _.extend({}, this.options, this._getCreateOptions(), attributes.options);
  23. _.extend(this, attributes);
  24. this.options = options;
  25. this.bindings = $();
  26. this.hoverable = $();
  27. this.focusable = $();
  28. var fullName = this.widgetName; // create selector for plugin
  29. $.expr[":"][fullName.toLowerCase()] = function (elem) {
  30. return !!$.data(elem, fullName);
  31. };
  32. this.element.data("widgetName", this.widgetName);
  33. this.element.data(this.widgetName, this);
  34. this.document = $(this.element.style ? // element within the document
  35. this.element.ownerDocument : // element is window or document
  36. this.element.document || this.element);
  37. this.window = $(this.document[0].defaultView || this.document[0].parentWindow);
  38. },
  39. cleanUp: function cleanUp() {
  40. this.$el.unbind(this.eventNamespace).removeData("widgetName").removeData(this.widgetName); // clean up events and states
  41. this.bindings.unbind(this.eventNamespace);
  42. this.hoverable.removeClass("ui-state-hover");
  43. this.focusable.removeClass("ui-state-focus");
  44. },
  45. widget: function widget() {
  46. return this.$el;
  47. },
  48. refresh: function refresh() {},
  49. option: function option(key, value) {
  50. var options = key,
  51. parts,
  52. curOption,
  53. i;
  54. if (arguments.length === 0) {
  55. // don't return a reference to the internal hash
  56. return _.extend({}, this.options);
  57. }
  58. if (typeof key === "string") {
  59. // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
  60. options = {};
  61. parts = key.split(".");
  62. key = parts.shift();
  63. if (parts.length) {
  64. curOption = options[key] = _.extend({}, this.options[key]);
  65. for (i = 0; i < parts.length - 1; i++) {
  66. curOption[parts[i]] = curOption[parts[i]] || {};
  67. curOption = curOption[parts[i]];
  68. }
  69. key = parts.pop();
  70. if (value === undefined) {
  71. return curOption[key] === undefined ? null : curOption[key];
  72. }
  73. curOption[key] = value;
  74. } else {
  75. if (value === undefined) {
  76. return this.options[key] === undefined ? null : this.options[key];
  77. }
  78. options[key] = value;
  79. }
  80. }
  81. this._setOptions(options);
  82. return this;
  83. },
  84. _setOptions: function _setOptions(options) {
  85. var key;
  86. for (key in options) {
  87. this._setOption(key, options[key]);
  88. }
  89. return this;
  90. },
  91. _setOption: function _setOption(key, value) {
  92. this.options[key] = value;
  93. if (key === "disabled") {
  94. this.$el.toggleClass(this.widgetName + "-disabled ui-state-disabled disabled", !!value).attr("aria-disabled", value);
  95. this.hoverable.removeClass("ui-state-hover");
  96. this.focusable.removeClass("ui-state-focus");
  97. }
  98. return this;
  99. },
  100. enable: function enable() {
  101. return this._setOption("disabled", false);
  102. },
  103. disable: function disable() {
  104. return this._setOption("disabled", true);
  105. },
  106. hide: function hide() {
  107. this.element.hide();
  108. },
  109. show: function show() {
  110. this.element.show();
  111. },
  112. _on: function _on(element, handlers) {
  113. var delegateElement,
  114. instance = this; // no element argument, shuffle and use this.$el
  115. if (!handlers) {
  116. handlers = element;
  117. element = delegateElement = this.$el;
  118. } else {
  119. // accept selectors, DOM elements
  120. element = delegateElement = $(element);
  121. this.bindings = this.bindings.add(element);
  122. }
  123. $.each(handlers, function (event, handler) {
  124. function handlerProxy() {
  125. if (instance.options.disabled === true || $(this).hasClass("ui-state-disabled")) {
  126. return;
  127. }
  128. return (typeof handler === "string" ? instance[handler] : handler).apply(instance, arguments);
  129. }
  130. var match = event.match(/^(\w+)\s*(.*)$/),
  131. eventName = match[1] + instance.eventNamespace,
  132. selector = match[2];
  133. if (selector) {
  134. delegateElement.delegate(selector, eventName, handlerProxy);
  135. } else {
  136. element.bind(eventName, handlerProxy);
  137. }
  138. });
  139. },
  140. _off: function _off(element, eventName) {
  141. eventName = (eventName || "").split(" ").join(this.eventNamespace + " ") + this.eventNamespace;
  142. element.unbind(eventName).undelegate(eventName);
  143. },
  144. _hoverable: function _hoverable(element) {
  145. this.hoverable = this.hoverable.add(element);
  146. this._on(element, {
  147. mouseenter: function mouseenter(event) {
  148. $(event.currentTarget).addClass("ui-state-hover");
  149. },
  150. mouseleave: function mouseleave(event) {
  151. $(event.currentTarget).removeClass("ui-state-hover");
  152. }
  153. });
  154. },
  155. _focusable: function _focusable(element) {
  156. this.focusable = this.focusable.add(element);
  157. this._on(element, {
  158. focusin: function focusin(event) {
  159. $(event.currentTarget).addClass("ui-state-focus");
  160. },
  161. focusout: function focusout(event) {
  162. $(event.currentTarget).removeClass("ui-state-focus");
  163. }
  164. });
  165. }
  166. });
  167. WidgetView.keyCode = {
  168. BACKSPACE: 8,
  169. COMMA: 188,
  170. DELETE: 46,
  171. DOWN: 40,
  172. END: 35,
  173. ENTER: 13,
  174. ESCAPE: 27,
  175. HOME: 36,
  176. LEFT: 37,
  177. NUMPAD_ADD: 107,
  178. NUMPAD_DECIMAL: 110,
  179. NUMPAD_DIVIDE: 111,
  180. NUMPAD_ENTER: 108,
  181. NUMPAD_MULTIPLY: 106,
  182. NUMPAD_SUBTRACT: 109,
  183. PAGE_DOWN: 34,
  184. PAGE_UP: 33,
  185. PERIOD: 190,
  186. RIGHT: 39,
  187. SPACE: 32,
  188. TAB: 9,
  189. UP: 38,
  190. PLUS: 107,
  191. MINUS: 109
  192. };
  193. return WidgetView;
  194. });