Updated to work better with new Additionals widget
[ajatus.git] / js / old_jqcouch.js
blob0ec1cdf97ed42fc70f1f99ed6efefa4a7b9e1ab0
1 /**
2  * jqCouchDB - jQuery plugin for couchdb connections
3  * @requires jQuery > v1.0.3
4  * @requires couchDB >= v0.7.0a5575
5  *
6  * http://protoblogr.net
7  *
8  * Copyright (c) 2007 Jerry Jalava (protoblogr.net)
9  * Dual licensed under the MIT and GPL licenses:
10  * http://www.opensource.org/licenses/mit-license.php
11  * http://www.gnu.org/licenses/gpl.html
12  *
13  * Version: 1.1.0
14  *
15  * 
16  * Example usages:
17  *
18 var db_exists = false;
19 var jqcouch_db = $.jqcouch.db;
20 var info = null;
21 jqcouch_db.on_error = function(request, caller) { db_exists = false; };
22 jqcouch_db.on_success = function(data, caller) { db_exists = data.exists; };
23 jqcouch_db.exists('/some_database');
25 if (! db_exists)
27     $.jqcouch.db.create('/some_database');
30 jqcouch_db.on_success = function(data, caller)
32     info = data;
34 jqcouch_db.info('/some_database');
36 ---
37  *
38  * You don't have to override the on_error and on_success methods if you don't wan't to...
39  *
41 $.jqcouch.doc.save('/some_database/note1', {
42     name: 'Test',
43     title: 'Test title'
44 });
45 $.jqcouch.doc.on_success = function(data, caller) {
46     alert(data.value);
47     $.jqcouch.doc.on_success = $.jqcouch.doc._original_on_success;
49 $.jqcouch.doc.get('/some_database/note1');
51  *
52  * You could also override the global function:
53  *
54 $.jqcouch.on_error = function(request,caller)
56     some_other_error_func(request, caller);
58 $.jqcouch.on_success = function(data,caller)
60     some_other_success_func(data, caller);
62  */
64 (function($){
66     $.jqcouch = $.jqcouch || {};
67     
68     $.jqcouch.parseJSON = function (json_str)
69     {
70         try
71         {
72             return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
73                     json_str.replace(/"(\\.|[^"\\])*"/g, ''))) &&
74                 eval('(' + json_str + ')');
75         }
76         catch (e)
77         {
78             return false;
79         }
80     }
81     $.jqcouch.toJSON = function (item,item_type)
82     {
83         var m = {
84                 '\b': '\\b',
85                 '\t': '\\t',
86                 '\n': '\\n',
87                 '\f': '\\f',
88                 '\r': '\\r',
89                 '"' : '\\"',
90                 '\\': '\\\\'
91             },
92             s = {
93                 arr: function (x) {
94                     var a = ['['], b, f, i, l = x.length, v;
95                     for (i = 0; i < l; i += 1) {
96                         v = x[i];
97                         v = conv(v);
98                         if (typeof v == 'string') {
99                             if (b) {
100                                 a[a.length] = ',';
101                             }
102                             a[a.length] = v;
103                             b = true;
104                         }
105                     }
106                     a[a.length] = ']';
107                     return a.join('');
108                 },
109                 bool: function (x) {
110                     return String(x);
111                 },
112                 nul: function (x) {
113                     return "null";
114                 },
115                 num: function (x) {
116                     return isFinite(x) ? String(x) : 'null';
117                 },
118                 obj: function (x) {
119                     if (x) {
120                         if (x instanceof Array) {
121                             return s.arr(x);
122                         }
123                         var a = ['{'], b, f, i, v;
124                         for (i in x) {
125                             v = x[i];
126                             v = conv(v);
127                             if (typeof v == 'string') {
128                                 if (b) {
129                                     a[a.length] = ',';
130                                 }
131                                 a.push(s.str(i), ':', v);
132                                 b = true;
133                             }
134                         }
135                         a[a.length] = '}';
136                         return a.join('');
137                     }
138                     return 'null';
139                 },
140                 str: function (x) {
141                     if (/["\\\x00-\x1f]/.test(x)) {
142                         x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
143                             var c = m[b];
144                             if (c) {
145                                 return c;
146                             }
147                             c = b.charCodeAt();
148                             return '\\u00' +
149                                 Math.floor(c / 16).toString(16) +
150                                 (c % 16).toString(16);
151                         });
152                     }
153                     return '"' + x + '"';
154                 }
155             };
156             conv = function (x) {
157                 var itemtype = typeof x;
158                 switch(itemtype) {
159                         case "array":
160                           return s.arr(x);
161                           break;
162                         case "object":
163                           return s.obj(x);
164                           break;
165                         case "string":
166                           return s.str(x);
167                           break;
168                         case "number":
169                           return s.num(x);
170                           break;
171                         case "null":
172                           return s.nul(x);
173                           break;
174                         case "boolean":
175                           return s.bool(x);
176                           break;
177                 }
178             }
180         var itemtype = item_type || typeof item;
181         switch(itemtype) {
182                 case "array":
183                   return s.arr(item);
184                   break;
185                 case "object":
186                   return s.obj(item);
187                   break;
188                 case "string":
189                   return s.str(item);
190                   break;
191                 case "number":
192                   return s.num(item);
193                   break;
194                 case "null":
195                   return s.nul(item);
196                   break;
197                 case "boolean":
198                   return s.bool(item);
199                   break;                                
200                 default:
201                   throw("Unknown type for $.jqcouch.toJSON");
202                 }
203     }
204     
205     $.jqcouch.on_error = function(request, caller)
206     {
207         var error = "Error occured while talking with server!\n";
208         error += "request.status: "+request.status+"\n";
209         
210         if (typeof caller != 'undefined')
211         {
212             error += "caller.method: "+caller.method+"\n";
213             error += "caller.action: "+caller.action+"\n";
214             if (caller.args != undefined)
215             {
216                 error += "caller.args: "+caller.args.toString()+"\n";                
217             }
218         }
220         var response = eval("(" + request.responseText + ")");
221         error += "response.error.reason: "+response.error.reason+"\n";
222     };
223     $.jqcouch.on_success = function(data, caller)
224     {
225     };
226     
227     $.jqcouch.generate_query_str = function(query_args)
228     {
229         var query_string = '';
230         if (typeof query_args != 'undefined')
231         {
232             query_string += '?';
233             $.each(query_args, function(key,value){
234                 query_string += key + '=' + value + '&';
235             });
236             query_string = query_string.substr(0,query_string.length-1);
237         }
239         return query_string;
240     };
241     
242     $.jqcouch.server_url = '/';
243     
244     $.jqcouch.ajax_setup = {
245         global: false,
246         cache: true
247     };
248     
249     $.jqcouch.db = {
250         exists: function(path)
251         {            
252             var exists = false;
253             var server_url = $.jqcouch.server_url;
254             $.ajax({
255                 url: server_url + '_all_dbs',
256                 type: "GET",
257                 global: $.jqcouch.ajax_setup.global,
258                 cache: $.jqcouch.ajax_setup.cache,
259                 async: false,
260                 dataType: "json",
261                 contentType: 'application/json',
262                 error: function(request){
263                     $.jqcouch.db.on_error(request, {
264                         method: 'jqcouch.db.exists',
265                         action: '_all_dbs',
266                         args: {path: path}
267                     });
268                     return false;
269                 },
270                 success: function(data){
271                     $.each(data, function(i,n){
272                         if (n == path)
273                         {
274                             exists = true;
275                         }
276                     });
277                     $.jqcouch.db.on_success({exists: exists}, {
278                         method: 'jqcouch.db.exists',
279                         action: '_all_dbs',
280                         args: {path: path}
281                     });
282                     return exists;
283                 }
284             });
285         },
286         get_all: function()
287         {            
288             var server_url = $.jqcouch.server_url;
289             $.ajax({
290                 url: server_url + '_all_dbs',
291                 type: "GET",
292                 global: $.jqcouch.ajax_setup.global,
293                 cache: $.jqcouch.ajax_setup.cache,
294                 async: false,
295                 dataType: "json",
296                 contentType: 'application/json',
297                 error: function(request){
298                     $.jqcouch.db.on_error(request, {
299                         method: 'jqcouch.db.get_all',
300                         action: '_all_dbs',
301                         args: {}
302                     });
303                     return false;
304                 },
305                 success: function(data){
306                     $.jqcouch.db.on_success(data, {
307                         method: 'jqcouch.db.get_all',
308                         action: '',
309                         args: {}
310                     });
311                     return true;
312                 }
313             });
314         },
315         create: function(path)
316         {
317             if (path.substr(path.length-1,1) != '/')
318             {
319                 path += '/';
320             }
321             
322             var server_url = $.jqcouch.server_url;
323             $.ajax({
324                 url: server_url + path,
325                 type: "PUT",
326                 global: $.jqcouch.ajax_setup.global,
327                 cache: $.jqcouch.ajax_setup.cache,
328                 async: false,
329                 dataType: "json",
330                 contentType: 'application/json',
331                 error: function(request){
332                     $.jqcouch.db.on_error(request, {
333                         method: 'jqcouch.db.create',
334                         action: '',
335                         args: {path: path}
336                     });
337                     return false;
338                 },
339                 success: function(data){
340                     $.jqcouch.db.on_success(data, {
341                         method: 'jqcouch.db.create',
342                         action: '',
343                         args: {path: path}
344                     });
345                     return data;
346                 }
347             });
348         },
349         remove: function(path)
350         {
351             if (path.substr(path.length-1,1) != '/')
352             {
353                 path += '/';
354             }
355             
356             var server_url = $.jqcouch.server_url;
357             $.ajax({
358                 url: server_url + path,
359                 type: "DELETE",
360                 global: $.jqcouch.ajax_setup.global,
361                 cache: $.jqcouch.ajax_setup.cache,
362                 async: false,
363                 dataType: "json",
364                 contentType: 'application/json',
365                 error: function(request){
366                     $.jqcouch.db.on_error(request, {
367                         method: 'jqcouch.db.remove',
368                         action: '',
369                         args: {path: path}
370                     });
371                     return false;
372                 },
373                 success: function(data){
374                     $.jqcouch.db.on_success(data, {
375                         method: 'jqcouch.db.remove',
376                         action: '',
377                         args: {path: path}
378                     });
379                     return data.ok;
380                 }
381             });
382         },
383         info: function(path)
384         {   
385             if (path.substr(path.length-1,1) != '/')
386             {
387                 path += '/';
388             }
389             
390             var server_url = $.jqcouch.server_url;
391             $.ajax({
392                 url: server_url + path,
393                 type: "GET",
394                 global: $.jqcouch.ajax_setup.global,
395                 cache: $.jqcouch.ajax_setup.cache,
396                 async: false,
397                 dataType: "json",
398                 contentType: 'application/json',
399                 error: function(request){
400                     $.jqcouch.db.on_error(request, {
401                         method: 'jqcouch.db.info',
402                         action: '',
403                         args: {path: path}
404                     });
405                     return false;
406                 },
407                 success: function(data){
408                     $.jqcouch.db.on_success(data, {
409                         method: 'jqcouch.db.info',
410                         action: '',
411                         args: {path: path}
412                     });
413                     return true;
414                 }
415             });
416         },
417         restart: function()
418         {
419             var server_url = $.jqcouch.server_url;
420             
421             $.ajax({
422                 url: server_url + '_restart',
423                 type: "POST",
424                 global: $.jqcouch.ajax_setup.global,
425                 async: false,
426                 error: function(request){
427                     $.jqcouch.db.on_error(request, {
428                         method: 'jqcouch.db.restart',
429                         action: '',
430                         args: {}
431                     });
432                     return false;
433                 },
434                 success: function(data){
435                     $.jqcouch.db.on_success(data, {
436                         method: 'jqcouch.db.restart',
437                         action: '',
438                         args: {}
439                     });
440                     return true;
441                 }
442             });
443         },
444         
445         on_error: function(request, caller)
446         {
447             $.jqcouch.on_error(request, caller);            
448         },
449         on_success: function(data, caller)
450         {
451             $.jqcouch.on_success(data, caller);
452         },
453         _original_on_error: this.on_error,
454         _original_on_success: this.on_success
455     };
456     
457     $.jqcouch.doc = {
458         get: function(path, query_args)
459         {
460             path += $.jqcouch.generate_query_str(query_args);
461             
462             var server_url = $.jqcouch.server_url;
463             $.ajax({
464                 url: server_url + path,
465                 type: "GET",
466                 global: $.jqcouch.ajax_setup.global,
467                 cache: $.jqcouch.ajax_setup.cache,
468                 async: false,
469                 dataType: "json",
470                 contentType: 'application/json',
471                 error: function(request){
472                     $.jqcouch.doc.on_error(request, {
473                         method: 'jqcouch.doc.get',
474                         action: '',
475                         args: {path: path}
476                     });
477                     return false;
478                 },
479                 success: function(data){
480                     $.jqcouch.doc.on_success(data, {
481                         method: 'jqcouch.doc.get',
482                         action: '',
483                         args: {path: path}
484                     });
485                     return true;
486                 }
487             });
488         },
489         get_all: function(path, query_args)
490         {
491             if (path.substr(path.length-1,1) != '/')
492             {
493                 path += '/';
494             }
495             path += '_all_docs';            
496             path += $.jqcouch.generate_query_str(query_args);
497             
498             var server_url = $.jqcouch.server_url;
499             $.ajax({
500                 url: server_url + path,
501                 type: "GET",
502                 global: $.jqcouch.ajax_setup.global,
503                 cache: $.jqcouch.ajax_setup.cache,
504                 async: false,
505                 dataType: "json",
506                 contentType: 'application/json',
507                 error: function(request){
508                     $.jqcouch.doc.on_error(request, {
509                         method: 'jqcouch.doc.get_all',
510                         action: '_all_docs',
511                         args: {query_args: query_args}
512                     });
513                     return false;
514                 },
515                 success: function(data){
516                     $.jqcouch.doc.on_success(data, {
517                         method: 'jqcouch.doc.get_all',
518                         action: '_all_docs',
519                         args: {query_args: query_args}
520                     });
521                     return true;
522                 }
523             });
524         },
525         save: function(path, data)
526         {            
527             if (path.substr(path.length-1,1) != '/')
528             {
529                 path += '/';
530             }
531             
532             if (   data._id == undefined
533                 || data._id == '')
534             {
535                 $.jqcouch.doc.post(path, data);
536             }
537             else
538             {
539                 $.jqcouch.doc.put(path + data._id, data);
540             }
541         },
542         put: function(path, data)
543         {            
544             var server_url = $.jqcouch.server_url;
545             $.ajax({
546                 url: server_url + path,
547                 type: "PUT",
548                 data: $.jqcouch.toJSON(data),
549                 processData: false,
550                 global: $.jqcouch.ajax_setup.global,
551                 cache: $.jqcouch.ajax_setup.cache,
552                 async: false,
553                 dataType: "json",
554                 contentType: 'application/json',
555                 error: function(request){
556                     $.jqcouch.doc.on_error(request, {
557                         method: 'jqcouch.doc.put',
558                         action: '',
559                         args: {path: path}
560                     });
561                     return false;
562                 },
563                 success: function(data){
564                     $.jqcouch.doc.on_success(data, {
565                         method: 'jqcouch.doc.put',
566                         action: '',
567                         args: {path: path}
568                     });
569                     return true;
570                 }
571             });
572         },
573         post: function(path, data)
574         {            
575             var server_url = $.jqcouch.server_url;
576             $.ajax({
577                 url: server_url + path,
578                 type: "POST",
579                 global: $.jqcouch.ajax_setup.global,
580                 cache: $.jqcouch.ajax_setup.cache,
581                 async: false,
582                 dataType: "json",
583                 contentType: 'application/json',
584                 data: $.jqcouch.toJSON(data),
585                 processData: false,
586                 error: function(request){
587                     $.jqcouch.doc.on_error(request, {
588                         method: 'jqcouch.doc.post',
589                         action: '',
590                         args: {path: path, data: data}
591                     });
592                     return false;
593                 },
594                 success: function(data){
595                     $.jqcouch.doc.on_success(data, {
596                         method: 'jqcouch.doc.post',
597                         action: '',
598                         args: {path: path, data: data}
599                     });
600                     return true;
601                 }
602             });
603         },
604         bulk_save: function(path, data, query_args)
605         {
606             if (path.substr(path.length-1,1) != '/')
607             {
608                 path += '/';
609             }            
610             path += '_bulk_docs';
611             path += $.jqcouch.generate_query_str(query_args);
613             var server_url = $.jqcouch.server_url;                        
614             $.ajax({
615                 url: server_url + path,
616                 type: "POST",
617                 global: $.jqcouch.ajax_setup.global,
618                 cache: $.jqcouch.ajax_setup.cache,
619                 async: false,
620                 dataType: "json",
621                 contentType: 'application/json',
622                 data: $.jqcouch.toJSON(data),
623                 processData: false,
624                 error: function(request){
625                     $.jqcouch.doc.on_error(request, {
626                         method: 'jqcouch.doc.bulk_save',
627                         action: '',
628                         args: {path: path, data: data}
629                     });
630                     return false;
631                 },
632                 success: function(data){
633                     $.jqcouch.doc.on_success(data, {
634                         method: 'jqcouch.doc.bulk_save',
635                         action: '',
636                         args: {path: path, data: data}
637                     });
638                     return true;
639                 }
640             });            
641         },
642         del: function(path, revision)
643         {
644             if (typeof revision != 'undefined')
645             {
646                 path += '?rev=' + revision;                
647             }
648             
649             var server_url = $.jqcouch.server_url;
650             $.ajax({
651                 url: server_url + path,
652                 type: "DELETE",
653                 global: $.jqcouch.ajax_setup.global,
654                 cache: $.jqcouch.ajax_setup.cache,
655                 async: false,
656                 dataType: "json",
657                 contentType: 'application/json',
658                 error: function(request){
659                     $.jqcouch.doc.on_error(request, {
660                         method: 'jqcouch.doc.del',
661                         action: '',
662                         args: {path: path}
663                     });
664                     return false;
665                 },
666                 success: function(data){
667                     $.jqcouch.doc.on_success(data, {
668                         method: 'jqcouch.doc.del',
669                         action: '',
670                         args: {path: path}
671                     });
672                     return true;
673                 }
674             });
675         },
676         
677         on_error: function(request, caller)
678         {
679             $.jqcouch.on_error(request, caller);            
680         },
681         on_success: function(data, caller)
682         {
683             $.jqcouch.on_success(data, caller);
684         },
685         _original_on_error: this.on_error,
686         _original_on_success: this.on_success
687     };
688     
689     $.jqcouch.view = {
690         exists: function(path, name)
691         {
692             var exists = false;
693             var rev = null;
694             
695             if (path.substr(path.length-1,1) != '/')
696             {
697                 path += '/';
698             }
699             path += '_all_docs';
700             
701             var server_url = $.jqcouch.server_url;
702             $.ajax({
703                 url: server_url + path,
704                 type: "GET",
705                 global: $.jqcouch.ajax_setup.global,
706                 cache: $.jqcouch.ajax_setup.cache,
707                 async: false,
708                 dataType: "json",
709                 contentType: 'application/json',
710                 error: function(request){
711                     $.jqcouch.view.on_error(request, {
712                         method: 'jqcouch.view.exists',
713                         action: '_all_docs',
714                         args: {path: path}
715                     });
716                     return false;
717                 },
718                 success: function(data){
719                     $.each(data.rows, function(i,n){
720                         if (n.id == name)
721                         {
722                             exists = true;
723                             rev = n.value.rev;
724                         }
725                     });
726                     $.jqcouch.view.on_success({exists: exists, rev: rev}, {
727                         method: 'jqcouch.view.exists',
728                         action: '_all_docs',
729                         args: {path: path}
730                     });
731                     return exists;
732                 }
733             });
734         },
735         get: function(path, query_args)
736         {
737             $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
738             $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
739             $.jqcouch.doc.get(path, query_args);
740         },
741         save: function(path, data)
742         {            
743             if (   data._id == undefined
744                 || data._id == '')
745             {
746                 return false;
747             }
749             if (path.substr(path.length-1,1) != '/') {
750                 path += '/';
751             }
752             
753             $.jqcouch.view.put(path + data._id, data);
754         },
755         put: function(path, data)
756         {
757             $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
758             $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
759             $.jqcouch.doc.put(path, data);
760         },
761         del: function(path)
762         {
763             $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
764             $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
765             $.jqcouch.doc.del(path);
766         },
767         temp: function(path, query_args, map)
768         {            
769             if (path.substr(path.length-1,1) != '/')
770             {
771                 path += '/';
772             }            
773             path += '_temp_view';            
774             path += $.jqcouch.generate_query_str(query_args);
775             
776             var server_url = $.jqcouch.server_url;
777             
778             if (typeof(map) == 'string') {
779                 map = eval(map);
780             }
781             
782             if (typeof(map['toSource']) == 'function') {
783                 var map = map.toSource();
784             } else {
785                 var map = map;
786             }
788             $.ajax({
789                 url: server_url + path,
790                 type: "POST",
791                 global: $.jqcouch.ajax_setup.global,
792                 cache: $.jqcouch.ajax_setup.cache,
793                 async: false,
794                 dataType: "json",
795                 data: map,
796                 contentType: 'application/javascript',
797                 processData: false,
798                 error: function(request){
799                     $.jqcouch.view.on_error(request, {
800                         method: 'jqcouch.view.temp',
801                         action: '_temp_view',
802                         args: {query_args: query_args}
803                     });
804                     return false;
805                 },
806                 success: function(data){
807                     $.jqcouch.view.on_success(data, {
808                         method: 'jqcouch.view.temp',
809                         action: '_temp_view',
810                         args: {query_args: query_args}
811                     });
812                     return true;
813                 }
814             });
815         },
816         
817         on_error: function(request, caller)
818         {
819             $.jqcouch.on_error(request, caller);            
820         },
821         on_success: function(data, caller)
822         {
823             $.jqcouch.on_success(data, caller);
824         },
825         _original_on_error: this.on_error,
826         _original_on_success: this.on_success
827     };
829 })(jQuery);