Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update vendor couchapp

  • Loading branch information...
commit 3b63dfa3d80b5e95d2d0c768c03231c854c27457 1 parent 6d012f0
Chris Anderson jchris authored
9 _attachments/index.html
@@ -6,5 +6,14 @@
6 6 </head>
7 7 <body>
8 8 <p>Visit /pages/recent</p>
  9 + <script src="script/myloader.js"></script>
  10 + <script src="/_utils/script/base64.js"></script>
  11 + <script src="/_utils/script/jquery.form.js"></script>
  12 + <script type="text/javascript" charset="utf-8">
  13 + $.couch.db("pages").info();
  14 + $(function() {
  15 + $.couch.db("a").info();
  16 + });
  17 + </script>
9 18 </body>
10 19 </html>
333 vendor/couchapp/_attachments/jquery.couch.app.js
@@ -19,7 +19,7 @@
19 19 // });
20 20
21 21 (function($) {
22   -
  22 +
23 23 function Design(db, name) {
24 24 this.doc_id = "_design/"+name;
25 25 this.view = function(view, opts) {
@@ -30,229 +30,122 @@
30 30 };
31 31 }
32 32
33   - $.couch.app = $.couch.app || function(appFun, opts) {
34   - opts = opts || {};
35   - $(function() {
36   - var urlPrefix = opts.urlPrefix || "";
37   - $.couch.urlPrefix = urlPrefix;
38   -
39   - var index = urlPrefix.split('/').length;
40   - var fragments = unescape(document.location.href).split('/');
41   - var dbname = opts.db || fragments[index + 2];
42   - var dname = opts.design || fragments[index + 4];
43   -
44   - var db = $.couch.db(dbname);
45   - var design = new Design(db, dname);
46   -
47   - // docForm applies CouchDB behavior to HTML forms.
48   - // todo make this a couch.app plugin
49   - function docForm(formSelector, opts) {
50   - var localFormDoc = {};
51   - opts = opts || {};
52   - opts.fields = opts.fields || [];
  33 + function docForm() { alert("docForm has been moved to vendor/couchapp/lib/docForm.js, use app.require to load") };
53 34
54   - // turn the form into deep json
55   - // field names like 'author-email' get turned into json like
56   - // {"author":{"email":"quentin@example.com"}}
57   - function formToDeepJSON(form, fields, doc) {
58   - form = $(form);
59   - fields.forEach(function(field) {
60   - var element = form.find("[name="+field+"]");
61   - if (element.attr('type') === 'checkbox') {
62   - var val = element.attr('checked');
63   - } else {
64   - var val = element.val();
65   - if (!val) return;
66   - }
67   - var parts = field.split('-');
68   - var frontObj = doc, frontName = parts.shift();
69   - while (parts.length > 0) {
70   - frontObj[frontName] = frontObj[frontName] || {};
71   - frontObj = frontObj[frontName];
72   - frontName = parts.shift();
73   - }
74   - frontObj[frontName] = val;
75   - });
76   - }
77   -
78   - // Apply the behavior
79   - $(formSelector).submit(function(e) {
80   - e.preventDefault();
81   - if (opts.validate && opts.validate() == false) { return false;}
82   - // formToDeepJSON acts on localFormDoc by reference
83   - formToDeepJSON(this, opts.fields, localFormDoc);
84   - if (opts.beforeSave) {opts.beforeSave(localFormDoc);}
85   - db.saveDoc(localFormDoc, {
86   - success : function(resp) {
87   - if (opts.success) {opts.success(resp, localFormDoc);}
88   - }
89   - });
90   -
91   - return false;
92   - });
  35 + function resolveModule(names, parent, current) {
  36 + if (names.length === 0) {
  37 + if (typeof current != "string") {
  38 + throw ["error","invalid_require_path",
  39 + 'Must require a JavaScript string, not: '+(typeof current)];
  40 + }
  41 + return [current, parent];
  42 + }
  43 + var n = names.shift();
  44 + if (n == '..') {
  45 + if (!(parent && parent.parent)) {
  46 + throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
  47 + }
  48 + return resolveModule(names, parent.parent.parent, parent.parent);
  49 + } else if (n == '.') {
  50 + if (!parent) {
  51 + throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
  52 + }
  53 + return resolveModule(names, parent.parent, parent);
  54 + }
  55 + if (!current[n]) {
  56 + throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
  57 + }
  58 + var p = current;
  59 + current = current[n];
  60 + current.parent = p;
  61 + return resolveModule(names, p, current);
  62 + }
93 63
94   - // populate form from an existing doc
95   - function docToForm(doc) {
96   - var form = $(formSelector);
97   - // fills in forms
98   - opts.fields.forEach(function(field) {
99   - var parts = field.split('-');
100   - var run = true, frontObj = doc, frontName = parts.shift();
101   - while (frontObj && parts.length > 0) {
102   - frontObj = frontObj[frontName];
103   - frontName = parts.shift();
104   - }
105   - if (frontObj && frontObj[frontName]) {
106   - var element = form.find("[name="+field+"]");
107   - if (element.attr('type') === 'checkbox') {
108   - element.attr('checked', frontObj[frontName]);
109   - } else {
110   - element.val(frontObj[frontName]);
111   - }
112   - }
113   - });
  64 + function makeRequire(ddoc) {
  65 + var moduleCache = [];
  66 + function getCachedModule(name, parent) {
  67 + var key, i, len = moduleCache.length;
  68 + for (i=0;i<len;++i) {
  69 + key = moduleCache[i].key;
  70 + if (key[0] === name && key[1] === parent) {
  71 + return moduleCache[i].module;
114 72 }
115   -
116   - if (opts.id) {
117   - db.openDoc(opts.id, {
118   - attachPrevRev : opts.attachPrevRev,
119   - error: function() {
120   - if (opts.error) {opts.error.apply(opts, arguments);}
121   - },
122   - success: function(doc) {
123   - if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
124   - localFormDoc = doc;
125   - docToForm(doc);
126   - }});
127   - } else if (opts.template) {
128   - if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
129   - localFormDoc = opts.template;
130   - docToForm(localFormDoc);
131   - }
132   - var instance = {
133   - deleteDoc : function(opts) {
134   - opts = opts || {};
135   - if (confirm("Really delete this document?")) {
136   - db.removeDoc(localFormDoc, opts);
137   - }
138   - },
139   - localDoc : function() {
140   - formToDeepJSON(formSelector, opts.fields, localFormDoc);
141   - return localFormDoc;
142   - }
143   - };
144   - return instance;
145 73 }
146   -
147   - function resolveModule(names, parent, current) {
148   - if (names.length === 0) {
149   - if (typeof current != "string") {
150   - throw ["error","invalid_require_path",
151   - 'Must require a JavaScript string, not: '+(typeof current)];
152   - }
153   - return [current, parent];
154   - }
155   - // we need to traverse the path
156   - var n = names.shift();
157   - if (n == '..') {
158   - if (!(parent && parent.parent)) {
159   - throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
160   - }
161   - return resolveModule(names, parent.parent.parent, parent.parent);
162   - } else if (n == '.') {
163   - if (!parent) {
164   - throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
165   - }
166   - return resolveModule(names, parent.parent, parent);
167   - }
168   - if (!current[n]) {
169   - throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
170   - }
171   - var p = current;
172   - current = current[n];
173   - current.parent = p;
174   - return resolveModule(names, p, current);
  74 + return null;
  75 + }
  76 + function setCachedModule(name, parent, module) {
  77 + moduleCache.push({ key: [name, parent], module: module });
  78 + }
  79 + var require = function (name, parent) {
  80 + var cachedModule = getCachedModule(name, parent);
  81 + if (cachedModule !== null) {
  82 + return cachedModule;
175 83 }
176   -
177   - var p = document.location.pathname.split('/');
178   - p.shift();
179   - var qs = document.location.search.replace(/^\?/,'').split('&');
180   - var q = {};
181   - qs.forEach(function(param) {
182   - var ps = param.split('=');
183   - var k = decodeURIComponent(ps[0]);
184   - var v = decodeURIComponent(ps[1]);
185   - if (["startkey", "endkey", "key"].indexOf(k) != -1) {
186   - q[k] = JSON.parse(v);
187   - } else {
188   - q[k] = v;
189   - }
190   - });
191   - var mockReq = {
192   - path : p,
193   - query : q
194   - };
195   -
196   - var appExports = $.extend({
197   - db : db,
198   - design : design,
199   - view : design.view,
200   - list : design.list,
201   - docForm : docForm,
202   - req : mockReq
203   - }, $.couch.app.app);
  84 + var exports = {};
  85 + var resolved = resolveModule(name.split('/'), parent, ddoc);
  86 + var source = resolved[0];
  87 + parent = resolved[1];
  88 + var s = "var func = function (exports, require) { " + source + " };";
  89 + try {
  90 + eval(s);
  91 + func.apply(ddoc, [exports, function(name) {return require(name, parent, source)}]);
  92 + } catch(e) {
  93 + throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
  94 + }
  95 + setCachedModule(name, parent, exports);
  96 + return exports;
  97 + }
  98 + return require;
  99 + };
204 100
205   - function handleDDoc(ddoc) {
206   - var moduleCache = [];
207   -
208   - function getCachedModule(name, parent) {
209   - var key, i, len = moduleCache.length;
210   - for (i=0;i<len;++i) {
211   - key = moduleCache[i].key;
212   - if (key[0] === name && key[1] === parent) {
213   - return moduleCache[i].module;
214   - }
215   - }
216   -
217   - return null;
218   - }
219   -
220   - function setCachedModule(name, parent, module) {
221   - moduleCache.push({ key: [name, parent], module: module });
222   - }
223   -
224   - if (ddoc) {
225   - var require = function(name, parent) {
226   - var cachedModule = getCachedModule(name, parent);
227   - if (cachedModule !== null) {
228   - return cachedModule;
229   - }
230   -
231   - var exports = {};
232   - var resolved = resolveModule(name.split('/'), parent, ddoc);
233   - var source = resolved[0];
234   - parent = resolved[1];
235   - var s = "var func = function (exports, require) { " + source + " };";
236   - try {
237   - eval(s);
238   - func.apply(ddoc, [exports, function(name) {return require(name, parent, source)}]);
239   - } catch(e) {
240   - throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
241   - }
242   -
243   - setCachedModule(name, parent, exports);
244   -
245   - return exports;
246   - }
247   - appExports.ddoc = ddoc;
248   - appExports.require = require;
249   - }
250   - // todo make app-exports the this in the execution context?
251   - appFun.apply(appExports, [appExports]);
  101 + function mockReq() {
  102 + var p = document.location.pathname.split('/'),
  103 + qs = document.location.search.replace(/^\?/,'').split('&'),
  104 + q = {};
  105 + qs.forEach(function(param) {
  106 + var ps = param.split('='),
  107 + k = decodeURIComponent(ps[0]),
  108 + v = decodeURIComponent(ps[1]);
  109 + if (["startkey", "endkey", "key"].indexOf(k) != -1) {
  110 + q[k] = JSON.parse(v);
  111 + } else {
  112 + q[k] = v;
252 113 }
253   -
  114 + });
  115 + p.shift();
  116 + return {
  117 + path : p,
  118 + query : q
  119 + };
  120 + };
  121 +
  122 + $.couch.app = $.couch.app || function(appFun, opts) {
  123 + opts = opts || {};
  124 + var urlPrefix = (opts.urlPrefix || ""),
  125 + index = urlPrefix.split('/').length,
  126 + fragments = unescape(document.location.href).split('/'),
  127 + dbname = opts.db || fragments[index + 2],
  128 + dname = opts.design || fragments[index + 4];
  129 + $.couch.urlPrefix = urlPrefix;
  130 + var db = $.couch.db(dbname),
  131 + design = new Design(db, dname);
  132 + var appExports = $.extend({
  133 + db : db,
  134 + design : design,
  135 + view : design.view,
  136 + list : design.list,
  137 + docForm : docForm, // deprecated
  138 + req : mockReq()
  139 + }, $.couch.app.app);
  140 + function handleDDoc(ddoc) {
  141 + if (ddoc) {
  142 + appExports.ddoc = ddoc;
  143 + appExports.require = makeRequire(ddoc);
  144 + }
  145 + appFun.apply(appExports, [appExports]);
  146 + }
254 147 if ($.couch.app.ddocs[design.doc_id]) {
255   - handleDDoc($.couch.app.ddocs[design.doc_id])
  148 + $(function() {handleDDoc($.couch.app.ddocs[design.doc_id])});
256 149 } else {
257 150 // only open 1 connection for this ddoc
258 151 if ($.couch.app.ddoc_handlers[design.doc_id]) {
@@ -264,22 +157,20 @@
264 157 success : function(doc) {
265 158 $.couch.app.ddocs[design.doc_id] = doc;
266 159 $.couch.app.ddoc_handlers[design.doc_id].forEach(function(h) {
267   - h(doc);
  160 + $(function() {h(doc)});
268 161 });
269 162 $.couch.app.ddoc_handlers[design.doc_id] = null;
270 163 },
271 164 error : function() {
272 165 $.couch.app.ddoc_handlers[design.doc_id].forEach(function(h) {
273   - h();
  166 + $(function() {h()});
274 167 });
275 168 $.couch.app.ddoc_handlers[design.doc_id] = null;
276 169 }
277 170 });
278 171 }
279 172 }
280   -
281   - });
282   - };
  173 + };
283 174 $.couch.app.ddocs = {};
284 175 $.couch.app.ddoc_handlers = {};
285 176 // legacy support. $.CouchApp is deprecated, please use $.couch.app
126 vendor/couchapp/lib/docform.js
@@ -14,48 +14,49 @@
14 14 // field names like 'author-email' get turned into json like
15 15 // {"author":{"email":"quentin@example.com"}}
16 16 // acts on doc by reference, so you can safely pass non-form fields through
17   -function formToDeepJSON(form, fields, doc) {
18   - form = $(form);
19   - fields.forEach(function(field) {
20   - var val = form.find("[name="+field+"]").val();
21   - if (!val) {return;}
22   - var parts = field.split('-');
23   - var frontObj = doc, frontName = parts.shift();
24   - while (parts.length > 0) {
25   - frontObj[frontName] = frontObj[frontName] || {};
26   - frontObj = frontObj[frontName];
27   - frontName = parts.shift();
28   - }
29   - frontObj[frontName] = val;
30   - });
31   -}
32   -
33   -function onSubmit(form, db, doc, opts) {
34   - formToDeepJSON(form, opts.fields, doc);
35   - if (opts.beforeSave) {opts.beforeSave(doc);}
36   - db.saveDoc(localFormDoc, {
37   - success : function(resp) {
38   - if (opts.success) {opts.success(resp, doc);}
39   - }
40   - });
41   -};
42   -
43   -function applyFields(form, doc) {
44   -
45   -};
46   -exports.applyFields = applyFields;
47 17
48   -// docForm applies CouchDB behavior to HTML forms.
49   -// todo make this a couch.app plugin
50 18 function docForm(formSelector, opts) {
51 19 var localFormDoc = {};
52 20 opts = opts || {};
53 21 opts.fields = opts.fields || [];
54 22
  23 + // turn the form into deep json
  24 + // field names like 'author-email' get turned into json like
  25 + // {"author":{"email":"quentin@example.com"}}
  26 + function formToDeepJSON(form, fields, doc) {
  27 + form = $(form);
  28 + fields.forEach(function(field) {
  29 + var element = form.find("[name="+field+"]");
  30 + if (element.attr('type') === 'checkbox') {
  31 + var val = element.attr('checked');
  32 + } else {
  33 + var val = element.val();
  34 + if (!val) return;
  35 + }
  36 + var parts = field.split('-');
  37 + var frontObj = doc, frontName = parts.shift();
  38 + while (parts.length > 0) {
  39 + frontObj[frontName] = frontObj[frontName] || {};
  40 + frontObj = frontObj[frontName];
  41 + frontName = parts.shift();
  42 + }
  43 + frontObj[frontName] = val;
  44 + });
  45 + }
  46 +
55 47 // Apply the behavior
56 48 $(formSelector).submit(function(e) {
57   -
58   -
  49 + e.preventDefault();
  50 + if (opts.validate && opts.validate() == false) { return false;}
  51 + // formToDeepJSON acts on localFormDoc by reference
  52 + formToDeepJSON(this, opts.fields, localFormDoc);
  53 + if (opts.beforeSave) {opts.beforeSave(localFormDoc);}
  54 + db.saveDoc(localFormDoc, {
  55 + success : function(resp) {
  56 + if (opts.success) {opts.success(resp, localFormDoc);}
  57 + }
  58 + });
  59 +
59 60 return false;
60 61 });
61 62
@@ -71,38 +72,43 @@ function docForm(formSelector, opts) {
71 72 frontName = parts.shift();
72 73 }
73 74 if (frontObj && frontObj[frontName]) {
74   - form.find("[name="+field+"]").val(frontObj[frontName]);
  75 + var element = form.find("[name="+field+"]");
  76 + if (element.attr('type') === 'checkbox') {
  77 + element.attr('checked', frontObj[frontName]);
  78 + } else {
  79 + element.val(frontObj[frontName]);
  80 + }
75 81 }
76   - });
  82 + });
77 83 }
78   -
  84 +
