nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / lib / simple.widget.js
1
2 /*
3 Copyright 2013 Marco Braak
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16  */
17 var $, SimpleWidget,
18   slice = [].slice;
19
20 $ = jQuery;
21
22 SimpleWidget = (function() {
23   SimpleWidget.prototype.defaults = {};
24
25   function SimpleWidget(el, options) {
26     this.$el = $(el);
27     this.options = $.extend({}, this.defaults, options);
28   }
29
30   SimpleWidget.prototype.destroy = function() {
31     return this._deinit();
32   };
33
34   SimpleWidget.prototype._init = function() {
35     return null;
36   };
37
38   SimpleWidget.prototype._deinit = function() {
39     return null;
40   };
41
42   SimpleWidget.register = function(widget_class, widget_name) {
43     var callFunction, createWidget, destroyWidget, getDataKey, getWidgetData;
44     getDataKey = function() {
45       return "simple_widget_" + widget_name;
46     };
47     getWidgetData = function(el, data_key) {
48       var widget;
49       widget = $.data(el, data_key);
50       if (widget && (widget instanceof SimpleWidget)) {
51         return widget;
52       } else {
53         return null;
54       }
55     };
56     createWidget = function($el, options) {
57       var data_key, el, existing_widget, i, len, widget;
58       data_key = getDataKey();
59       for (i = 0, len = $el.length; i < len; i++) {
60         el = $el[i];
61         existing_widget = getWidgetData(el, data_key);
62         if (!existing_widget) {
63           widget = new widget_class(el, options);
64           if (!$.data(el, data_key)) {
65             $.data(el, data_key, widget);
66           }
67           widget._init();
68         }
69       }
70       return $el;
71     };
72     destroyWidget = function($el) {
73       var data_key, el, i, len, results, widget;
74       data_key = getDataKey();
75       results = [];
76       for (i = 0, len = $el.length; i < len; i++) {
77         el = $el[i];
78         widget = getWidgetData(el, data_key);
79         if (widget) {
80           widget.destroy();
81         }
82         results.push($.removeData(el, data_key));
83       }
84       return results;
85     };
86     callFunction = function($el, function_name, args) {
87       var el, i, len, result, widget, widget_function;
88       result = null;
89       for (i = 0, len = $el.length; i < len; i++) {
90         el = $el[i];
91         widget = $.data(el, getDataKey());
92         if (widget && (widget instanceof SimpleWidget)) {
93           widget_function = widget[function_name];
94           if (widget_function && (typeof widget_function === 'function')) {
95             result = widget_function.apply(widget, args);
96           }
97         }
98       }
99       return result;
100     };
101     return $.fn[widget_name] = function() {
102       var $el, args, argument1, function_name, options;
103       argument1 = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
104       $el = this;
105       if (argument1 === void 0 || typeof argument1 === 'object') {
106         options = argument1;
107         return createWidget($el, options);
108       } else if (typeof argument1 === 'string' && argument1[0] !== '_') {
109         function_name = argument1;
110         if (function_name === 'destroy') {
111           return destroyWidget($el);
112         } else if (function_name === 'get_widget_class') {
113           return widget_class;
114         } else {
115           return callFunction($el, function_name, args);
116         }
117       }
118     };
119   };
120
121   return SimpleWidget;
122
123 })();
124
125 module.exports = SimpleWidget;