b72cc30cd81116a0b9d34dfdf07746b2ae89148f
[myslice.git] / manifold / js / plugin.js
1 // INHERITANCE
2 // http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/
3 // We will use John Resig's proposal
4
5 // http://pastie.org/517177
6
7 // NOTE: missing a destroy function
8
9 $.plugin = function(name, object) {
10     $.fn[name] = function(options) {
11         var args = Array.prototype.slice.call(arguments, 1);
12         return this.each(function() {
13             var instance = $.data(this, name);
14             if (instance) {
15                 instance[options].apply(instance, args);
16             } else {
17                 instance = $.data(this, name, new object(options, this));
18             }
19         });
20     };
21 };
22
23 var Plugin = Class.extend({
24
25     init: function(options, element)
26     {
27         // Mix in the passed in options with the default options
28         this.options = $.extend({}, this.default_options, options);
29
30         // Save the element reference, both as a jQuery
31         // reference and a normal reference
32         this.element  = element;
33         this.$element = $(element);
34
35         // return this so we can chain/use the bridge with less code.
36         return this;
37     },
38
39     has_query_handler: function() {
40         return (typeof this.on_filter_added === 'function');
41     },
42
43     _query_handler: function(prefix, event_type, data)
44     {
45         // We suppose this.query_handler_prefix has been defined if this
46         // callback is triggered    
47         var fn;
48         switch(event_type) {
49             case FILTER_ADDED:
50                 fn = 'filter_added';
51                 break;
52             case FILTER_REMOVED:
53                 fn = 'filter_removed';
54                 break;
55             case CLEAR_FILTERS:
56                 fn = 'filter_clear';
57                 break;
58             case FIELD_ADDED:
59                 fn = 'field_added';
60                 break;
61             case FIELD_REMOVED:
62                 fn = 'field_removed';
63                 break;
64             case CLEAR_FIELDS:
65                 fn = 'field_clear';
66                 break;
67             default:
68                 return;
69         } // switch
70         
71         fn = 'on_' + prefix + fn;
72         if (typeof this[fn] === 'function') {
73             // call with data as parameter
74             // XXX implement anti loop
75             this[fn](data);
76         }
77     },
78
79     _record_handler: function(prefix, event_type, record)
80     {
81         // We suppose this.query_handler_prefix has been defined if this
82         // callback is triggered    
83         var fn;
84         switch(event_type) {
85             case NEW_RECORD:
86                 fn = 'new_record';
87                 break;
88             case CLEAR_RECORDS:
89                 fn = 'clear_records';
90                 break;
91             case IN_PROGRESS:
92                 fn = 'query_in_progress';
93                 break;
94             case DONE:
95                 fn = 'query_done';
96                 break;
97             default:
98                 return;
99         } // switch
100         
101         fn = 'on_' + prefix + fn;
102         if (typeof this[fn] === 'function') {
103             // call with data as parameter
104             // XXX implement anti loop
105             this[fn](record);
106         }
107     },
108
109     get_handler_function: function(type, prefix)
110     {
111         
112         return $.proxy(function(e, event_type, record) {
113             return this['_' + type + '_handler'](prefix, event_type, record);
114         }, this);
115     },
116
117     listen_query: function(query_uuid, prefix)
118     {
119         // default: prefix = ''
120         prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_');
121
122         this.$element.on(manifold.get_channel('query', query_uuid),  this.get_handler_function('query',  prefix));
123         this.$element.on(manifold.get_channel('record', query_uuid),  this.get_handler_function('record', prefix));
124     },
125
126     default_options: {},
127
128     /* Helper functions for naming HTML elements (ID, classes), with support for filters and fields */
129
130     id: function()
131     {
132         var ret = this.options.plugin_uuid;
133         for (var i = 0; i < arguments.length; i++) {
134             ret = ret + manifold.separator + arguments[i];
135         }
136         return ret;
137     },
138
139     el: function()
140     {
141         if (arguments.length == 0) {
142             return $('#' + this.id());
143         } else {
144             // We make sure to search _inside_ the dom tag of the plugin
145             return $('#' + this.id.apply(this, arguments), this.el());
146         }
147     },
148
149     els: function(cls)
150     {
151         return $('.' + cls, this.el());
152     },
153
154     id_from_filter: function(filter, use_value)
155     {
156         use_value = typeof use_value !== 'undefined' ? use_value : true;
157
158         var key    = filter[0];
159         var op     = filter[1];
160         var value  = filter[2];
161         var op_str = this.getOperatorLabel(op);
162         var s      = manifold.separator;
163
164         if (use_value) {
165             return 'filter' + s + key + s + op_str + s + value;
166         } else {
167             return 'filter' + s + key + s + op_str;
168         }
169     },
170
171     str_from_filter: function(filter)
172     {
173         return filter[0] + ' ' + filter[1] + ' ' + filter[2];
174     },
175
176     array_from_id: function(id)
177     {
178         var ret = id.split(manifold.separator);
179         ret.shift(); // remove plugin_uuid at the beginning
180         return ret;
181     },
182
183     id_from_field: function(field)
184     {
185         return 'field' + manifold.separator + field;
186     },
187
188     field_from_id: function(id)
189     {
190         var array;
191         if (typeof id === 'string') {
192             array = id.split(manifold.separator);
193         } else { // We suppose we have an array ('object')
194             array = id;
195         }
196         // array = ['field', FIELD_NAME]
197         return array[1];
198     },
199
200     spin: function()
201     {
202         manifold.spin(this.element);
203     },
204
205     unspin: function()
206     {
207         manifold.spin(this.element, false);
208     },
209
210 });