Skip to content

Commit

Permalink
offline support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben committed Feb 14, 2011
1 parent 57d6496 commit cb39f5b
Show file tree
Hide file tree
Showing 44 changed files with 584 additions and 2 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ software and other kinds of works.

The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
the GNU General Public Licensasdase is intended to guarantee your freedom to
share and change all versions of asda program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
Expand Down
1 change: 1 addition & 0 deletions client/ext/gotofile/gotofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ return ext.register("ext/gotofile/gotofile", {
alone : true,
type : ext.GENERAL,
markup : markup,
offline : 0,
commands : {
"gotofile": {hint: "search for a filename and jump to it"}
},
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297621334000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297621888000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297622003000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297622910000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297623803000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297623883000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297623956000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297624215000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297624271000.gz
Binary file not shown.
Binary file added client/ext/offline/.lib-sync.js.1297624318000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297609329000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297618511000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297621783000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297621832000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297621947000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297622097000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297622166000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297622262000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297622400000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297622486000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297623019000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297623385000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297623411000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297623471000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297623551000.gz
Binary file not shown.
Binary file added client/ext/offline/.offline.js.1297671819000.gz
Binary file not shown.
205 changes: 205 additions & 0 deletions client/ext/offline/lib-offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
* General Purpose Offline Syncing Library
*
* @event losechanges Fires before the offline state is removed.
* cancelable: Prevents the application from losing it's recorded offline state.
* @event beforeoffline Fires before bringing the application offline.
* cancelable: Prevents the application from going offline
* @event afteroffline Firest after the application is brought offline.
* @event beforeonline Fires before bringing the application online.
* cancelable: Prevents the application from going online
* @event afteronline Fires after the application is brought online.
* @event beforeload Fires before loading the offline state into this application.
* cancelable: Prevents the application from reloading it's offline state.
* @event sync Fires at each sync item's completion.
* object:
* {Number} position the number of the item in the list that is currently processed.
* {Number} length the total number of items in the list.
*
* @property {Number} progress the progress of the sync. A number between 0 and 1.
* @property {Number} position the progress of the sync.
* @property {Number} length the total length of items to sync.
* @property {Boolean} syncing whether the application is syncing while coming online.
* @property {Boolean} onLine whether the application is online. This property is false during sync.
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
require.def("ext/offline/lib-offline",
function() {

var Offline = function(namespace, detectUrl){
/**
* whether the application is online.
* @type {Boolean}
*/
this.onLine = -1;
this.detectUrl = detectUrl;
this.interval = 5000;
this.namespace = namespace;

//navigator.onLine
var cache = window.applicationCache;

//@todo this is non-ie for now

cache.addEventListener("offline", function(e){
console.log(e.type);
});

cache.addEventListener("online", function(e){
console.log(e.type);
});

cache.addEventListener("checking", function(e){
console.log(e.type);
});

cache.addEventListener("downloading", function(e){
console.log(e.type);
});

cache.addEventListener("progress", function(e){
console.log(e.type);
});

cache.addEventListener("cached", function(e){
console.log(e.type);
});

cache.addEventListener("noupdate", function(e){
console.log(e.type);
});

cache.addEventListener("updateready", function(e){
console.log(e.type);
cache.swapCache();
});

cache.addEventListener("error", function(e){
console.log(e.type);
});
};

(function(){

this.start = function(){
this.offlineTime = parseInt(localStorage[this.namespace + ".offlinetime"]);

//If we were offline lets stay offline
if (this.offlineTime)
this.goOffline();
else //Else we try to go online
this.goOnline();

this.startDetect();
}

/**** Offline Detection ****/

this.isSiteAvailable = function(callback){
var _self = this;

this.http.get(apf.getNoCacheUrl(this.detectUrl), {
callback: function(data, state, extra){
if(state != apf.SUCCESS || !window.navigator.onLine){
_self.goOffline(callback); //retry here??
}
else{
_self.goOnline(callback);
}
},
ignoreOffline : true,
hideLogMessage : true
});
};

this.startDetect = function(){
if (this.detectErrorHandler) //Detection already started
return;

var _self = this;

apf.addEventListener("error", this.detectErrorHandler = function(e){
//Timeout detected.. Network is probably gone
if (e.state == apf.TIMEOUT) {
//Let's try to go offline and return false to cancel the error
return !_self.goOffline();//callback //@todo callback???
}
});

this.http = new apf.http();
this.http.timeout = this.interval;

//Check if we have connection right now
this.isSiteAvailable();

//#ifdef __DEBUG
apf.console.info("Started automatic detection of network state");
//#endif

this.detectTimer = setInterval(function(){
_self.isSiteAvailable();
}, this.interval);
}

this.stopDetect = function(){
clearInterval(this.detectTimer);
apf.removeEventListener("error", this.detectErrorHandler);

//#ifdef __DEBUG
apf.console.info("Stopped automatic detection of network state");
//#endif
}

/**** Offline State Management ****/

/**
* Brings the application offline.
*/
this.goOffline = function(){
if (this.onLine === false)
return false;

if (this.dispatchEvent("beforeoffline") === false)
return false;

//We're offline, let's dim the light
this.onLine = false;

if (!this.offlineTime) {
this.offlineTime = new Date().getTime();
localStorage[this.namespace + ".offlinetime"] = this.offlineTime;
}

this.dispatchEvent("afteroffline");

return true;//success
}

/**
* Brings the application online.
*/
this.goOnline = function(){
if (this.onLine === true)
return false;

if (this.dispatchEvent("beforeonline") === false)
return false;

//We're online, let's show the beacon
this.onlineTime = new Date().getTime();
this.onLine = true; //@todo Think about doing this in the callback, because of processes that will now intersect
this.offlineTime = null;

delete localStorage[this.namespace + ".offlinetime"];

this.dispatchEvent("afteronline");

return true;//success
}
}).call(Offline.prototype = new apf.Class().$init());

return Offline;

});
98 changes: 98 additions & 0 deletions client/ext/offline/lib-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require.def("ext/offline/lib-sync",
function() {

var Sync = function(namespace){
this.namespace = namespace;

this.items = localStorage[this.namespace + ".syncitems"]
? apf.unserialize(localStorage[this.namespace + ".syncitems"])
: {length: 100};

};

(function(){
this.getLength = function(){
return this.items.length;
}

/**
* Clears all offline data.
*/
this.clear = function(){
if (!this.enabled)
return false;

//#ifdef __DEBUG
apf.console.info("Clearing all offline and state cache");
//#endif

this.items = {length: 0};
localStorage[this.namespace + ".syncitems"] = apf.serialize(this.items);
}

this.add = function(id, syncItem){
this.items[id] = syncItem;
this.items.length++;
console.log("SYNC");
localStorage[this.namespace + ".syncitems"] = apf.serialize(this.items);

//@todo error handling
}

/**
* Does cleanup after we've come online
* @private
*/
this.start = function(handler){
if (this.syncing)
return;

var syncItems = apf.extend({}, this.items),
syncLength = this.items.length,
len, i;

var _self = this;
var next = function(){
if (!_self.syncing)
return false;

syncItems.length--;
localStorage[_self.namespace + ".syncitems"] = apf.serialize(syncItems); //Save state up to now

if (syncItems.length < 0) {
_self.items = {length: 0};
localStorage[_self.namespace + ".syncitems"] = apf.serialize(_self.items);

return -1;
}

for (var id in syncItems) {
if (id == "length") continue;
var item = syncItems[id];
delete syncItems[id];
break;
}

handler({
item : item,
progress : parseInt((syncLength - syncItems.length)/syncLength*100),
position : (syncLength - syncItems.length),
length : syncLength
}, next);

return 1;
}

this.syncing = true;
next();
};

this.stop = function(){
if (this.syncing)
this.syncing = false;
};
}).call(Sync.prototype);

return Sync;

});
Loading

0 comments on commit cb39f5b

Please sign in to comment.