123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- "use strict";
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: admin
- * Copyright IBM Corp. 2015, 2016
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['jquery', 'underscore', 'bi/commons/ui/View'], function ($, _, View) {
- 'use strict'; //NOSONAR: meant to be strict
- var WidgetView = View.extend({
- widgetName: 'widget',
- widgetEventPrefix: "",
- _getCreateOptions: $.noop,
- options: {
- disabled: false
- },
- init: function init(attributes) {
- WidgetView.inherited('init', this, arguments);
- this.element = this.$el;
- this.eventNamespace = "." + this.widgetName + "-" + this.viewId;
- attributes = attributes || {};
- var options = _.extend({}, this.options, this._getCreateOptions(), attributes.options);
- _.extend(this, attributes);
- this.options = options;
- this.bindings = $();
- this.hoverable = $();
- this.focusable = $();
- var fullName = this.widgetName; // create selector for plugin
- $.expr[":"][fullName.toLowerCase()] = function (elem) {
- return !!$.data(elem, fullName);
- };
- this.element.data("widgetName", this.widgetName);
- this.element.data(this.widgetName, this);
- this.document = $(this.element.style ? // element within the document
- this.element.ownerDocument : // element is window or document
- this.element.document || this.element);
- this.window = $(this.document[0].defaultView || this.document[0].parentWindow);
- },
- cleanUp: function cleanUp() {
- this.$el.unbind(this.eventNamespace).removeData("widgetName").removeData(this.widgetName); // clean up events and states
- this.bindings.unbind(this.eventNamespace);
- this.hoverable.removeClass("ui-state-hover");
- this.focusable.removeClass("ui-state-focus");
- },
- widget: function widget() {
- return this.$el;
- },
- refresh: function refresh() {},
- option: function option(key, value) {
- var options = key,
- parts,
- curOption,
- i;
- if (arguments.length === 0) {
- // don't return a reference to the internal hash
- return _.extend({}, this.options);
- }
- if (typeof key === "string") {
- // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
- options = {};
- parts = key.split(".");
- key = parts.shift();
- if (parts.length) {
- curOption = options[key] = _.extend({}, this.options[key]);
- for (i = 0; i < parts.length - 1; i++) {
- curOption[parts[i]] = curOption[parts[i]] || {};
- curOption = curOption[parts[i]];
- }
- key = parts.pop();
- if (value === undefined) {
- return curOption[key] === undefined ? null : curOption[key];
- }
- curOption[key] = value;
- } else {
- if (value === undefined) {
- return this.options[key] === undefined ? null : this.options[key];
- }
- options[key] = value;
- }
- }
- this._setOptions(options);
- return this;
- },
- _setOptions: function _setOptions(options) {
- var key;
- for (key in options) {
- this._setOption(key, options[key]);
- }
- return this;
- },
- _setOption: function _setOption(key, value) {
- this.options[key] = value;
- if (key === "disabled") {
- this.$el.toggleClass(this.widgetName + "-disabled ui-state-disabled disabled", !!value).attr("aria-disabled", value);
- this.hoverable.removeClass("ui-state-hover");
- this.focusable.removeClass("ui-state-focus");
- }
- return this;
- },
- enable: function enable() {
- return this._setOption("disabled", false);
- },
- disable: function disable() {
- return this._setOption("disabled", true);
- },
- hide: function hide() {
- this.element.hide();
- },
- show: function show() {
- this.element.show();
- },
- _on: function _on(element, handlers) {
- var delegateElement,
- instance = this; // no element argument, shuffle and use this.$el
- if (!handlers) {
- handlers = element;
- element = delegateElement = this.$el;
- } else {
- // accept selectors, DOM elements
- element = delegateElement = $(element);
- this.bindings = this.bindings.add(element);
- }
- $.each(handlers, function (event, handler) {
- function handlerProxy() {
- if (instance.options.disabled === true || $(this).hasClass("ui-state-disabled")) {
- return;
- }
- return (typeof handler === "string" ? instance[handler] : handler).apply(instance, arguments);
- }
- var match = event.match(/^(\w+)\s*(.*)$/),
- eventName = match[1] + instance.eventNamespace,
- selector = match[2];
- if (selector) {
- delegateElement.delegate(selector, eventName, handlerProxy);
- } else {
- element.bind(eventName, handlerProxy);
- }
- });
- },
- _off: function _off(element, eventName) {
- eventName = (eventName || "").split(" ").join(this.eventNamespace + " ") + this.eventNamespace;
- element.unbind(eventName).undelegate(eventName);
- },
- _hoverable: function _hoverable(element) {
- this.hoverable = this.hoverable.add(element);
- this._on(element, {
- mouseenter: function mouseenter(event) {
- $(event.currentTarget).addClass("ui-state-hover");
- },
- mouseleave: function mouseleave(event) {
- $(event.currentTarget).removeClass("ui-state-hover");
- }
- });
- },
- _focusable: function _focusable(element) {
- this.focusable = this.focusable.add(element);
- this._on(element, {
- focusin: function focusin(event) {
- $(event.currentTarget).addClass("ui-state-focus");
- },
- focusout: function focusout(event) {
- $(event.currentTarget).removeClass("ui-state-focus");
- }
- });
- }
- });
- WidgetView.keyCode = {
- BACKSPACE: 8,
- COMMA: 188,
- DELETE: 46,
- DOWN: 40,
- END: 35,
- ENTER: 13,
- ESCAPE: 27,
- HOME: 36,
- LEFT: 37,
- NUMPAD_ADD: 107,
- NUMPAD_DECIMAL: 110,
- NUMPAD_DIVIDE: 111,
- NUMPAD_ENTER: 108,
- NUMPAD_MULTIPLY: 106,
- NUMPAD_SUBTRACT: 109,
- PAGE_DOWN: 34,
- PAGE_UP: 33,
- PERIOD: 190,
- RIGHT: 39,
- SPACE: 32,
- TAB: 9,
- UP: 38,
- PLUS: 107,
- MINUS: 109
- };
- return WidgetView;
- });
|