Created
May 7, 2014 16:53
-
-
Save SoftCreatR/564d25326962200e6767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Message autosave related classes for WCF (WIP) | |
* | |
* @author Sascha Greuel | |
* @copyright 2014 Sascha Greuel | |
* @license Creative Commons Attribution-NoDerivatives <http://creativecommons.org/licenses/by-nd/4.0/legalcode> | |
*/ | |
/** | |
* Displays and manages an info box for autosaved contents. | |
*/ | |
WCF.Message.AutoSave = Class.extend({ | |
/** | |
* info box | |
* @var jQuery | |
*/ | |
_container: null, | |
/** | |
* info box id | |
* @var string | |
*/ | |
_containerID: '', | |
/** | |
* info box list items | |
* @var array | |
*/ | |
_containerItems: [ ], | |
/** | |
* info box list item keys | |
* @var array | |
*/ | |
_containerItemKeys: [ ], | |
/** | |
* Initializes the WCF.Message.AutoSave class. | |
*/ | |
init: function() { | |
this._containerID = 'autoSaveNotice'; | |
this._createBox(); | |
var self = this; | |
setTimeout(function() { self._jumpTo(); }, 100); | |
new WCF.PeriodicalExecuter($.proxy(function() { | |
this._loadItems(); | |
}, this), 1000); | |
}, | |
/** | |
* Creates the box. | |
*/ | |
_createBox: function() { | |
this._container = $('<p id="' + this._containerID + '" class="info">Es befinden sich noch ungespeicherte Inhalte im Zwischenspeicher:</p>').hide().insertAfter($('.boxHeadline')); | |
$('<ul />').appendTo(this._container); | |
this._loadItems(); | |
}, | |
/** | |
* Adds an item to the box. | |
*/ | |
_addItem: function(key, object) { | |
var $exists = false; | |
for (var $i = 0; $i < this._containerItems.length; $i++) { | |
if (this._containerItems[$i].objectKey && this._containerItems[$i].objectKey == key) { | |
$exists = true; | |
break; | |
} | |
} | |
if (!$exists) { | |
var $obj = JSON.parse(object); | |
this._containerItems.push({ | |
objectKey: key, | |
objectID: $obj.objectID, | |
objectURL: $obj.url | |
}); | |
$('<li id="autoSaveObj' + $obj.objectID + '"><a href="' + $obj.url + '#autoSaveObj-' + $obj.objectID + '">' + $obj.url + '</a></li>').appendTo(this._container.find('ul')); | |
} | |
if (!this._container.is(':visible')) { | |
this._container.show(); | |
} | |
}, | |
/** | |
* Deletes an item from the box. | |
*/ | |
_deleteItem: function(index, objectID) { | |
this._containerItems.splice(index); | |
this._container.find('#autoSaveObj' + objectID).remove(); | |
if (!this._containerItems.length && this._container.is(':visible')) { | |
this._container.hide(); | |
} | |
}, | |
/** | |
* Reads elements from local storage. | |
*/ | |
_loadItems: function() { | |
// add items | |
for (var $i = 0; $i < localStorage.length; $i++) { | |
var $key = localStorage.key($i); | |
if ($key.lastIndexOf('autosave_', 0) === 0) { | |
this._addItem($key, localStorage.getItem($key)); | |
} | |
} | |
// delete obsolete items | |
for (key in this._containerItems) { | |
var $obj = this._containerItems[key]; | |
if (localStorage.getItem($obj.objectKey) == null) { | |
this._deleteItem(key, $obj.objectID); | |
} | |
} | |
}, | |
_jumpTo: function() { | |
var $hash = window.location.hash.substr(1); | |
if ($hash.lastIndexOf('autoSaveObj-', 0) === 0) { | |
var $objID = $hash.replace('autoSaveObj-', ''); | |
if ($objID.charAt(0) === 'q') { | |
$('.jsQuickReply:eq(0)').trigger('click'); | |
} else { | |
$('[data-object-id=' + parseInt($objID) + ']').find('.jsMessageEditButton:eq(0)').trigger('dblclick'); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment