all extension type

This commit is contained in:
Mo Bitar
2017-01-05 19:22:34 -06:00
parent 54ac4ffd15
commit 6306f2a316
16 changed files with 508 additions and 272 deletions

View File

@@ -207,7 +207,7 @@ angular.module('app.frontend')
var original = this.note.presentation_name;
this.note.presentation_name = this.url.token;
this.note.dirty = true;
this.note.setDirty(true);
apiController.sync(function(response){
if(!response) {

View File

@@ -48,7 +48,9 @@ angular.module('app.frontend')
}
this.selectedAction = function(action, extension) {
action.running = true;
extensionManager.executeAction(action, extension, function(response){
action.running = false;
apiController.sync(null);
})
}

View File

@@ -54,7 +54,7 @@ angular.module('app.frontend')
}
$scope.tagsSave = function(tag, callback) {
tag.dirty = true;
tag.setDirty(true);
apiController.sync(callback);
}
@@ -111,7 +111,7 @@ angular.module('app.frontend')
*/
$scope.saveNote = function(note, callback) {
note.dirty = true;
note.setDirty(true);
apiController.sync(function(){
note.hasChanges = false;

View File

@@ -25,7 +25,7 @@ angular.module('app.frontend')
}
}
})
.controller('NotesCtrl', function (apiController, $timeout, $rootScope) {
.controller('NotesCtrl', function (apiController, $timeout, $rootScope, modelManager) {
$rootScope.$on("editorFocused", function(){
this.showMenu = false;
@@ -110,7 +110,7 @@ angular.module('app.frontend')
this.createNewNote = function() {
var title = "New Note" + (this.tag.notes ? (" " + (this.tag.notes.length + 1)) : "");
this.newNote = new Note({dummy: true, text: ""});
this.newNote = modelManager.createItem({content_type: "Note", dummy: true, text: ""});
this.newNote.title = title;
this.selectNote(this.newNote);
this.addNew()(this.newNote);

View File

@@ -33,7 +33,7 @@ angular.module('app.frontend')
}
}
})
.controller('TagsCtrl', function () {
.controller('TagsCtrl', function (modelManager) {
var initialLoad = true;
@@ -63,7 +63,7 @@ angular.module('app.frontend')
return;
}
this.newTag = new Tag({});
this.newTag = modelManager.createItem({content_type: "Tag"});
this.selectedTag = this.newTag;
this.editingTag = this.newTag;
this.addNew()(this.newTag);

View File

@@ -4,6 +4,8 @@ class Item {
this.updateFromJSON(json_obj);
this.observers = [];
if(!this.uuid) {
this.uuid = Neeto.crypto.generateUUID();
}
@@ -44,6 +46,30 @@ class Item {
}
}
setDirty(dirty) {
this.dirty = dirty;
if(dirty) {
this.notifyObserversOfChange();
}
}
addObserver(observer, callback) {
if(!_.find(this.observers, observer)) {
this.observers.push({observer: observer, callback: callback});
}
}
removeObserver(observer) {
_.remove(this.observers, {observer: observer})
}
notifyObserversOfChange() {
for(var observer of this.observers) {
observer.callback(this);
}
}
mapContentToLocalProperties(contentObj) {
}

View File

@@ -2,11 +2,16 @@ class Action {
constructor(json) {
_.merge(this, json);
this.actionVerb = this.type;
var comps = this.type.split(":");
if(comps.length > 0) {
this.repeatable = true;
this.repeatType = comps[0]; // 'watch' or 'poll'
this.repeatVerb = comps[1]; // http verb
if(comps.length > 1) {
this.actionType = comps[0]; // 'watch', 'poll', or 'all'
this.repeatable = this.actionType == "watch" || this.actionType == "poll";
this.actionVerb = comps[1]; // http verb : "get", "post", "show"
this.repeatFrequency = comps[2];
}
}

View File

@@ -51,7 +51,7 @@ class Note extends Item {
removeAllRelationships() {
this.tags.forEach(function(tag){
_.pull(tag.notes, this);
tag.dirty = true;
tag.setDirty(true);
}.bind(this))
this.tags = [];
}

View File

@@ -49,7 +49,7 @@ class Tag extends Item {
removeAllRelationships() {
this.notes.forEach(function(note){
_.pull(note.tags, this);
note.dirty = true;
note.setDirty(true);
}.bind(this))
this.notes = [];

View File

@@ -312,7 +312,7 @@ angular.module('app.frontend')
item.presentation_name = "_auto_";
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
needsUpdate.forEach(function(needingUpdate){
needingUpdate.dirty = true;
needingUpdate.setDirty(true);
})
this.sync();
}.bind(this)
@@ -339,7 +339,7 @@ angular.module('app.frontend')
item.presentation_name = null;
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
needsUpdate.forEach(function(needingUpdate){
needingUpdate.dirty = true;
needingUpdate.setDirty(true);
})
this.sync(null);
}
@@ -352,7 +352,7 @@ angular.module('app.frontend')
var data = JSON.parse(jsonString);
modelManager.mapResponseItemsToLocalModels(data.items);
modelManager.items.forEach(function(item){
item.dirty = true;
item.setDirty(true);
})
this.syncWithOptions(callback, {additionalFields: ["created_at", "updated_at"]});
}
@@ -459,7 +459,8 @@ angular.module('app.frontend')
if(!draftString || draftString == 'undefined') {
return null;
}
return new Note(JSON.parse(draftString));
var jsonObj = _.merge({content_type: "Note"}, JSON.parse(draftString));
return modelManager.createItem(jsonObj);
}

View File

@@ -34,7 +34,6 @@ class ExtensionManager {
retrieveExtensionFromServer(url, callback) {
console.log("Registering URL", url);
this.Restangular.oneUrl(url, url).get().then(function(response){
console.log("get response", response.plain());
var ext = this.handleExtensionLoadExternalResponseItem(url, response.plain());
if(callback) {
callback(ext);
@@ -49,12 +48,10 @@ class ExtensionManager {
var extension = _.find(this.extensions, {url: url});
if(extension) {
extension.updateFromExternalResponseItem(externalResponseItem);
console.log("updated existing ext", extension);
} else {
console.log("creating new ext", externalResponseItem);
extension = new Extension(externalResponseItem);
extension.url = url;
extension.dirty = true;
extension.setDirty(true);
this.modelManager.addItem(extension);
this.apiController.sync(null);
}
@@ -70,12 +67,13 @@ class ExtensionManager {
for(var ext of this.extensions) {
this.retrieveExtensionFromServer(ext.url, function(extension){
extension.dirty = true;
extension.setDirty(true);
});
}
}
executeAction(action, extension, callback) {
if(action.type == "get") {
this.Restangular.oneUrl(action.url, action.url).get().then(function(response){
console.log("Execute action response", response);
@@ -84,6 +82,21 @@ class ExtensionManager {
callback(items);
}.bind(this))
}
else if(action.type == "show") {
var win = window.open(action.url, '_blank');
win.focus();
callback();
}
else if(action.actionType == "all") {
var allItems = this.modelManager.allItems();
this.performPost(action, allItems, function(items){
callback(items);
});
}
action.lastExecuted = new Date();
}
isRepeatActionEnabled(action) {
@@ -93,7 +106,7 @@ class ExtensionManager {
disableRepeatAction(action, extension) {
console.log("Disabling action", action);
_.pull(this.enabledRepeatActionUrls, action.url);
this.modelManager.removeItemSyncObserver(action.url);
this.modelManager.removeItemChangeObserver(action.url);
console.assert(this.isRepeatActionEnabled(action) == false);
}
@@ -107,7 +120,7 @@ class ExtensionManager {
if(action.repeatType == "watch") {
for(var structure of action.structures) {
this.modelManager.addItemSyncObserver(action.url, structure.type, function(changedItems){
this.modelManager.addItemChangeObserver(action.url, structure.type, function(changedItems){
this.triggerWatchAction(action, changedItems);
}.bind(this))
}
@@ -117,12 +130,9 @@ class ExtensionManager {
queueAction(action, delay, changedItems) {
this.actionQueue = this.actionQueue || [];
if(_.find(this.actionQueue, action)) {
// console.log("Action already queued, skipping.")
return;
}
// console.log("Adding action to queue", action);
this.actionQueue.push(action);
setTimeout(function () {
@@ -132,6 +142,10 @@ class ExtensionManager {
}.bind(this), delay * 1000);
}
outgoingParamsForItem(item) {
return this.apiController.paramsForItem(item, false, null, true);
}
triggerWatchAction(action, changedItems) {
// console.log("Watch action triggered", action, changedItems);
if(action.repeatFrequency > 0) {
@@ -147,21 +161,29 @@ class ExtensionManager {
}
console.log("Performing action immediately", action);
action.lastExecuted = new Date();
// console.log("setting last exectured", action.lastExecuted)
if(action.repeatVerb == "post") {
var request = this.Restangular.oneUrl(action.url, action.url);
request.items = changedItems.map(function(item){
var params = {uuid: item.uuid, content_type: item.content_type, content: item.createContentJSONFromProperties()};
return params;
})
request.post().then(function(response){
// console.log("watch action response", response);
})
this.performPost(action, changedItems, null);
}
}
performPost(action, items, callback) {
var request = this.Restangular.oneUrl(action.url, action.url);
request.items = items.map(function(item){
var params = this.outgoingParamsForItem(item);
return params;
}.bind(this))
request.post().then(function(response){
// console.log("watch action response", response);
if(callback) {
callback(response.plain());
}
})
}
}
angular.module('app.frontend').service('extensionManager', ExtensionManager);

View File

@@ -4,10 +4,15 @@ class ModelManager {
this.notes = [];
this.tags = [];
this.itemSyncObservers = [];
this.itemChangeObservers = [];
this.items = [];
this.extensions = [];
}
allItems() {
return this.items.filter(function(item){return !item.dummy})
}
findItem(itemId) {
return _.find(this.items, {uuid: itemId});
}
@@ -45,27 +50,47 @@ class ModelManager {
models.push(item)
}
this.notifySyncObserversOfModels(models);
this.sortItems();
return models;
}
notifySyncObserversOfModels(models) {
for(var observer of this.itemSyncObservers) {
var relevantItems = models.filter(function(item){return item.content_type == observer.type});
if(relevantItems.length > 0) {
observer.callback(relevantItems);
}
}
}
this.sortItems();
return models;
notifyItemChangeObserversOfModels(models) {
for(var observer of this.itemChangeObservers) {
var relevantItems = models.filter(function(item){return item.content_type == observer.type});
if(relevantItems.length > 0) {
observer.callback(relevantItems);
}
}
}
createItem(json_obj) {
var item;
if(json_obj.content_type == "Note") {
return new Note(json_obj);
item = new Note(json_obj);
} else if(json_obj.content_type == "Tag") {
return new Tag(json_obj);
item = new Tag(json_obj);
} else if(json_obj.content_type == "Extension") {
return new Extension(json_obj);
item = new Extension(json_obj);
} else {
return new Item(json_obj);
item = new Item(json_obj);
}
item.addObserver(this, function(changedItem){
this.notifyItemChangeObserversOfModels([changedItem]);
}.bind(this));
return item;
}
addItems(items) {
@@ -131,6 +156,14 @@ class ModelManager {
_.remove(this.itemSyncObservers, _.find(this.itemSyncObservers, {id: id}));
}
addItemChangeObserver(id, type, callback) {
this.itemChangeObservers.push({id: id, type: type, callback: callback});
}
removeItemChangeObserver(id) {
_.remove(this.itemChangeObservers, _.find(this.itemChangeObservers, {id: id}));
}
get filteredNotes() {
return Note.filterDummyNotes(this.notes);
}
@@ -141,13 +174,13 @@ class ModelManager {
clearDirtyItems() {
this.getDirtyItems().forEach(function(item){
item.dirty = false;
item.setDirty(false);
})
}
setItemToBeDeleted(item) {
item.deleted = true;
item.dirty = true;
item.setDirty(true);
item.removeAllRelationships();
}
@@ -171,16 +204,16 @@ class ModelManager {
itemOne.addItemAsRelationship(itemTwo);
itemTwo.addItemAsRelationship(itemOne);
itemOne.dirty = true;
itemTwo.dirty = true;
itemOne.setDirty(true);
itemTwo.setDirty(true);
}
removeRelationshipBetweenItems(itemOne, itemTwo) {
itemOne.removeItemAsRelationship(itemTwo);
itemTwo.removeItemAsRelationship(itemOne);
itemOne.dirty = true;
itemTwo.dirty = true;
itemOne.setDirty(true);
itemTwo.setDirty(true);
}
}