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