79 85 if (opts.id) {
80 86 db.openDoc(opts.id, {
  87 + attachPrevRev : opts.attachPrevRev,
  88 + error: function() {
  89 + if (opts.error) {opts.error.apply(opts, arguments);}
  90 + },
81 91 success: function(doc) {
82   - if (opts.onLoad) {opts.onLoad(doc);}
  92 + if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
83 93 localFormDoc = doc;
84 94 docToForm(doc);
85   - }});
86   - } else if (opts.template) {
87   - if (opts.onLoad) {opts.onLoad(opts.template);}
88   - localFormDoc = opts.template;
89   - docToForm(localFormDoc);
  95 + }});
  96 + } else if (opts.template) {
  97 + if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
  98 + localFormDoc = opts.template;
  99 + docToForm(localFormDoc);
  100 + }
  101 + var instance = {
  102 + deleteDoc : function(opts) {
  103 + opts = opts || {};
  104 + if (confirm("Really delete this document?")) {
  105 + db.removeDoc(localFormDoc, opts);
90 106 }
91   - var instance = {
92   - deleteDoc : function(opts) {
93   - opts = opts || {};
94   - if (confirm("Really delete this document?")) {
95   - db.removeDoc(localFormDoc, opts);
96   - }
97   - },
98   - localDoc : function() {
99   - formToDeepJSON(formSelector, opts.fields, localFormDoc);
100   - return localFormDoc;
101   - }
102   - };
103   - return instance;
  107 + },
  108 + localDoc : function() {
  109 + formToDeepJSON(formSelector, opts.fields, localFormDoc);
  110 + return localFormDoc;
104 111 }
105   -
106   -
107   -
108   -
  112 + };
  113 + return instance;
  114 +}

0 comments on commit 3b63dfa

Please sign in to comment.
Something went wrong with that request. Please try again.