From: Thierry Parmentelat Date: Wed, 6 Mar 2013 17:00:17 +0000 (+0100) Subject: simplelist to reach $.subscribe as defined in tophat-core.js X-Git-Tag: myslice-django-0.1-1~68 X-Git-Url: http://git.onelab.eu/?p=unfold.git;a=commitdiff_plain;h=fb7dbecd4a79019e40139a8bde65f8737b44ae76 simplelist to reach $.subscribe as defined in tophat-core.js --- diff --git a/engine/static/js/tophat-core.js b/engine/static/js/tophat-core.js new file mode 100644 index 00000000..ae9067f5 --- /dev/null +++ b/engine/static/js/tophat-core.js @@ -0,0 +1,119 @@ +/* + * This file is included in tophat_render.php + */ + +/* getting random error messages with this... -- jordan + wait until query code is fixed +jQuery(document).ready(function() { + // ajax default settings + jQuery.ajaxSetup({ + timeout: 3000, + error:function(x,e){ + if('parsererror'==e) { + alert('Sorry, we ran into a technical problem (parse error). Please try again...'); + } else if('timeout'==e) { + alert('Request timed out. Please try again...'); + } + else if ( "status" in x ) { + if(0 == x.status){ + alert('You are offline! Please check your network.'); + }else if (404 == x.status){ + alert('Sorry, we ran into a technical problem (404). Please try again...'); + }else if(500 == x.status){ + alert('Sorry, we ran into a technical problem (500). Please try again...'); + } + } + else { + alert('Sorry, we ran into a technical problem (unknown error). Please try again...'); + } + } + }); +}); +*/ + +function get_value(value) { + //if(typeof(jQuery(value).attr('value'))!="undefined"){ + if (/.*<\/span>/i.test(value)) { + return jQuery(value).attr('value'); + } else { + return value; + } +} + +/* +From: http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object + I want to note that the .clone() method in jQuery only clones DOM elements. In order to clone JavaScript objects, you would do: + + // Shallow copy + var newObject = jQuery.extend({}, oldObject); + + // Deep copy + var newObject = jQuery.extend(true, {}, oldObject); + + More information can be found in the jQuery documentation +*/ +function clone_object(obj) { + return jQuery.extend(true, {}, obj); +} + +/* https://gist.github.com/661855 */ +(function(jQuery) { + + var o = jQuery({}); + + jQuery.subscribe = function( types, selector, data, fn) { + /* borrowed from jQuery */ + if ( data == null && fn == null ) { + // ( types, fn ) + fn = selector; + data = selector = undefined; + } else if ( fn == null ) { + if ( typeof selector === "string" ) { + // ( types, selector, fn ) + fn = data; + data = undefined; + } else { + // ( types, data, fn ) + fn = data; + data = selector; + selector = undefined; + } + } + /* */ + + /* We use an indirection function that will clone the object passed in + * parameter to the subscribe callback + * + * FIXME currently we only clone query objects which are the only ones + * supported and editable, we might have the same issue with results but + * the page load time will be severely affected... + */ + o.on.apply(o, [types, selector, data, function() { + for(i = 1; i < arguments.length; i++) { + if ( arguments[i].constructor.name == 'Query' ) + arguments[i] = arguments[i].clone(); + } + fn.apply(o, arguments); + }]); + }; + + jQuery.unsubscribe = function() { + o.off.apply(o, arguments); + }; + + jQuery.publish = function() { + o.trigger.apply(o, arguments); + }; + +}(jQuery)); + + +function executeFunctionByName(functionName, context /*, args */) { + var args = Array.prototype.slice.call(arguments).splice(2); + var namespaces = functionName.split("."); + var func = namespaces.pop(); + for(var i = 0; i < namespaces.length; i++) { + context = context[namespaces[i]]; + } + return context[func].apply(this, args); +} diff --git a/engine/templates/plugin-setenv.js b/engine/templates/plugin-setenv.js index 2cfd6e34..f6c27faa 100644 --- a/engine/templates/plugin-setenv.js +++ b/engine/templates/plugin-setenv.js @@ -1,6 +1 @@ -{# from plugin.php Plugin.render() #} -{# Plugin initialization (if the plugin has the right structure) #} -if (typeof jQuery('#{{ domid }}').{{ classname }} != 'undefined') { - jQuery('#{{ domid }}').{{ classname }}({{ settings_json|safe }}); - {#jQuery('#{{ domid }}').{{ classname }}('show');#} -}; +$(document).ready(function() { jQuery('#{{ domid }}').{{ classname }}({{ settings_json|safe }}); }); diff --git a/plugins/simplelist.py b/plugins/simplelist.py index 3a6e9886..9fd2ba77 100644 --- a/plugins/simplelist.py +++ b/plugins/simplelist.py @@ -14,7 +14,8 @@ class SimpleList (Plugin) : def template_file (self): return "simplelist.html" def requirements (self): - reqs = { 'js_files' : [ "js/plugin.js", "js/query.js", "js/simplelist.js" ], + reqs = { 'js_files' : [ "js/plugin.js", "js/query.js", + "js/tophat-core.js", "js/simplelist.js" ], 'css_files': [ "css/simplelist.css" ], } if self.with_datatables: diff --git a/plugins/static/js/simplelist.js b/plugins/static/js/simplelist.js index 7247463f..58c656a2 100644 --- a/plugins/static/js/simplelist.js +++ b/plugins/static/js/simplelist.js @@ -2,77 +2,59 @@ * MySlice SimpleList plugin * Version: 0.1.0 * URL: http://www.myslice.info - * Description: Google maps display of geolocated data + * Description: display simple lists like slices or testbeds * Requires: * Author: The MySlice Team - * Copyright: Copyright 2012 UPMC Sorbonne Universités + * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA * License: GPLv3 */ -(function(jQuery){ - - var methods = { - init : function( options ) { - - return this.each(function(){ - - var $this = jQuery(this), - data = $this.data('SimpleList'), SimpleList = jQuery('
', { text : $this.attr('title') }); - - // If the plugin hasn't been initialized yet - if ( ! data ) { - - /* Plugin initialization */ - - /* Subscribe to query updates */ - var url='/results/' + options.query_uuid + '/changed'; - jQuery.subscribe(url, {instance: $this}, update_list); - - /* End of plugin initialization */ - - $this.data('SimpleList', { - options: options, - target : $this, - SimpleList : SimpleList - }); - - } - }); - }, - destroy : function( ) { - - return this.each(function(){ - var $this = jQuery(this), data = $this.data('SimpleList'); - jQuery(window).unbind('SimpleList'); - data.SimpleList.remove(); - $this.removeData('SimpleList'); - }) - +(function($){ + var methods = { + init : function( options ) { + return this.each(function(){ + var $this = $(this); + var data = $this.data('SimpleList'); + console.log("data" + data); +// looks like $this.attr('title') in undefined.. +// console.log('iterating in simplelist.init with data='+data+' and title='+$this.attr('title')); + /* create an empty DOM object */ + var SimpleList = $('
', { text : $this.attr('title') }); + // If the plugin hasn't been initialized yet + if ( ! data ) { + /* Subscribe to query updates */ + var url='/results/' + options.query_uuid + '/changed'; + $.subscribe(url, {instance: this}, update_list); + window.console.log('subscribing to ' + url); + $this.data('SimpleList', {options: options, target : this, SimpleList : SimpleList}); + } + }); + }, + destroy : function( ) { + return this.each(function(){ + var $this = $(this), data = $this.data('SimpleList'); + $(window).unbind('SimpleList'); + data.SimpleList.remove(); + $this.removeData('SimpleList'); + }) }, -/* - reposition : function( ) { // ... }, - show : function( ) { // ... }, - hide : function( ) { // ... }, -*/ - update : function( content ) { } - }; + update : function( content ) { } + }; - jQuery.fn.SimpleList = function( method ) { + $.fn.SimpleList = function( method ) { /* Method calling logic */ 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.SimpleList' ); + $.error( 'Method ' + method + ' does not exist on jQuery.SimpleList' ); } - }; /* Private methods */ - function update_list(e, rows) - { + function update_list(e, rows) { if (rows.length == 0) { e.data.instance.html('No result !'); return; @@ -99,8 +81,7 @@ return "
  • " + value + "
  • "; } } - - + function myslice_html_ul(data, key, value, is_cached) { var out = "
      "; for (var i = 0; i < data.length; i++) { @@ -108,30 +89,7 @@ //out += myslice_html_li(key, myslice_html_a(data[i][key], data[i][value], key), is_cached); } out += "
    "; - return out; } - - /* - function myslice_async_render_list(data, key, value, is_cached) { - // we suppose we only have one column, or we need more precisions - var col = []; - if (myslice_array_size(data[0]) == 1) { - for (var k in data[0]) { - key = k; - value = k; - } - } else { - for (var k in data[0]) { - if (k.substr(-4) == '_hrn') { - key = k; - } else { - value = k; - } - } - } - return myslice_html_ul(data, key, value, is_cached); - } - */ - + })( jQuery );