Safety checks
[ajatus.git] / js / jqcouch2.js
blob338091e2561731b7d0ce69a48ed4d2415a6f7cd5
1 /**
2 * jqCouchDB - jQuery plugin for couchdb connections
3 * @requires jQuery > v1.0.3
4 * @requires couchDB >= v0.7.0a5575
6 * http://protoblogr.net
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
13 * Version: 2.0.1
15 * Example usages:
18 # Database
20 var dbc = $.jqCouch.connection('db');
21 dbc.exists('database_name');
22 ---
23 if ($.jqCouch.connection('db').create('database_name').ok) {
24 alert("database created");
27 # Doc
29 var dc = $.jqCouch.connection('doc');
30 var rev = dc.get('database/document1')._rev;
31 ---
32 var doc = {_id:"0",a:1,b:1};
33 if ($.jqCouch.connection('doc').save('database_name', doc)._id !== false) {
34 alert("Created document with rev: "+doc._rev+", a="+doc.a);
36 ---
37 //Get all documents from database. (With cache)
38 var dc = $.jqCouch.connection('doc');
39 dc.update_config('cache', true);
40 if (var total_documents = dc.all('database_name').total_rows) {
41 var all_documents = dc.all('database_name').rows;
43 //Get all documents from database. (Without cache)
44 var dc = $.jqCouch.connection('doc');
45 var all = dc.all('database_name');
46 if (all.total_rows > 0) {
47 var all_documents = all.rows;
50 # View
52 var vc = $.jqCouch.connection('view');
53 if (vc.exists('database_name', 'event') !== false) {
54 alert("View "event" exists");
56 ---
57 if ($.jqCouch.connection('view').exists('database_name', 'event/all') !== false) {
58 alert("View "event/all" exists");
61 More examples can be found from the testsuite
65 /**
66 * @private
67 * Generates new connection instance
68 * @param {String} type Connection type to initiate
69 * @param {Mixed}(String/Function) map_fun Global result mapping function (Optional)
70 * @param {Object} config Global configuration for connection (Optional)
71 * @returns Connection handler
72 * @type Function
74 function jqCouch_connection(type, map_fun, config) {
75 var default_config = {
76 cache: false
79 this.id = jQuery.jqCouch._generate_id();
80 this.type = type;
81 this.map_fun = map_fun || null;
82 this.inited = false;
83 this.config = jQuery.extend({}, default_config, config);
85 var handler = false;
87 this.destroy = function() {
88 this.id = null;
91 if (typeof eval('jqCouch_'+this.type) == 'function') {
92 eval('var fn = eval(jqCouch_'+this.type+'); handler = fn.apply(fn, [this.map_fun, config, this]);');
95 handler.last_error = typeof handler.last_error == 'undefined' ? {} : handler.last_error;
96 handler.update_config = function(key, value) {
97 this.config[key] = value;
98 return true;
100 handler.last_results = typeof handler.last_results == 'undefined' ? {} : handler.last_results;
101 handler.purge_cache = function() {
102 handler.last_results = {};
103 return true;
106 return handler;
110 * @private
111 * Handles DB actions
112 * @param {Mixed}(String/Function) map_fun Global result mapping function (Optional)
113 * @param {Object} config Global configuration for connection (Optional)
114 * @param {Function} conn Connection constructor instance
115 * @see jqCouch_connection
116 * @returns Connection handler
117 * @type Function
119 function jqCouch_db(map_fun, config, conn) {
120 var default_config = {
121 path: ''
123 this.config = jQuery.extend({}, default_config, config);
124 conn.inited = true;
125 this.instance = conn;
126 var _self = this;
128 this.exists = function(path, mf) {
129 if (typeof path == 'undefined') {
130 var path = this.config.path;
133 if (this.info(path, mf).db_name) {
134 return true;
137 return false;
140 this.info = function(path, mf) {
141 if (typeof path == 'undefined') {
142 var path = this.config.path;
144 if (path.substr(path.length-1,1) != '/') {
145 path += '/';
148 var map_fn = mf || map_fun;
150 var results = false;
152 var lr_key = encodeURIComponent(path).toString();
153 if ( typeof this.last_results[lr_key] != 'undefined'
154 && this.config.cache)
156 return this.last_results[lr_key];
159 jQuery.ajax({
160 url: _self.config.server_url + path,
161 type: "GET",
162 global: _self.config.ajax_setup.global,
163 cache: _self.config.ajax_setup.cache,
164 async: false,
165 dataType: "json",
166 contentType: 'application/json',
167 error: function(req) {
168 results = jQuery.extend({
169 db_name: false
170 }, jqCouch_map_error(req));
171 _self.last_error = results;
172 return false;
174 success: function(data) {
175 results = jqCouch_map_results(data, map_fn);
176 return true;
180 if (this.config.cache) {
181 this.last_results[lr_key] = results;
184 return results;
187 this.create = function(path, mf) {
188 if (typeof path == 'undefined') {
189 var path = this.config.path;
191 if (path.substr(path.length-1,1) != '/') {
192 path += '/';
195 var map_fn = mf || map_fun;
197 var results = {
198 ok: false
201 jQuery.ajax({
202 url: _self.config.server_url + path,
203 type: "PUT",
204 global: _self.config.ajax_setup.global,
205 cache: _self.config.ajax_setup.cache,
206 async: false,
207 dataType: "json",
208 contentType: 'application/json',
209 error: function(req) {
210 results = jQuery.extend({
211 ok: false
212 }, jqCouch_map_error(req));
213 _self.last_error = results;
214 return false;
216 success: function(data) {
217 results = jqCouch_map_results(data, map_fn);
218 return true;
222 return results;
225 this.del = function(path, mf) {
226 if (typeof path == 'undefined') {
227 var path = this.config.path;
229 if (path.substr(path.length-1,1) != '/') {
230 path += '/';
233 var map_fn = mf || map_fun;
235 var results = {
236 ok: false
239 jQuery.ajax({
240 url: _self.config.server_url + path,
241 type: "DELETE",
242 global: _self.config.ajax_setup.global,
243 cache: _self.config.ajax_setup.cache,
244 async: false,
245 dataType: "json",
246 contentType: 'application/json',
247 error: function(req) {
248 results = jQuery.extend({
249 ok: false
250 }, jqCouch_map_error(req));
251 _self.last_error = results;
252 return false;
254 success: function(data) {
255 results = jqCouch_map_results(data, map_fn);
256 return true;
260 return results;
263 this.all = function(mf) {
264 var results = {
265 length: 0
268 var map_fn = mf || map_fun;
270 var lr_key = encodeURIComponent(_self.config.server_url + '_all_dbs').toString();
271 if ( typeof this.last_results[lr_key] != 'undefined'
272 && this.config.cache)
274 return this.last_results[lr_key];
277 jQuery.ajax({
278 url: _self.config.server_url + '_all_dbs',
279 type: "GET",
280 global: _self.config.ajax_setup.global,
281 cache: _self.config.ajax_setup.cache,
282 async: false,
283 dataType: "json",
284 contentType: 'application/json',
285 error: function(req) {
286 results = jqCouch_map_error(req);
287 _self.last_error = results;
288 return false;
290 success: function(data) {
291 results = jqCouch_map_results(data, map_fn);
292 return true;
296 if (this.config.cache) {
297 this.last_results[lr_key] = results;
300 return results;
303 this.restart = function(mf) {
304 var result = {
305 ok: false
308 var map_fn = mf || map_fun;
310 jQuery.ajax({
311 url: _self.config.server_url + '_restart',
312 type: "POST",
313 global: _self.config.ajax_setup.global,
314 async: false,
315 error: function(req) {
316 results = jQuery.extend({
317 ok: false
318 }, jqCouch_map_error(req));
319 _self.last_error = results;
320 return false;
322 success: function(data) {
323 results = jqCouch_map_results(data, map_fn);
324 return true;
328 return results;
331 return this;
335 * @private
336 * Handles Doc actions
337 * @param {Mixed}(String/Function) map_fun Global result mapping function (Optional)
338 * @param {Object} config Global configuration for connection (Optional)
339 * @param {Function} conn Connection constructor instance
340 * @see jqCouch_connection
341 * @returns Connection handler
342 * @type Function
344 function jqCouch_doc(map_fun, config, conn) {
345 var default_config = {
346 path: ''
348 this.config = jQuery.extend({}, default_config, config);
349 conn.inited = true;
350 this.instance = conn;
351 var _self = this;
353 this.get = function(path, args, mf) {
354 if ( typeof path == 'undefined'
355 || path == '')
357 var path = this.config.path;
360 var map_fn = mf || map_fun;
362 var results = {
363 _id: false
366 path += jQuery.jqCouch._generate_query_str(args);
368 var lr_key = encodeURIComponent(path).toString();
369 if ( typeof this.last_results[lr_key] != 'undefined'
370 && this.config.cache)
372 return this.last_results[lr_key];
375 jQuery.ajax({
376 url: _self.config.server_url + path,
377 type: "GET",
378 global: _self.config.ajax_setup.global,
379 cache: _self.config.ajax_setup.cache,
380 async: false,
381 dataType: "json",
382 contentType: 'application/json',
383 error: function(req) {
384 results = jQuery.extend({
385 _id: false
386 }, jqCouch_map_error(req));
387 _self.last_error = results;
388 return false;
390 success: function(data) {
391 results = jqCouch_map_results(data, map_fn);
392 return true;
396 if (this.config.cache) {
397 this.last_results[lr_key] = results;
400 return results;
403 this.all = function(path, args, mf) {
404 if ( typeof path == 'undefined'
405 || path == '')
407 var path = this.config.path;
409 if (path.substr(path.length-1,1) != '/') {
410 path += '/';
412 path += '_all_docs';
413 path += jQuery.jqCouch._generate_query_str(args);
415 var map_fn = mf || map_fun;
417 var results = {
418 total_rows: 0
421 var lr_key = encodeURIComponent(path).toString();
422 if ( typeof this.last_results[lr_key] != 'undefined'
423 && this.config.cache)
425 return this.last_results[lr_key];
428 jQuery.ajax({
429 url: _self.config.server_url + path,
430 type: "GET",
431 global: _self.config.ajax_setup.global,
432 cache: _self.config.ajax_setup.cache,
433 async: false,
434 dataType: "json",
435 contentType: 'application/json',
436 error: function(req) {
437 results = jQuery.extend({
438 total_rows: 0
439 }, jqCouch_map_error(req));
440 _self.last_error = results;
441 return false;
443 success: function(data) {
444 results = jqCouch_map_results(data, map_fn);
445 return true;
449 if (this.config.cache) {
450 this.last_results[lr_key] = results;
453 return results;
456 this.bulk_save = function(path, data, args, mf) {
457 if ( typeof path == 'undefined'
458 || path == '')
460 var path = this.config.path;
462 if (path.substr(path.length-1,1) != '/') {
463 path += '/';
465 path += '_bulk_docs';
466 path += jQuery.jqCouch._generate_query_str(args);
468 var map_fn = mf || map_fun;
470 var results = {
471 ok: false,
472 results: null
475 jQuery.ajax({
476 url: _self.config.server_url + path,
477 type: "POST",
478 global: _self.config.ajax_setup.global,
479 cache: _self.config.ajax_setup.cache,
480 async: false,
481 dataType: "json",
482 contentType: 'application/json',
483 data: jQuery.jqCouch.toJSON(data),
484 processData: false,
485 error: function(req) {
486 results = jQuery.extend({
487 ok: false
488 }, jqCouch_map_error(req));
489 _self.last_error = results;
490 return false;
492 success: function(data) {
493 results = jqCouch_map_results(data, map_fn);
494 return true;
498 return results;
501 this.save = function(path, data, mf) {
502 if ( typeof path == 'undefined'
503 || path == '')
505 var path = this.config.path;
507 if (path.substr(path.length-1,1) != '/') {
508 path += '/';
511 var map_fn = mf || map_fun;
512 var results = false;
514 if ( typeof data._id == 'undefined'
515 || data._id == '')
517 results = this.post(path, data, mf);
518 } else {
519 results = this.put(path + data._id, data, map_fn);
522 if ( results
523 && results.rev)
525 data._id = results.id;
526 data._rev = results.rev;
529 return results;
532 this.post = function(path, data, mf) {
533 if ( typeof path == 'undefined'
534 || path == '')
536 var path = this.config.path;
539 var map_fn = mf || map_fun;
541 var results = {
542 _id: false
545 jQuery.ajax({
546 url: _self.config.server_url + path,
547 type: "POST",
548 global: _self.config.ajax_setup.global,
549 cache: _self.config.ajax_setup.cache,
550 async: false,
551 dataType: "json",
552 contentType: 'application/json',
553 data: jQuery.jqCouch.toJSON(data),
554 processData: false,
555 error: function(req) {
556 results = jQuery.extend({
557 _id: false
558 }, jqCouch_map_error(req));
559 _self.last_error = results;
560 return false;
562 success: function(data) {
563 results = jqCouch_map_results(data, map_fn);
564 return true;
568 return results;
571 this.put = function(path, data, mf) {
572 if ( typeof path == 'undefined'
573 || path == '')
575 var path = this.config.path;
578 var map_fn = mf || map_fun;
580 var results = {
581 _id: false
584 jQuery.ajax({
585 url: _self.config.server_url + path,
586 type: "PUT",
587 data: jQuery.jqCouch.toJSON(data),
588 processData: false,
589 global: _self.config.ajax_setup.global,
590 cache: _self.config.ajax_setup.cache,
591 async: false,
592 dataType: "json",
593 contentType: 'application/json',
594 error: function(req) {
595 results = jQuery.extend({
596 _id: false
597 }, jqCouch_map_error(req));
598 _self.last_error = results;
599 return false;
601 success: function(data) {
602 results = jqCouch_map_results(data, map_fn);
603 return true;
607 return results;
610 this.del = function(path, doc_or_rev, mf) {
611 if ( typeof path == 'undefined'
612 || path == '')
614 var path = this.config.path;
616 if ( typeof doc_or_rev == 'object'
617 && typeof doc_or_rev._id != 'undefined'
618 && typeof doc_or_rev._rev != 'undefined')
620 if (path.substr(path.length-1,1) != '/') {
621 path += '/';
623 path += doc_or_rev._id;
624 path += '?rev=' + doc_or_rev._rev;
626 if (typeof doc_or_rev == 'string') {
627 path += '?rev=' + doc_or_rev;
630 var map_fn = mf || map_fun;
632 var results = {
633 ok: false
636 jQuery.ajax({
637 url: _self.config.server_url + path,
638 type: "DELETE",
639 global: _self.config.ajax_setup.global,
640 cache: _self.config.ajax_setup.cache,
641 async: false,
642 dataType: "json",
643 contentType: 'application/json',
644 error: function(req) {
645 results = jQuery.extend({
646 ok: false
647 }, jqCouch_map_error(req));
648 _self.last_error = results;
649 return false;
651 success: function(data) {
652 results = jqCouch_map_results(data, map_fn);
653 return true;
657 if ( typeof doc_or_rev == 'object'
658 && results.ok)
660 doc_or_rev._rev = results.rev;
661 doc_or_rev._deleted = true;
664 return results;
667 return this;
671 * @private
672 * Handles View actions
673 * @param {Mixed}(String/Function) map_fun Global result mapping function (Optional)
674 * @param {Object} config Global configuration for connection (Optional)
675 * @param {Function} conn Connection constructor instance
676 * @see jqCouch_connection
677 * @returns Connection handler
678 * @type Function
680 function jqCouch_view(map_fun, config, conn) {
681 var default_config = {
682 path: '',
683 language: "text/javascript"
685 this.config = jQuery.extend({}, default_config, config);
686 conn.inited = true;
687 this.instance = conn;
688 var _self = this;
690 //TODO: add chance to create if doesnt exist
691 this.exists = function(path, view, mf) {
692 if (typeof path == 'undefined') {
693 var path = this.config.path;
696 if (typeof view == 'undefined') {
697 return false;
700 if (this.info(path, view, mf).views) {
701 return true;
704 return false;
707 this.info = function(path, view, mf) {
708 var results = {
709 _id: false,
710 views: false
712 if (typeof view == 'undefined') {
713 return results;
716 if (typeof path == 'undefined') {
717 var path = this.config.path;
719 if (path.substr(path.length-1,1) != '/') {
720 path += '/';
722 path += '_design/';
724 var view_parts = false;
725 if (view.toString().match(/\//)) {
726 view_parts = view.split("/");
727 path += view_parts[0];
728 } else {
729 path += view;
732 path += '?revs_info=true';
734 var map_fn = mf || map_fun;
736 var lr_key = encodeURIComponent(path).toString();
737 if ( typeof this.last_results[lr_key] != 'undefined'
738 && this.config.cache)
740 return this.last_results[lr_key];
743 jQuery.ajax({
744 url: _self.config.server_url + path,
745 type: "GET",
746 global: _self.config.ajax_setup.global,
747 cache: _self.config.ajax_setup.cache,
748 async: false,
749 dataType: "json",
750 contentType: 'application/json',
751 error: function(req) {
752 results = jQuery.extend({
753 _id: false,
754 views: false
755 }, jqCouch_map_error(req));
756 _self.last_error = results;
757 return false;
759 success: function(data) {
760 if ( view_parts
761 && typeof data.views[view_parts[1]] == 'undefined')
763 return false;
765 results = jqCouch_map_results(data, map_fn);
766 if (typeof results['views'] == 'undefined') {
767 results['views'] = false;
769 return true;
773 if (this.config.cache) {
774 this.last_results[lr_key] = results;
777 return results;
780 this.get = function(path, name, args, mf) {
781 var results = {
782 total_rows: false
785 if ( typeof path == 'undefined'
786 || path == '')
788 var path = this.config.path;
790 if (path.substr(path.length-1,1) != '/') {
791 path += '/';
793 if (! path.toString().match(/_view\//)) {
794 path += '_view/';
797 if (typeof name == 'undefined') {
798 return results;
801 if (! name.toString().match(/\//)) {
802 return results;
804 path += name;
806 var map_fn = mf || map_fun;
808 path += jQuery.jqCouch._generate_query_str(args);
810 var lr_key = encodeURIComponent(path).toString();
811 if ( typeof this.last_results[lr_key] != 'undefined'
812 && this.config.cache)
814 return this.last_results[lr_key];
817 jQuery.ajax({
818 url: _self.config.server_url + path,
819 type: "GET",
820 global: _self.config.ajax_setup.global,
821 cache: _self.config.ajax_setup.cache,
822 async: false,
823 dataType: "json",
824 contentType: 'application/json',
825 error: function(req) {
826 results = jQuery.extend({
827 total_rows: false
828 }, jqCouch_map_error(req));
829 _self.last_error = results;
830 return false;
832 success: function(data) {
833 results = jqCouch_map_results(data, map_fn);
834 return true;
838 if (this.config.cache) {
839 this.last_results[lr_key] = results;
842 return results;
845 this.save = function(path, data, mf) {
846 var results = {
847 ok: false
849 if (typeof data != 'object') {
850 return results;
853 if ( typeof path == 'undefined'
854 || path == '')
856 var path = this.config.path;
858 if (path.substr(path.length-1,1) != '/') {
859 path += '/';
862 if (typeof data._id == 'undefined') {
863 return results;
866 if (! data._id.toString().match(/_design/)) {
867 data._id = '_design/' + data._id;
870 if (typeof data['language'] == 'undefined') {
871 data.language = this.config.language;
874 for (var name in data.views) {
875 if (typeof(data.views[name]['toSource']) == 'function') {
876 data.views[name] = data.views[name].toSource();
877 } else {
878 data.views[name] = data.views[name].toString();
882 results = this.put(path + data._id, data, mf);
884 if ( results
885 && results.rev)
887 data._id = results.id;
888 data._rev = results.rev;
891 return results;
894 this.put = function(path, data, mf) {
895 if ( typeof path == 'undefined'
896 || path == '')
898 var path = this.config.path;
901 var map_fn = mf || map_fun;
903 var results = {
904 _id: false
907 jQuery.ajax({
908 url: _self.config.server_url + path,
909 type: "PUT",
910 data: jQuery.jqCouch.toJSON(data),
911 processData: false,
912 global: _self.config.ajax_setup.global,
913 cache: _self.config.ajax_setup.cache,
914 async: false,
915 dataType: "json",
916 contentType: 'application/json',
917 error: function(req) {
918 results = jQuery.extend({
919 _id: false
920 }, jqCouch_map_error(req));
921 _self.last_error = results;
922 return false;
924 success: function(data) {
925 results = jqCouch_map_results(data, map_fn);
926 return true;
930 return results;
933 this.del = function(path, doc_or_rev, mf) {
934 if ( typeof path == 'undefined'
935 || path == '')
937 var path = this.config.path;
939 if ( typeof doc_or_rev == 'object'
940 && typeof doc_or_rev._id != 'undefined'
941 && typeof doc_or_rev._rev != 'undefined')
943 if (path.substr(path.length-1,1) != '/') {
944 path += '/';
946 path += doc_or_rev._id;
947 path += '?rev=' + doc_or_rev._rev;
949 if (typeof doc_or_rev == 'string') {
950 path += '?rev=' + doc_or_rev;
953 var map_fn = mf || map_fun;
955 var results = {
956 ok: false
959 jQuery.ajax({
960 url: _self.config.server_url + path,
961 type: "DELETE",
962 global: _self.config.ajax_setup.global,
963 cache: _self.config.ajax_setup.cache,
964 async: false,
965 dataType: "json",
966 contentType: 'application/json',
967 error: function(req) {
968 results = jQuery.extend({
969 ok: false
970 }, jqCouch_map_error(req));
971 _self.last_error = results;
972 return false;
974 success: function(data) {
975 results = jqCouch_map_results(data, map_fn);
976 return true;
980 if ( typeof doc_or_rev == 'object'
981 && results.ok)
983 doc_or_rev._rev = results.rev;
984 doc_or_rev._deleted = true;
987 return results;
990 this.temp = function(path, map, args, mf) {
991 if ( typeof path == 'undefined'
992 || path == '')
994 var path = this.config.path;
996 if (path.substr(path.length-1,1) != '/') {
997 path += '/';
999 path += '_temp_view';
1000 path += jQuery.jqCouch._generate_query_str(args);
1002 var lr_key = encodeURIComponent(path).toString();
1003 if ( typeof this.last_results[lr_key] != 'undefined'
1004 && this.config.cache)
1006 return this.last_results[lr_key];
1009 var map_fn = mf || map_fun;
1011 if (typeof map == 'string') {
1012 map = eval(map);
1015 if (typeof map['toSource'] == 'function') {
1016 map = map.toSource();
1017 } else {
1018 map = map.toString();
1021 var results = {
1022 total_rows: false
1025 jQuery.ajax({
1026 url: _self.config.server_url + path,
1027 type: "POST",
1028 global: _self.config.ajax_setup.global,
1029 cache: _self.config.ajax_setup.cache,
1030 async: false,
1031 dataType: "json",
1032 data: map,
1033 contentType: 'application/javascript',
1034 processData: false,
1035 error: function(req) {
1036 results = jQuery.extend({
1037 total_rows: false
1038 }, jqCouch_map_error(req));
1039 _self.last_error = results;
1040 return false;
1042 success: function(data) {
1043 results = jqCouch_map_results(data, map_fn);
1044 return true;
1048 if (this.config.cache) {
1049 this.last_results[lr_key] = results;
1052 return results;
1055 return this;
1059 * @private
1060 * Default result wrapper
1061 * @param {Object} data Results from query
1062 * @param {Mixed}(String/Function) map result mapping function (Optional)
1063 * @see jqCouch_db, jqCouch_doc, jqCouch_view
1064 * @returns Finished results
1065 * @type Object
1067 function jqCouch_map_results(data, map) {
1068 if (typeof map == 'undefined') {
1069 return data;
1072 if (typeof map == 'function') {
1073 var res = map.apply(map, [data]);
1074 return jQuery.extend({}, res || {});
1077 if (typeof map == 'string') {
1078 var fn = eval('('+map+')');
1079 var res = fn.apply(fn, [data]);
1080 return jQuery.extend({}, res || {});
1083 return (data || {});
1087 * @private
1088 * Default error wrapper
1089 * @param {Object} req XHR Request object
1090 * @see jqCouch_db, jqCouch_doc, jqCouch_view
1091 * @returns Rendered error
1092 * @type Object
1094 function jqCouch_map_error(req) {
1095 var results = {
1096 status: req.status,
1097 response: eval("(" + req.responseText + ")")
1099 return results;
1102 (function($){
1104 $.jqCouch = {
1105 _connection_types: [
1106 'db', 'doc', 'view'
1108 _default_config: {
1109 server_url: '/',
1110 database: null,
1111 cache: false,
1112 ajax_setup: {
1113 global: false,
1114 cache: true
1117 _connections: {},
1119 _generate_id: function() {
1120 var rk = Math.floor(Math.random()*4013);
1121 return (10016486 + (rk * 22423));
1124 _generate_query_str: function(qa) {
1125 var qs = '';
1126 if (typeof qa != 'undefined') {
1127 qs += '?';
1128 $.each(qa, function(k,v){
1129 qs += k + '=' + v + '&';
1131 qs = qs.substr(0, qs.length-1);
1134 return qs;
1137 parseJSON: function(json_str) {
1138 try {
1139 return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
1140 json_str.replace(/"(\\.|[^"\\])*"/g, ''))) &&
1141 eval('(' + json_str + ')');
1142 } catch(e) {
1143 return false;
1146 toJSON: function (item, item_type) {
1147 var m = {
1148 '\b': '\\b',
1149 '\t': '\\t',
1150 '\n': '\\n',
1151 '\f': '\\f',
1152 '\r': '\\r',
1153 '"' : '\\"',
1154 '\\': '\\\\'
1156 s = {
1157 arr: function (x) {
1158 var a = ['['], b, f, i, l = x.length, v;
1159 for (i = 0; i < l; i += 1) {
1160 v = x[i];
1161 v = conv(v);
1162 if (typeof v == 'string') {
1163 if (b) {
1164 a[a.length] = ',';
1166 a[a.length] = v;
1167 b = true;
1170 a[a.length] = ']';
1171 return a.join('');
1173 bool: function (x) {
1174 return String(x);
1176 nul: function (x) {
1177 return "null";
1179 num: function (x) {
1180 return isFinite(x) ? String(x) : 'null';
1182 obj: function (x) {
1183 if (x) {
1184 if (x instanceof Array) {
1185 return s.arr(x);
1187 var a = ['{'], b, f, i, v;
1188 for (i in x) {
1189 v = x[i];
1190 v = conv(v);
1191 if (typeof v == 'string') {
1192 if (b) {
1193 a[a.length] = ',';
1195 a.push(s.str(i), ':', v);
1196 b = true;
1199 a[a.length] = '}';
1200 return a.join('');
1202 return 'null';
1204 str: function (x) {
1205 if (/["\\\x00-\x1f]/.test(x)) {
1206 x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
1207 var c = m[b];
1208 if (c) {
1209 return c;
1211 c = b.charCodeAt();
1212 return '\\u00' +
1213 Math.floor(c / 16).toString(16) +
1214 (c % 16).toString(16);
1217 return '"' + x + '"';
1220 var conv = function (x) {
1221 var itemtype = typeof x;
1222 switch(itemtype) {
1223 case "array":
1224 return s.arr(x);
1225 break;
1226 case "object":
1227 return s.obj(x);
1228 break;
1229 case "string":
1230 return s.str(x);
1231 break;
1232 case "number":
1233 return s.num(x);
1234 break;
1235 case "null":
1236 return s.nul(x);
1237 break;
1238 case "boolean":
1239 return s.bool(x);
1240 break;
1244 var itemtype = item_type || typeof item;
1245 switch(itemtype) {
1246 case "array":
1247 return s.arr(item);
1248 break;
1249 case "object":
1250 return s.obj(item);
1251 break;
1252 case "string":
1253 return s.str(item);
1254 break;
1255 case "number":
1256 return s.num(item);
1257 break;
1258 case "null":
1259 return s.nul(item);
1260 break;
1261 case "boolean":
1262 return s.bool(item);
1263 break;
1264 default:
1265 throw("Unknown type for $.jqcouch.toJSON");
1266 break;
1271 * Defines globally used default settings for jqCouch
1272 * @param {Object} defaults Config dictionary
1273 * @see $.jqCouch._default_config
1275 set_defaults: function(defaults) {
1276 $.jqCouch._default_config = $.extend({}, $.jqCouch._default_config, defaults || {});
1280 * @public
1281 * Initiates new connection and returns it.
1282 * Possible parameter combinations:
1283 * type OR type, map_fun OR type, config OR type, map_fun, config
1284 * @param {String} type Type of connection to initiate
1285 * @param {Mixed}(String/Function) map_fun Global result mapping function
1286 * @param {Object} config Global configuration for connection
1287 * @see jqCouch_connection
1288 * @returns Connection handler
1289 * @type Function
1291 connection: function() {
1292 if (arguments.length <= 0) {
1293 return false;
1296 if (typeof $.jqCouch._connections != 'object') {
1297 $.jqCouch._connections = {};
1300 var type = 'doc';
1301 var map_fun = null;
1302 var user_config = {};
1304 if ($.inArray(arguments[0], $.jqCouch._connection_types) != -1) {
1305 type = arguments[0];
1306 } else {
1307 return false;
1310 if (arguments.length > 1) {
1311 if (typeof arguments[1] == 'object') {
1312 user_config = arguments[1];
1313 } else {
1314 map_fun = arguments[1];
1318 if ( arguments.length == 3
1319 && typeof arguments[2] == 'object')
1321 user_config = arguments[2];
1324 var config = $.extend({}, $.jqCouch._default_config, user_config);
1326 var connection = new jqCouch_connection(type, map_fun, config);
1327 if (! connection.instance.inited) {
1328 return false;
1331 $.jqCouch._connections[connection.id] = connection;
1333 return connection;
1336 destroy_connection: function(id) {
1337 if ( typeof $.jqCouch._connections[id] != 'undefined'
1338 || $.jqCouch._connections[id] != 'null')
1340 $.jqCouch._connections[id].destroy();
1341 $.jqCouch._connections[id] = null;
1343 $.jqCouch._connections = $.grep($.jqCouch._connections, function(n,i){
1344 if (n != null) {
1345 return true;
1351 })(jQuery);