use $
[unfold.git] / engine / static / js / manifold-pubsub.js
1 /*
2  * This file is included in tophat_render.php
3  */
4
5 /* getting random error messages with this... -- jordan
6    wait until query code is fixed
7 jQuery(document).ready(function() {
8     // ajax default settings
9     jQuery.ajaxSetup({
10         timeout: 3000,
11         error:function(x,e){
12             if('parsererror'==e) {
13                 alert('Sorry, we ran into a technical problem (parse error). Please try again...');
14             } else if('timeout'==e) {
15                 alert('Request timed out. Please try again...');
16             }
17             else if ( "status" in x ) {
18                 if(0 == x.status){
19                     alert('You are offline! Please check your network.');
20                 }else if (404 == x.status){
21                     alert('Sorry, we ran into a technical problem (404). Please try again...');
22                 }else if(500 == x.status){
23                     alert('Sorry, we ran into a technical problem (500). Please try again...');
24                 }
25             }
26             else {
27                 alert('Sorry, we ran into a technical problem (unknown error). Please try again...');
28             }
29         }
30     });
31 });
32 */
33
34 function get_value(value) {
35     //if(typeof(jQuery(value).attr('value'))!="undefined"){
36     if (/<span value=['"].*['"]>.*<\/span>/i.test(value)) {
37         return jQuery(value).attr('value');
38     } else {
39         return value;
40     }
41 }
42
43 /*
44 From: http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object
45     I want to note that the .clone() method in jQuery only clones DOM elements. In order to clone JavaScript objects, you would do:
46
47     // Shallow copy
48     var newObject = jQuery.extend({}, oldObject);
49
50     // Deep copy
51     var newObject = jQuery.extend(true, {}, oldObject);
52
53     More information can be found in the jQuery documentation <http://docs.jquery.com/Utilities/jQuery.extend>
54 */
55 function clone_object(obj) {
56     return jQuery.extend(true, {}, obj);
57 }
58
59 /* https://gist.github.com/661855 */
60 (function($) {
61
62   var o = $({});
63
64   $.subscribe = function( types, selector, data, fn) {
65     /* borrowed from jQuery */
66     if ( data == null && fn == null ) {
67         // ( types, fn )
68         fn = selector;
69         data = selector = undefined;
70     } else if ( fn == null ) {
71         if ( typeof selector === "string" ) {
72             // ( types, selector, fn )
73             fn = data;
74             data = undefined;
75         } else {
76             // ( types, data, fn )
77             fn = data;
78             data = selector;
79             selector = undefined;
80         }
81     }
82     /* </ugly> */
83
84     /* We use an indirection function that will clone the object passed in
85      * parameter to the subscribe callback 
86      * 
87      * FIXME currently we only clone query objects which are the only ones
88      * supported and editable, we might have the same issue with results but
89      * the page load time will be severely affected...
90      */
91     o.on.apply(o, [types, selector, data, function() { 
92         for(i = 1; i < arguments.length; i++) {
93             if ( arguments[i].constructor.name == 'Query' )
94                 arguments[i] = arguments[i].clone();
95         }
96         fn.apply(o, arguments);
97     }]);
98   };
99
100   $.unsubscribe = function() {
101     o.off.apply(o, arguments);
102   };
103
104   $.publish = function() {
105     o.trigger.apply(o, arguments);
106   };
107
108 }(jQuery));
109
110
111 function executeFunctionByName(functionName, context /*, args */) {
112   var args = Array.prototype.slice.call(arguments).splice(2);
113   var namespaces = functionName.split(".");
114   var func = namespaces.pop();
115   for(var i = 0; i < namespaces.length; i++) {
116     context = context[namespaces[i]];
117   }
118   return context[func].apply(this, args);
119 }