2 Copyright 2013 Marco Braak
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 constructor: (el, options) ->
25 @options = $.extend({}, @defaults, options)
36 @register = (widget_class, widget_name) ->
38 return "simple_widget_#{widget_name}"
40 getWidgetData = (el, data_key) ->
41 widget = $.data(el, data_key)
43 if widget and (widget instanceof SimpleWidget)
48 createWidget = ($el, options) ->
49 data_key = getDataKey()
52 existing_widget = getWidgetData(el, data_key)
54 if not existing_widget
55 widget = new widget_class(el, options)
57 if not $.data(el, data_key)
58 $.data(el, data_key, widget)
60 # Call init after setting data, so we can call methods
65 destroyWidget = ($el) ->
66 data_key = getDataKey()
69 widget = getWidgetData(el, data_key)
74 $.removeData(el, data_key)
76 callFunction = ($el, function_name, args) ->
80 widget = $.data(el, getDataKey())
82 if widget and (widget instanceof SimpleWidget)
83 widget_function = widget[function_name]
85 if widget_function and (typeof widget_function == 'function')
86 result = widget_function.apply(widget, args)
90 $.fn[widget_name] = (argument1, args...) ->
93 if argument1 is undefined or typeof argument1 == 'object'
95 return createWidget($el, options)
96 else if typeof argument1 == 'string' and argument1[0] != '_'
97 function_name = argument1
99 if function_name == 'destroy'
100 return destroyWidget($el)
101 else if function_name == 'get_widget_class'
104 return callFunction($el, function_name, args)
107 module.exports = SimpleWidget