extensions wip

This commit is contained in:
Mo Bitar
2017-01-04 00:14:20 -06:00
parent 18a9726f89
commit 24ed04b46f
9 changed files with 422 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
angular.module('app.frontend')
.directive("header", function(apiController){
.directive("header", function(apiController, extensionManager){
return {
restrict: 'E',
scope: {
@@ -19,7 +19,9 @@ angular.module('app.frontend')
}
}
})
.controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout) {
.controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout, extensionManager) {
this.extensionManager = extensionManager;
this.changePasswordPressed = function() {
this.showNewPasswordForm = !this.showNewPasswordForm;
@@ -32,6 +34,25 @@ angular.module('app.frontend')
this.showNewPasswordForm = false;
}
this.toggleExtensions = function() {
this.showExtensionsMenu = !this.showExtensionsMenu;
}
this.toggleExtensionForm = function() {
this.newExtensionData = {};
this.showNewExtensionForm = !this.showNewExtensionForm;
}
this.submitNewExtensionForm = function() {
extensionManager.addExtension(this.newExtensionData.url)
}
this.selectedAction = function(action, extension) {
extensionManager.executeAction(action, extension, function(response){
apiController.sync(null);
})
}
this.changeServer = function() {
apiController.setServer(this.serverData.url, true);
}

View File

@@ -14,8 +14,6 @@ angular.module('app.frontend')
setInterval(function () {
apiController.sync(null);
}, 30000);
// apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){});
}
apiController.getCurrentUser(function(user){

View File

@@ -219,6 +219,7 @@ angular.module('app.frontend')
this.syncWithOptions = function(callback, options = {}) {
if(!this.user.uuid) {
this.writeItemsToLocalStorage();
modelManager.clearDirtyItems();
if(callback) {
callback();
}

View File

@@ -0,0 +1,105 @@
class Extension {
constructor(json) {
_.merge(this, json);
this.actions = this.actions.map(function(action){
return new Action(action);
})
}
}
class Action {
constructor(json) {
_.merge(this, json);
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
this.repeatFrequency = comps[2];
}
}
}
class ExtensionManager {
constructor(Restangular, modelManager) {
this.Restangular = Restangular;
this.modelManager = modelManager;
this.extensions = [];
this.enabledRepeatActions = [];
}
addExtension(url) {
console.log("Registering URL", url);
this.Restangular.oneUrl(url, url).get().then(function(response){
console.log("get response", response.plain());
var extension = new Extension(response.plain());
this.registerExtension(extension);
}.bind(this))
.catch(function(response){
console.log("Error registering extension", response);
})
}
registerExtension(extension) {
this.extensions.push(extension);
for(var action of extension.actions) {
if(action.repeatable) {
this.enableRepeatAction(action);
}
}
console.log("registered extensions", this.extensions);
}
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);
var items = response.items;
this.modelManager.mapResponseItemsToLocalModels(items);
callback(items);
}.bind(this))
}
}
enableRepeatAction(action, extension) {
console.log("Enabling repeat action", action);
this.enabledRepeatActions.push(action);
if(action.repeatType == "watch") {
for(var structure of action.structures) {
this.modelManager.watchItemType(structure.type, function(changedItems){
this.triggerWatchAction(action, changedItems);
}.bind(this))
}
}
}
triggerWatchAction(action, changedItems) {
console.log("Watch action triggered", action, changedItems);
if(action.repeatFrequency > 0) {
var lastExecuted = action.lastExecuted;
var diffInSeconds = (new Date() - lastExecuted)/1000;
if(diffInSeconds < action.repeatFrequency) {
console.log("too frequent, returning");
return;
}
}
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.content};
return params;
})
request.post().then(function(response){
console.log("watch action response", response);
action.lastExecuted = new Date();
})
}
}
}
angular.module('app.frontend').service('extensionManager', ExtensionManager);

View File

@@ -5,6 +5,7 @@ class ModelManager extends ItemManager {
this.notes = [];
this.tags = [];
this.dirtyItems = [];
this.changeObservers = [];
}
resolveReferences() {
@@ -22,6 +23,12 @@ class ModelManager extends ItemManager {
})
}
watchItemType(type, callback) {
console.log("Watching item type", type, "callback:", callback);
this.changeObservers.push({type: type, callback: callback});
console.log("Change observers", this.changeObservers);
}
addDirtyItems(items) {
if(!(items instanceof Array)) {
items = [items];
@@ -36,6 +43,12 @@ class ModelManager extends ItemManager {
}
clearDirtyItems() {
console.log("Clearing dirty items", this.dirtyItems);
for(var observer of this.changeObservers) {
var changedItems = this.dirtyItems.filter(function(item){return item.content_type == observer.type});
console.log("observer:", observer, "items", changedItems);
observer.callback(changedItems);
}
this.dirtyItems = [];
}