From c84873b7b55121705ca5ec4d897194faadcb8408 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jordan=20Aug=C3=A9?= Date: Fri, 9 Aug 2013 11:56:42 +0200 Subject: [PATCH] plugin: switching hazelnut to use the Plugin class (wip) template: started documenting the myplugin template to use the Plugin class and demonstrate the different functionalities --- plugins/hazelnut/__init__.py | 56 +++++++++++ plugins/hazelnut/hazelnut.py | 56 ----------- plugins/myplugin/static/js/myplugin.js | 126 ++++++++++++------------- 3 files changed, 114 insertions(+), 124 deletions(-) delete mode 100644 plugins/hazelnut/hazelnut.py diff --git a/plugins/hazelnut/__init__.py b/plugins/hazelnut/__init__.py index e69de29b..9509dc0a 100644 --- a/plugins/hazelnut/__init__.py +++ b/plugins/hazelnut/__init__.py @@ -0,0 +1,56 @@ +from unfold.plugin import Plugin + +class Hazelnut (Plugin): + + # set checkboxes if a final column with checkboxes is desired + # pass columns as the initial set of columns + # if None then this is taken from the query's fields + def __init__ (self, query=None, query_all=None, checkboxes=False, columns=None, datatables_options={}, **settings): + Plugin.__init__ (self, **settings) + self.query = query + # Until we have a proper way to access queries in Python + self.query_all = query_all + self.query_all_uuid = query_all.query_uuid if query_all else None + self.checkboxes=checkboxes + # XXX We need to have some hidden columns until we properly handle dynamic queries + if columns is not None: + self.columns=columns + self.hidden_columns = [] + elif self.query: + self.columns = self.query.fields + if query_all: + # We need a list because sets are not JSON-serilizable + self.hidden_columns = list(self.query_all.fields - self.query.fields) + else: + self.hidden_columns = [] + else: + self.columns = [] + self.hidden_columns = [] + self.datatables_options=datatables_options + + def template_file (self): + return "hazelnut.html" + + def template_env (self, request): + env={} + env.update(self.__dict__) + env['columns']=self.columns + return env + + def requirements (self): + reqs = { + 'js_files' : [ "js/hazelnut.js", + "js/manifold.js", "js/manifold-query.js", + "js/dataTables.js", "js/dataTables.bootstrap.js", "js/with-datatables.js", + "js/spin.presets.js", "js/spin.min.js", "js/jquery.spin.js", + "js/unfold-helper.js", + ] , + 'css_files': [ "css/hazelnut.css" , + "css/dataTables.bootstrap.css", + ], + } + return reqs + + # the list of things passed to the js plugin + def json_settings_list (self): + return ['plugin_uuid', 'domid', 'query_uuid', 'query_all_uuid', 'checkboxes', 'datatables_options', 'hidden_columns'] diff --git a/plugins/hazelnut/hazelnut.py b/plugins/hazelnut/hazelnut.py deleted file mode 100644 index 9509dc0a..00000000 --- a/plugins/hazelnut/hazelnut.py +++ /dev/null @@ -1,56 +0,0 @@ -from unfold.plugin import Plugin - -class Hazelnut (Plugin): - - # set checkboxes if a final column with checkboxes is desired - # pass columns as the initial set of columns - # if None then this is taken from the query's fields - def __init__ (self, query=None, query_all=None, checkboxes=False, columns=None, datatables_options={}, **settings): - Plugin.__init__ (self, **settings) - self.query = query - # Until we have a proper way to access queries in Python - self.query_all = query_all - self.query_all_uuid = query_all.query_uuid if query_all else None - self.checkboxes=checkboxes - # XXX We need to have some hidden columns until we properly handle dynamic queries - if columns is not None: - self.columns=columns - self.hidden_columns = [] - elif self.query: - self.columns = self.query.fields - if query_all: - # We need a list because sets are not JSON-serilizable - self.hidden_columns = list(self.query_all.fields - self.query.fields) - else: - self.hidden_columns = [] - else: - self.columns = [] - self.hidden_columns = [] - self.datatables_options=datatables_options - - def template_file (self): - return "hazelnut.html" - - def template_env (self, request): - env={} - env.update(self.__dict__) - env['columns']=self.columns - return env - - def requirements (self): - reqs = { - 'js_files' : [ "js/hazelnut.js", - "js/manifold.js", "js/manifold-query.js", - "js/dataTables.js", "js/dataTables.bootstrap.js", "js/with-datatables.js", - "js/spin.presets.js", "js/spin.min.js", "js/jquery.spin.js", - "js/unfold-helper.js", - ] , - 'css_files': [ "css/hazelnut.css" , - "css/dataTables.bootstrap.css", - ], - } - return reqs - - # the list of things passed to the js plugin - def json_settings_list (self): - return ['plugin_uuid', 'domid', 'query_uuid', 'query_all_uuid', 'checkboxes', 'datatables_options', 'hidden_columns'] diff --git a/plugins/myplugin/static/js/myplugin.js b/plugins/myplugin/static/js/myplugin.js index 5ea308d6..fee1e4bd 100644 --- a/plugins/myplugin/static/js/myplugin.js +++ b/plugins/myplugin/static/js/myplugin.js @@ -1,92 +1,82 @@ /** - * MySlice MyPlugin demonstration plugin - * Version: 0.1.0 - * URL: http://www.myslice.info - * Description: Template for writing new plugins and illustrating the different possibilities of the plugin API - * Requires: - * Author: The MySlice Team - * Copyright: Copyright 2012-2013 UPMC Sorbonne Universités - * License: GPLv3 + * MyPlugin: demonstration plugin + * Version: 0.1 + * Description: Template for writing new plugins and illustrating the different + * possibilities of the plugin API. + * This file is part of the Manifold project + * Requires: js/plugin.js + * URL: http://www.myslice.info + * Author: Jordan Augé + * Copyright: Copyright 2012-2013 UPMC Sorbonne Universités + * License: GPLv3 */ -/* - * It's a best practice to pass jQuery to an IIFE (Immediately Invoked Function - * Expression) that maps it to the dollar sign so it can't be overwritten by - * another library in the scope of its execution. - */ -(function( $ ){ +(function($){ + + var MyPlugin = Plugin.extend({ + + // Constructor + init: function(options, element) { + // Call the parent constructor, see FAQ when forgotten + this._super(options, element); - var PLUGIN_NAME = 'MyPlugin'; + // Explain this will allow query events to be handled + // What happens when we don't define some events ? + // Some can be less efficient + this.listen_query(options.query_uuid); + this.listen_query(options.query_uuid, 'all'); - // Routing calls - jQuery.fn.ResourcesSelected = function( method ) { - if ( methods[method] ) { - return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); - } else if ( typeof method === 'object' || ! method ) { - return methods.init.apply( this, arguments ); - } else { - jQuery.error( 'Method ' + method + ' does not exist on jQuery' + PLUGIN_NAME ); - } + // GUI Event binding + // call function - }; + }, - /*************************************************************************** - * Public methods - ***************************************************************************/ + /* GUI EVENTS */ - var methods = { + // a function to bind events here + // how to raise manifold events - /** - * @brief Plugin initialization - * @param options : an associative array of setting values - * @return : a jQuery collection of objects on which the plugin is - * applied, which allows to maintain chainability of calls - */ - init : function( options ) { + /* GUI MANIPULATION */ - return this.each(function(){ + // We advise you to write function to change behaviour of the GUI + // Will use naming helpers to access content _inside_ the plugin + // always refer to these functions in the remaining of the code - var $this = $(this); + show_hide_button: function() + { + // this.id, this.el, this.cl, this.els + // same output as a jquery selector with some guarantees + }, - /* An object that will hold private variables and methods */ - var s = new ResourcesSelected(options); - $(this).data('Manifold', s); - - }); // this.each - }, // init + /* TEMPLATES */ - /** - * @brief Plugin destruction - * @return : a jQuery collection of objects on which the plugin is - * applied, which allows to maintain chainability of calls - */ - destroy : function( ) { + // see in the html template + // How to load a template, use of mustache - return this.each(function(){ - var $this = jQuery(this), data = $this.data('Manifold'); - jQuery(window).unbind('Manifold'); - data.Manifold.remove(); - $this.removeData('Manifold'); - }) + /* QUERY HANDLERS */ - }, // destroy + // How to make sure the plugin is not desynchronized + // He should manifest its interest in filters, fields or records + // functions triggered only if the proper listen is done - }; // var methods + // no prefix - /*************************************************************************** - * ResourcesSelected object - ***************************************************************************/ + on_filter_added: function(filter) + { - function MyPlugin(options) - { - /* member variables */ + }, - this.options = options; + // ... be sure to list all events here - /* methods */ + /* RECORD HANDLERS */ + + on_record_received: function(record) + { + // + }, - /* constructor */ + }); - } // function MyPlugin + // TODO Here use cases for instanciating plugins in different ways like in the pastie. })(jQuery); -- 2.43.0