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);
33 // programmatically add specific class for publishing events
34 // used in manifold.js for triggering API events
35 if ( ! this.$element.hasClass('pubsub')) this.$element.addClass('pubsub');
37 // return this so we can chain/use the bridge with less code.
41 has_query_handler: function() {
42 return (typeof this.on_filter_added === 'function');
45 _query_handler: function(prefix, event_type, data) {
46 // We suppose this.query_handler_prefix has been defined if this
47 // callback is triggered
54 fn = 'filter_removed';
72 fn = 'on_' + prefix + fn;
73 if (typeof this[fn] === 'function') {
74 // call with data as parameter
75 // XXX implement anti loop
80 _record_handler: function(prefix, event_type, record) {
81 // We suppose this.query_handler_prefix has been defined if this
82 // callback is triggered
92 fn = 'query_in_progress';
97 case FIELD_STATE_CHANGED:
98 fn = 'field_state_changed';
104 fn = 'on_' + prefix + fn;
105 if (typeof this[fn] === 'function') {
106 // call with data as parameter
107 // XXX implement anti loop
112 get_handler_function: function(type, prefix) {
114 return $.proxy(function(e, event_type, record) {
115 return this['_' + type + '_handler'](prefix, event_type, record);
119 listen_query: function(query_uuid, prefix) {
120 // default: prefix = ''
121 prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_');
123 this.$element.on(manifold.get_channel('query', query_uuid), this.get_handler_function('query', prefix));
124 this.$element.on(manifold.get_channel('record', query_uuid), this.get_handler_function('record', prefix));
129 /* Helper functions for naming HTML elements (ID, classes), with support for filters and fields */
132 var ret = this.options.plugin_uuid;
133 for (var i = 0; i < arguments.length; i++) {
134 ret = ret + manifold.separator + arguments[i];
140 if (arguments.length == 0) {
141 return $('#' + this.id());
143 // We make sure to search _inside_ the dom tag of the plugin
144 return $('#' + this.id.apply(this, arguments), this.elmt());
148 elts: function(cls) {
149 return $('.' + cls, this.elmt());
152 id_from_filter: function(filter, use_value) {
153 use_value = typeof use_value !== 'undefined' ? use_value : true;
157 var value = filter[2];
158 var op_str = this.getOperatorLabel(op);
159 var s = manifold.separator;
162 return 'filter' + s + key + s + op_str + s + value;
164 return 'filter' + s + key + s + op_str;
168 str_from_filter: function(filter) {
169 return filter[0] + ' ' + filter[1] + ' ' + filter[2];
172 array_from_id: function(id) {
173 var ret = id.split(manifold.separator);
174 ret.shift(); // remove plugin_uuid at the beginning
178 id_from_field: function(field) {
179 return 'field' + manifold.separator + field;
182 field_from_id: function(id) {
184 if (typeof id === 'string') {
185 array = id.split(manifold.separator);
186 } else { // We suppose we have an array ('object')
189 // array = ['field', FIELD_NAME]
193 id_from_key: function(key_field, value) {
195 return key_field + manifold.separator + this.escape_id(value).replace(/\\/g, '');
199 // at some point in time we used to have a helper function named 'flat_id' here
200 // the goals was to sort of normalize id's but it turned out we can get rid of that
201 // in a nutshell, we would have an id (can be urn, hrn, whatever) and
202 // we want to be able to retrieve a DOM element based on that (e.g. a checkbox)
203 // so we did something like <tag id="some-id-that-comes-from-the-db">
204 // and then $("#some-id-that-comes-from-the-db")
205 // however the syntax for that selector prevents from using some characters in id
206 // and so for some of our ids this won't work
207 // instead of 'flattening' we now do this instead
208 // <tag some_id="then!we:can+use.what$we!want">
209 // and to retrieve it
210 // $("[some_id='then!we:can+use.what$we!want']")
211 // which thanks to the quotes, works; and you can use this with id as well in fact
212 // of course if now we have quotes in the id it's going to squeak, but well..
214 // escape (read: backslashes) some meta-chars in input
215 escape_id: function(id) {
216 if( id !== undefined){
217 return id.replace( /(:|\.|\[|\])/g, "\\$1" );
219 return "undefined-id";
223 id_from_record: function(method, record) {
224 var keys = manifold.metadata.get_key(method);
231 switch (Object.toType(key)) {
233 if (!(key in record))
235 return this.id_from_key(key, record[key]);
238 throw 'Not implemented';
242 key_from_id: function(id) {
243 // NOTE this works only for simple keys
246 if (typeof id === 'string') {
247 array = id.split(manifold.separator);
248 } else { // We suppose we have an array ('object')
252 // arguments has the initial id but lacks the key field name (see id_from_key), so we are even
253 // we finally add +1 for the plugin_uuid at the beginning
254 return array[arguments.length + 1];
258 // plugin-helper.js is about managing toggled state
259 // it would be beneficial to merge it in here
260 toggle_on: function () { return this.toggle("true"); },
261 toggle_off: function () { return this.toggle("false"); },
262 toggle: function (status) {
263 plugin_helper.set_toggle_status (this.options.plugin_uuid,status);
267 // use spin() to get our default spin settings (called presets)
268 // use spin(true) to get spin's builtin defaults
269 // you can also call spin_presets() yourself and tweak what you need to, like topmenuvalidation does
270 spin: function (presets) {
271 var presets = ( presets === undefined ) ? spin_presets() : presets;
272 try { this.$element.spin(presets); }
273 catch (err) { messages.debug("Cannot turn on spin " + err); }
277 try { this.$element.spin(false); }
278 catch (err) { messages.debug("Cannot turn off spin " + err); }
283 load_template: function(name, ctx) {
284 return Mustache.render(this.elmt(name).html(), ctx);