-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathangular-pouchdb.js
242 lines (233 loc) · 8 KB
/
angular-pouchdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function() {
var concat, exists, indexOf, pouchdb, slice, sortedIndex;
pouchdb = angular.module('pouchdb', ['ng']);
slice = Array.prototype.slice;
concat = Array.prototype.concat;
sortedIndex = function(array, value, callback) {
var high, low, mid;
low = 0;
high = array != null ? array.length : low;
callback = callback || identity;
value = callback(value);
while (low < high) {
mid = (low + high) >>> 1;
if (callback(array[mid]) < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
};
indexOf = function(array, cond) {
var pos;
pos = 0;
while (pos < array.length && !cond(array[pos])) {
pos = pos + 1;
}
if (pos < array.length) {
return pos;
} else {
return -1;
}
};
exists = function(array, cond) {
return indexOf(array, cond) >= 0;
};
pouchdb.provider('pouchdb', function() {
return {
withAllDbsEnabled: function() {
return PouchDB.enableAllDbs = true;
},
$get: function($q, $rootScope, $timeout) {
var qify;
qify = function(fn) {
return function() {
var args, callback, deferred;
deferred = $q.defer();
callback = function(err, res) {
return $timeout(function() {
if (err) {
return deferred.reject(err);
} else {
return deferred.resolve(res);
}
});
};
args = arguments != null ? slice.call(arguments) : [];
args.push(callback);
fn.apply(this, args);
return deferred.promise;
};
};
return {
create: function(name, options) {
var db;
db = new PouchDB(name, options);
return {
id: db.id,
put: qify(db.put.bind(db)),
post: qify(db.post.bind(db)),
get: qify(db.get.bind(db)),
remove: qify(db.remove.bind(db)),
bulkDocs: qify(db.bulkDocs.bind(db)),
allDocs: qify(db.allDocs.bind(db)),
changes: function(options) {
var clone;
clone = angular.copy(options);
clone.onChange = function(change) {
return $rootScope.$apply(function() {
return options.onChange(change);
});
};
return db.changes(clone);
},
putAttachment: qify(db.putAttachment.bind(db)),
getAttachment: qify(db.getAttachment.bind(db)),
removeAttachment: qify(db.removeAttachment.bind(db)),
query: qify(db.query.bind(db)),
info: qify(db.info.bind(db)),
compact: qify(db.compact.bind(db)),
revsDiff: qify(db.revsDiff.bind(db)),
replicate: {
to: db.replicate.to.bind(db),
from: db.replicate.from.bind(db),
sync: db.replicate.sync.bind(db)
},
destroy: qify(db.destroy.bind(db))
};
}
};
}
};
});
pouchdb.directive('pouchRepeat', function($parse, $animate) {
return {
transclude: 'element',
priority: 10,
compile: function(elem, attrs, transclude) {
return function($scope, $element, $attr) {
var add, blocks, collection, cursor, fld, getters, modify, parent, remove, sort, top, vectorOf, _ref;
parent = $element.parent();
top = angular.element(document.createElement('div'));
parent.append(top);
_ref = /^\s*([a-zA-Z0-9]+)\s*in\s*([a-zA-Z0-9]+)\s*(?:order by\s*([a-zA-Z0-9\.,]+))?$/.exec($attr.pouchRepeat).splice(1), cursor = _ref[0], collection = _ref[1], sort = _ref[2];
blocks = [];
vectorOf = sort != null ? (getters = (function() {
var _i, _len, _ref1, _results;
_ref1 = sort.split(',');
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
fld = _ref1[_i];
_results.push($parse(fld));
}
return _results;
})(), function(doc) {
var getter, _i, _len, _results;
_results = [];
for (_i = 0, _len = getters.length; _i < _len; _i++) {
getter = getters[_i];
_results.push(getter(doc));
}
return _results;
}) : null;
add = function(doc) {
var childScope;
childScope = $scope.$new();
childScope[cursor] = doc;
return transclude(childScope, function(clone) {
var block, index, last, preceding;
block = {
doc: doc,
clone: clone,
scope: childScope,
vector: vectorOf != null ? vectorOf(doc) : null
};
last = blocks[blocks.length - 1];
if (vectorOf != null) {
index = sortedIndex(blocks, block, function(block) {
return block.vector;
});
preceding = index > 0 ? blocks[index - 1] : null;
$animate.enter(clone, parent, preceding != null ? preceding.clone : top);
return blocks.splice(index, 0, block);
} else {
blocks.push(block);
if (last != null) {
return $animate.enter(clone, parent, last.clone);
} else {
return $animate.enter(clone, parent, top);
}
}
});
};
modify = function(doc) {
var block, idx, newidx;
idx = indexOf(blocks, function(block) {
return block.doc._id === doc._id;
});
block = blocks[idx];
block.scope[cursor] = doc;
if (vectorOf != null) {
block.vector = vectorOf(doc);
blocks.splice(idx, 1);
newidx = sortedIndex(blocks, block, function(block) {
return block.vector;
});
blocks.splice(newidx, 0, block);
return $animate.move(block.clone, parent, newidx > 0 ? blocks[newidx - 1].clone : top);
}
};
remove = function(id) {
var block, idx;
idx = indexOf(blocks, function(block) {
return block.doc._id === id;
});
block = blocks[idx];
if (block != null) {
return $animate.leave(block.clone, function() {
return block.scope.$destroy();
});
}
};
return $scope.$watch(collection, function() {
var process;
process = function(result) {
var row, _i, _len, _ref1, _results;
_ref1 = result.rows;
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
row = _ref1[_i];
_results.push(add(row.doc));
}
return _results;
};
$scope[collection].allDocs({
include_docs: true
}).then(process);
$scope[collection].info().then(function(info) {
return $scope[collection].changes({
include_docs: true,
continuous: true,
since: info.update_seq,
onChange: function(update) {
if (update.deleted) {
return remove(update.doc._id);
} else {
if (exists(blocks, function(block) {
return block.doc._id === update.doc._id;
})) {
return modify(update.doc);
} else {
return add(update.doc);
}
}
}
});
});
});
};
}
};
});
}).call(this);