2 // http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/
3 // We will use John Resig's proposal
5 // http://pastie.org/517177
7 // NOTE: missing a destroy function
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);
15 instance[options].apply(instance, args);
17 instance = $.data(this, name, new object(options, this));
23 var Plugin = Class.extend({
25 init: function(options, element) {
26 // Mix in the passed in options with the default options
27 this.options = $.extend({}, this.default_options, options);
29 // Save the element reference, both as a jQuery
30 // reference and a normal reference
31 this.element = element;
32 this.$element = $(element);
34 // return this so we can chain/use the bridge with less code.
38 has_query_handler: function() {
39 return (typeof this.on_filter_added === 'function');
42 _query_handler: function(prefix, event_type, data) {
43 // We suppose this.query_handler_prefix has been defined if this
44 // callback is triggered
51 fn = 'filter_removed';
69 fn = 'on_' + prefix + fn;
70 if (typeof this[fn] === 'function') {
71 // call with data as parameter
72 // XXX implement anti loop
77 _record_handler: function(prefix, event_type, record) {
78 // We suppose this.query_handler_prefix has been defined if this
79 // callback is triggered
89 fn = 'query_in_progress';
94 case FIELD_STATE_CHANGED:
95 fn = 'field_state_changed';
101 fn = 'on_' + prefix + fn;
102 if (typeof this[fn] === 'function') {
103 // call with data as parameter
104 // XXX implement anti loop
109 get_handler_function: function(type, prefix) {
111 return $.proxy(function(e, event_type, record) {
112 return this['_' + type + '_handler'](prefix, event_type, record);
116 listen_query: function(query_uuid, prefix) {
117 // default: prefix = ''
118 prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_');
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));
126 /* Helper functions for naming HTML elements (ID, classes), with support for filters and fields */
129 var ret = this.options.plugin_uuid;
130 for (var i = 0; i < arguments.length; i++) {
131 ret = ret + manifold.separator + arguments[i];
137 if (arguments.length == 0) {
138 return $('#' + this.id());
140 // We make sure to search _inside_ the dom tag of the plugin
141 return $('#' + this.id.apply(this, arguments), this.elmt());
145 elts: function(cls) {
146 return $('.' + cls, this.elmt());
149 id_from_filter: function(filter, use_value) {
150 use_value = typeof use_value !== 'undefined' ? use_value : true;
154 var value = filter[2];
155 var op_str = this.getOperatorLabel(op);
156 var s = manifold.separator;
159 return 'filter' + s + key + s + op_str + s + value;
161 return 'filter' + s + key + s + op_str;
165 str_from_filter: function(filter) {
166 return filter[0] + ' ' + filter[1] + ' ' + filter[2];
169 array_from_id: function(id) {
170 var ret = id.split(manifold.separator);
171 ret.shift(); // remove plugin_uuid at the beginning
175 id_from_field: function(field) {
176 return 'field' + manifold.separator + field;
179 field_from_id: function(id) {
181 if (typeof id === 'string') {
182 array = id.split(manifold.separator);
183 } else { // We suppose we have an array ('object')
186 // array = ['field', FIELD_NAME]
190 id_from_key: function(key_field, value) {
192 return key_field + manifold.separator + this.escape_id(value).replace(/\\/g, '');
196 // at some point in time we used to have a helper function named 'flat_id' here
197 // the goals was to sort of normalize id's but it turned out we can get rid of that
198 // in a nutshell, we would have an id (can be urn, hrn, whatever) and
199 // we want to be able to retrieve a DOM element based on that (e.g. a checkbox)
200 // so we did something like <tag id="some-id-that-comes-from-the-db">
201 // and then $("#some-id-that-comes-from-the-db")
202 // however the syntax for that selector prevents from using some characters in id
203 // and so for some of our ids this won't work
204 // instead of 'flattening' we now do this instead
205 // <tag some_id="then!we:can+use.what$we!want">
206 // and to retrieve it
207 // $("[some_id='then!we:can+use.what$we!want']")
208 // which thanks to the quotes, works; and you can use this with id as well in fact
209 // of course if now we have quotes in the id it's going to squeak, but well..
211 // escape (read: backslashes) some meta-chars in input
212 escape_id: function(id) {
213 if( id !== undefined){
214 return id.replace( /(:|\.|\[|\])/g, "\\$1" );
216 return "undefined-id";
220 id_from_record: function(method, record) {
221 var keys = manifold.metadata.get_key(method);
228 switch (Object.toType(key)) {
230 if (!(key in record))
232 return this.id_from_key(key, record[key]);
235 throw 'Not implemented';
239 key_from_id: function(id) {
240 // NOTE this works only for simple keys
243 if (typeof id === 'string') {
244 array = id.split(manifold.separator);
245 } else { // We suppose we have an array ('object')
249 // arguments has the initial id but lacks the key field name (see id_from_key), so we are even
250 // we finally add +1 for the plugin_uuid at the beginning
251 return array[arguments.length + 1];
255 // plugin-helper.js is about managing toggled state
256 // it would be beneficial to merge it in here
257 toggle_on: function () { return this.toggle("true"); },
258 toggle_off: function () { return this.toggle("false"); },
259 toggle: function (status) {
260 plugin_helper.set_toggle_status (this.options.plugin_uuid,status);
266 manifold.spin(this.element);
270 manifold.spin(this.element, false);
275 load_template: function(name, ctx) {
276 return Mustache.render(this.elmt(name).html(), ctx);