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