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