Skip to content

Commit

Permalink
Save all modifications to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Apr 1, 2011
1 parent 5b04853 commit 1025649
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions static/js/midgardCreate.js
Expand Up @@ -2,13 +2,15 @@
jQuery.widget('Midgard.midgardCreate', {
options: {
toolbar: 'full',
saveButton: null,
state: 'browse',
highlightColor: '#67cc08'
},

_create: function() {
this._checkSession();
this._enableToolbar();
this._saveButton();
this._editButton();
},

Expand All @@ -34,6 +36,17 @@
}
},

_saveButton: function() {
if (this.options.saveButton) {
return this.options.saveButton;
}

jQuery('#midgard-bar .toolbarcontent-right').append(jQuery('<button id="midgardcreate-save">Save</button>'));
this.options.saveButton = jQuery('#midgardcreate-save');
this.options.saveButton.button({disabled: true})
return this.options.saveButton;
},

_editButton: function() {
var widget = this;
jQuery('#midgard-bar .toolbarcontent-right').append(jQuery('<input type="checkbox" id="midgardcreate-edit" /><label for="midgardcreate-edit">Edit</label>'));
Expand Down Expand Up @@ -63,19 +76,75 @@
this.element.toolbar({display: this.options.toolbar});
},

_checkEditableEvent: function(subject, element) {
if (VIE.RDFa.getSubject(element) !== subject) {
// Propagated event from another entity, ignore
return false;
}
return true;
},

_saveLocal: function(model) {
if (!Modernizr.localstorage) {
return;
}

if (model.isNew()) {
// TODO: We need a list of these
return;
}

localStorage.setItem(model.getSubject(), JSON.stringify(model.toJSONLD()));
},

_readLocal: function(model) {
if (!Modernizr.localstorage) {
return false;
}

var local = localStorage.getItem(model.getSubject());
if (!local) {
return false;
}

VIE.EntityManager.getByJSONLD(JSON.parse(local));
return true;
},

_enableEdit: function() {
var widget = this;
jQuery('[about]').each(function() {
var subject = VIE.RDFa.getSubject(this);
var loadedLocal = false;

jQuery(this).bind('editableenable', function(event, options) {
if (VIE.RDFa.getSubject(options.element) !== subject) {
// Propagated event from another entity, ignore
if (!widget._checkEditableEvent(subject, options.element)) {
return;
}

if (!loadedLocal) {
// Check if local storage has a version and use that
if (widget._readLocal(options.instance)) {
// Enable save button since there are local modifications
widget._saveButton().button({disabled: false});
}
loadedLocal = true;
}

// Highlight the editable
options.element.effect('highlight', {color: widget.options.highlightColor}, 3000);
});

jQuery(this).bind('editablechanged', function(event, options) {
// TODO: Highlight save button
if (!widget._checkEditableEvent(subject, options.element)) {
return;
}

// Save to local storage
widget._saveLocal(options.instance);

// Enable save button
widget._saveButton().button({disabled: false});
});
jQuery(this).editable({disabled: false});
});
Expand Down

0 comments on commit 1025649

Please sign in to comment.