X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=manifold%2Fjs%2Fplugin.js;h=b72cc30cd81116a0b9d34dfdf07746b2ae89148f;hb=8c344999d6ee3b3a9172a1068a77c83af77a7aee;hp=5d3df742cb6d6db2255bfa3392cc6b22469ef270;hpb=f7419e1b9bedd90e390fa27bf7d7868db617caee;p=myslice.git diff --git a/manifold/js/plugin.js b/manifold/js/plugin.js index 5d3df742..b72cc30c 100644 --- a/manifold/js/plugin.js +++ b/manifold/js/plugin.js @@ -40,7 +40,7 @@ var Plugin = Class.extend({ return (typeof this.on_filter_added === 'function'); }, - _query_handler: function(e, event_type, data) + _query_handler: function(prefix, event_type, data) { // We suppose this.query_handler_prefix has been defined if this // callback is triggered @@ -68,7 +68,7 @@ var Plugin = Class.extend({ return; } // switch - fn = 'on_' + this.query_handler_prefix + fn; + fn = 'on_' + prefix + fn; if (typeof this[fn] === 'function') { // call with data as parameter // XXX implement anti loop @@ -76,13 +76,57 @@ var Plugin = Class.extend({ } }, - listen_query: function(query_uuid, prefix) { - this.query_handler_prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_'); - this.$element.on(manifold.get_query_channel(query_uuid), $.proxy(this._query_handler, this)); + _record_handler: function(prefix, event_type, record) + { + // We suppose this.query_handler_prefix has been defined if this + // callback is triggered + var fn; + switch(event_type) { + case NEW_RECORD: + fn = 'new_record'; + break; + case CLEAR_RECORDS: + fn = 'clear_records'; + break; + case IN_PROGRESS: + fn = 'query_in_progress'; + break; + case DONE: + fn = 'query_done'; + break; + default: + return; + } // switch + + fn = 'on_' + prefix + fn; + if (typeof this[fn] === 'function') { + // call with data as parameter + // XXX implement anti loop + this[fn](record); + } + }, + + get_handler_function: function(type, prefix) + { + + return $.proxy(function(e, event_type, record) { + return this['_' + type + '_handler'](prefix, event_type, record); + }, this); + }, + + listen_query: function(query_uuid, prefix) + { + // default: prefix = '' + prefix = (typeof prefix === 'undefined') ? '' : (prefix + '_'); + + this.$element.on(manifold.get_channel('query', query_uuid), this.get_handler_function('query', prefix)); + this.$element.on(manifold.get_channel('record', query_uuid), this.get_handler_function('record', prefix)); }, default_options: {}, + /* Helper functions for naming HTML elements (ID, classes), with support for filters and fields */ + id: function() { var ret = this.options.plugin_uuid; @@ -129,4 +173,38 @@ var Plugin = Class.extend({ return filter[0] + ' ' + filter[1] + ' ' + filter[2]; }, + array_from_id: function(id) + { + var ret = id.split(manifold.separator); + ret.shift(); // remove plugin_uuid at the beginning + return ret; + }, + + id_from_field: function(field) + { + return 'field' + manifold.separator + field; + }, + + field_from_id: function(id) + { + var array; + if (typeof id === 'string') { + array = id.split(manifold.separator); + } else { // We suppose we have an array ('object') + array = id; + } + // array = ['field', FIELD_NAME] + return array[1]; + }, + + spin: function() + { + manifold.spin(this.element); + }, + + unspin: function() + { + manifold.spin(this.element, false); + }, + });