escape_id moves into plugin.js
[myslice.git] / manifold / static / 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         // Mix in the passed in options with the default options
27         this.options = $.extend({}, this.default_options, options);
28
29         // Save the element reference, both as a jQuery
30         // reference and a normal reference
31         this.element  = element;
32         this.$element = $(element);
33
34         // return this so we can chain/use the bridge with less code.
35         return this;
36     },
37
38     has_query_handler: function() {
39         return (typeof this.on_filter_added === 'function');
40     },
41
42     _query_handler: function(prefix, event_type, data) {
43         // We suppose this.query_handler_prefix has been defined if this
44         // callback is triggered    
45         var fn;
46         switch(event_type) {
47         case FILTER_ADDED:
48             fn = 'filter_added';
49             break;
50         case FILTER_REMOVED:
51             fn = 'filter_removed';
52             break;
53         case CLEAR_FILTERS:
54             fn = 'filter_clear';
55             break;
56         case FIELD_ADDED:
57             fn = 'field_added';
58             break;
59         case FIELD_REMOVED:
60             fn = 'field_removed';
61             break;
62         case CLEAR_FIELDS:
63             fn = 'field_clear';
64             break;
65         default:
66             return;
67         } // switch
68         
69         fn = 'on_' + prefix + fn;
70         if (typeof this[fn] === 'function') {
71             // call with data as parameter
72             // XXX implement anti loop
73             this[fn](data);
74         }
75     },
76
77     _record_handler: function(prefix, event_type, record) {
78         // We suppose this.query_handler_prefix has been defined if this
79         // callback is triggered    
80         var fn;
81         switch(event_type) {
82         case NEW_RECORD:
83             fn = 'new_record';
84             break;
85         case CLEAR_RECORDS:
86             fn = 'clear_records';
87             break;
88         case IN_PROGRESS:
89             fn = 'query_in_progress';
90             break;
91         case DONE:
92             fn = 'query_done';
93             break;
94         case FIELD_STATE_CHANGED:
95             fn = 'field_state_changed';
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         return $.proxy(function(e, event_type, record) {
112             return this['_' + type + '_handler'](prefix, event_type, record);
113         }, this);
114     },
115
116     listen_query: function(query_uuid, prefix) {
117         // default: prefix = ''
118         prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_');
119
120         this.$element.on(manifold.get_channel('query', query_uuid),  this.get_handler_function('query',  prefix));
121         this.$element.on(manifold.get_channel('record', query_uuid),  this.get_handler_function('record', prefix));
122     },
123
124     default_options: {},
125
126     /* Helper functions for naming HTML elements (ID, classes), with support for filters and fields */
127
128     id: function() {
129         var ret = this.options.plugin_uuid;
130         for (var i = 0; i < arguments.length; i++) {
131             ret = ret + manifold.separator + arguments[i];
132         }
133         return ret;
134     },
135
136     elmt: function() {
137         if (arguments.length == 0) {
138             return $('#' + this.id());
139         } else {
140             // We make sure to search _inside_ the dom tag of the plugin
141             return $('#' + this.id.apply(this, arguments), this.elmt());
142         }
143     },
144
145     elts: function(cls) {
146         return $('.' + cls, this.elmt());
147     },
148
149     id_from_filter: function(filter, use_value) {
150         use_value = typeof use_value !== 'undefined' ? use_value : true;
151
152         var key    = filter[0];
153         var op     = filter[1];
154         var value  = filter[2];
155         var op_str = this.getOperatorLabel(op);
156         var s      = manifold.separator;
157
158         if (use_value) {
159             return 'filter' + s + key + s + op_str + s + value;
160         } else {
161             return 'filter' + s + key + s + op_str;
162         }
163     },
164
165     str_from_filter: function(filter) {
166         return filter[0] + ' ' + filter[1] + ' ' + filter[2];
167     },
168
169     array_from_id: function(id) {
170         var ret = id.split(manifold.separator);
171         ret.shift(); // remove plugin_uuid at the beginning
172         return ret;
173     },
174
175     id_from_field: function(field) {
176         return 'field' + manifold.separator + field;
177     },
178
179     field_from_id: function(id) {
180         var array;
181         if (typeof id === 'string') {
182             array = id.split(manifold.separator);
183         } else { // We suppose we have an array ('object')
184             array = id;
185         }
186         // array = ['field', FIELD_NAME]
187         return array[1];
188     },
189
190     id_from_key: function(key_field, value) {
191         
192         return key_field + manifold.separator + this.escape_id(value).replace(/\\/g, '');
193     },
194
195     // we do not need to carry around all the nonsense about backslashing dots in hrns
196     // likewise, DOM ids do not like to have dots in them
197     // because "#foo.bar" matches an elem named foo with class bar - not an id that is foo.bar
198     // so this method gives you a valid DOMID but that cannot be 'reversed' back to retrieve an hrn or the like
199     // e.g. 
200     // input=input="ple.aluiple.host147-82-static\\.93-94-b\\.business\\.telecomitalia\\.it"
201     // > "ple.aluiple.host147-82-static\.93-94-b\.business\.telecomitalia\.it"
202     // flat_id(input)
203     // "ple-aluiple-host147-82-static-93-94-b-business-telecomitalia-it"
204     flat_id : function (id_in) {
205         return id_in.replace(/\\\./g,"-").replace(/\\/g,"-").replace(/\./g,"-");
206     },
207
208     // escape (read: backslashes) some meta-chars in input
209     escape_id: function(id) {
210         if( id !== undefined){
211             return id.replace( /(:|\.|\[|\])/g, "\\$1" );
212         }else{
213             return "undefined-id";
214         }
215     },
216
217     id_from_record: function(method, record) {
218         var keys = manifold.metadata.get_key(method);
219         if (!keys)
220             return;
221         if (keys.length > 1)
222             return;
223
224         var key = keys[0];
225         switch (Object.toType(key)) {
226         case 'string':
227             if (!(key in record))
228                 return null;
229             return this.id_from_key(key, record[key]);
230             
231         default:
232             throw 'Not implemented';
233         }
234     },
235
236     key_from_id: function(id) {
237         // NOTE this works only for simple keys
238
239         var array;
240         if (typeof id === 'string') {
241             array = id.split(manifold.separator);
242         } else { // We suppose we have an array ('object')
243             array = id;
244         }
245
246         // arguments has the initial id but lacks the key field name (see id_from_key), so we are even
247         // we finally add +1 for the plugin_uuid at the beginning
248         return array[arguments.length + 1];
249     },
250
251     /* SPIN */
252
253     spin: function() {
254         manifold.spin(this.element);
255     },
256
257     unspin: function() {
258         manifold.spin(this.element, false);
259     },
260
261     /* TEMPLATE */
262
263     load_template: function(name, ctx) {
264         return Mustache.render(this.elmt(name).html(), ctx);
265     },
266
267 });