Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion firefox/data/js/background/asteroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,22 @@ window.asteroid = (function(){
}
updateOption.updated_date = Date.now();
console.log("updateList - ", updateOption);
knotes.update(options.knoteId, updateOption).local;
if (!options.knoteId && _.isNumber(options.order)) {
if (!asteroid.backupUpdateListStack[options.order]) {
asteroid.backupUpdateListStack[options.order] = {}
}
asteroid.backupUpdateListStack[options.order].title = options.title;
switch(options.case) {
case "updateItems":
asteroid.backupUpdateListStack[options.order].options = options.options;
break;
}
}
return knotes.update(options.knoteId, updateOption).local;
};

exports.backupUpdateListStack = {};

exports.init(config.server);
return exports;
})();
36 changes: 34 additions & 2 deletions firefox/data/js/background/reactive_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

window.reactiveController = (function(){
var exports = {};
var knotes = null;



Expand All @@ -19,7 +20,7 @@ window.reactiveController = (function(){

var initKnoteWatchers = function() {
console.log('initWatchers');
var knotes = asteroid.getCollection('knotes');
knotes = asteroid.getCollection('knotes');
var knotesQuery = knotes.reactiveQuery({});
_watchKnotes(knotesQuery);
_sendCachedKnotes(knotesQuery);
Expand All @@ -29,7 +30,19 @@ window.reactiveController = (function(){

var _sendCachedKnotes = function(knotesQuery){
// send knotes from cache
_.each(knotesQuery.result, function(knote){
var results = _.groupBy(knotesQuery.result, function(knote) {
return knote._id;
});
_.each(_.values(results), function(knote){
// if the knote was created in offline mode, there will be
// two knotes here: the newest knote and the backup knote
if (knote.length > 1) {
knote = _.max(knote, function(k) {
return k.updated_date;
});
} else {
knote = knote[0];
}
if(!knote.archived){
_addedKnote(knote);
}
Expand All @@ -47,6 +60,25 @@ window.reactiveController = (function(){
return knoteId.match(knote._id);
});

if (knote.type == 'checklist') {
var updateOption = asteroid.backupUpdateListStack[knote.order];
var option;
if (!updateOption) {
for (var order in asteroid.backupUpdateListStack) {
option = asteroid.backupUpdateListStack[order];
if (option.title == knote.title) {
updateOption = option;
delete asteroid.backupUpdateListStack[order];
}
}
}
if (updateOption) {
console.log('[Update offline task] id: ', knote._id, ', title: ', knote.title);
delete asteroid.backupUpdateListStack[knote.order];
knotes.update(knote._id, updateOption);
}
}

if (knoteId.match('__upd__') && knote){
_idOfKnoteToUpdate = knote._id;
} else if (knoteId.match('__del__')){
Expand Down
5 changes: 0 additions & 5 deletions firefox/data/js/knotable-views/knotableView-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ var _addKnoteOnView = function(knotesView, newKnote) {
if(knote.get('type') == "checklist" && moment(knote.get('updated_date')).isBefore(newKnote.updated_date, 'second')){
console.log("#GC - _addKnoteOnView - updating checklist", knote.get('updated_date'), newKnote.updated_date);
knote.set({title: newKnote.title, options: newKnote.options, order: newKnote.order, timestamp: newKnote.timestamp, updated_date: newKnote.updated_date});
// if list is currently on active view,
// update the view
if (knotesView.activeKnote && knotesView.activeKnote.get('knoteId') == newKnote._id ){
$('.list-knote[data-knoteid=' + newKnote._id + ']').click();
}
}

// update the last sync time
Expand Down
33 changes: 23 additions & 10 deletions firefox/data/js/knotable-views/knotes-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ var KnotesView = Backbone.View.extend({
var oldOptions = KnoteHelper.getKnoteOptions(this.activeKnote.get('content'));
var content = $("#knote-edit-area").html().trim();

if (this.activeKnote.get('type') != 'knote') return;

if (options.title === oldOptions.title && options.htmlBody === oldOptions.htmlBody){
if (_.isFunction(callback)) {
callback(false);
Expand Down Expand Up @@ -493,12 +495,6 @@ var KnotesView = Backbone.View.extend({
return this;
}

var id = model.get('knoteId') || model.get('_id');
var $knote = $('.list-knote[data-knoteid=' + id + ']');
if ($knote.length) {
return;
}

var knoteView = new KnoteView(model);
knoteView = knoteView.render().$el;

Expand All @@ -521,9 +517,21 @@ var KnotesView = Backbone.View.extend({
onKnoteChanged: function(model, collection, idx) {
var newContent = model.get('content');
var editArea = $('#knote-edit-area');
var self = this;
if (this.activeKnote == model) {
if (editArea.is(':visible') && newContent != editArea.html().trim()) {
editArea.html(newContent);
if (editArea.is(':visible')) {
if (newContent != editArea.html().trim()) {
editArea.html(newContent);
}
} else {
this.cleanAddingListArea();
$('#knote-list-title').val(model.get('title'));
var options = model.get('options');
if(typeof options != 'undefined') {
options.forEach(function(item, index){
self.addListItemToUI(item.name, item.checked, item.voters[0])
})
}
}
}
this._sortKnotesList();
Expand Down Expand Up @@ -898,14 +906,19 @@ var KnotesView = Backbone.View.extend({


updateListItem: function(){
var options = KnoteHelper.getListData();
var options = KnoteHelper.getListData();
var knoteId = this.activeKnote.get('knoteId');
this.activeKnote.set({
'title': options.title,
'options': options.options,
'updated_date': Date.now()
});
options.knoteId = this.activeKnote.get('knoteId');
options.case = "updateItems";
options.knoteId = knoteId;
if (!knoteId) {
options.order = this.activeKnote.get('order');
options.title = options.title;
}
knoteClient.updateList(options);
},

Expand Down
37 changes: 37 additions & 0 deletions firefox/data/js/vendor/ddp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
this._onUpdatedCallbacks = {};
this._events = {};
this._queue = [];
this._waitingMethodResultStack = {
minId: 0,
methods: {}
};
// Setup
this.readyState = -1;
this._reconnect_count = 0;
Expand All @@ -70,6 +74,14 @@
var id = uniqueId();
this._onResultCallbacks[id] = onResult;
this._onUpdatedCallbacks[id] = onUpdated;
if (this._waitingMethodResultStack.minId == 0) {
this._waitingMethodResultStack.minId = id;
}
this._waitingMethodResultStack.methods[id] = {
method: name,
params: params
};

this._send({
msg: "method",
id: id,
Expand Down Expand Up @@ -164,6 +176,31 @@
};

DDP.prototype._on_result = function (data) {
delete this._waitingMethodResultStack.methods[data.id];
// some method did not get response, we assume that those method were sent failed.
// and we resend the method here.
if (this._waitingMethodResultStack.minId > 0 && data.id > this._waitingMethodResultStack.minId) {
var methodInfo = null;
for (var methodId in this._waitingMethodResultStack.methods) {
if (methodId < data.id) {
methodInfo = this._waitingMethodResultStack.methods[methodId];
this._send({
msg: "method",
id: methodId,
method: methodInfo.method,
params: methodInfo.params
});
// only re-try one time
delete this._waitingMethodResultStack.methods[methodId];
}
}
}
var waitingMethodIds = Object.keys(this._waitingMethodResultStack.methods);
if (waitingMethodIds.length) {
this._waitingMethodResultStack.minId = Math.min.apply(null, waitingMethodIds);
} else {
this._waitingMethodResultStack.minId = 0;
}
if (this._onResultCallbacks[data.id]) {
this._onResultCallbacks[data.id](data.error, data.result);
delete this._onResultCallbacks[data.id];
Expand Down