From e9da0aa3517cd125a379711d3c8546fa9633232c Mon Sep 17 00:00:00 2001 From: kgiszewski Date: Tue, 18 Aug 2015 12:09:16 -0400 Subject: [PATCH] What a mess this is turing out to be :-1: --- app/directives/archetypeproperty.js | 13 ++++- app/services/archetypeCacheService.js | 79 +++++++++++++++++++++++---- app/services/archetypeService.js | 15 ++--- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/app/directives/archetypeproperty.js b/app/directives/archetypeproperty.js index f3f99e2..03263c3 100644 --- a/app/directives/archetypeproperty.js +++ b/app/directives/archetypeproperty.js @@ -114,6 +114,11 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c archetypeService.propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); }); + scope.$on('formSubmitting', function(ev, args){ + archetypeCacheService.clearInvalidations(); + archetypeCacheService.clearNotifications(); + }); + scope.$on('archetypeFormSubmitting', function (ev, args) { // validate all fieldset properties _.each(scope.fieldset.properties, function (property) { @@ -135,6 +140,10 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c // notify the linker that the property value changed archetypeService.propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); } + + archetypeService.validateMinFieldsets(scope); + + archetypeCacheService.notifyEditor(); }); // issue 114: handle file selection on property editors @@ -162,11 +171,11 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c scope.archetypeRenderModel.fieldsets.length = args.visible; - archetypeService.validateMinFieldsets(scope, ngModelCtrl, notificationsService); + archetypeService.validateMinFieldsets(scope); }); scope.$on("archetypeAddFieldset", function (ev, args) { - archetypeService.validateMinFieldsets(scope, ngModelCtrl, notificationsService); + archetypeService.validateMinFieldsets(scope); }); element.html(data).show(); diff --git a/app/services/archetypeCacheService.js b/app/services/archetypeCacheService.js index cd63293..54d87c9 100644 --- a/app/services/archetypeCacheService.js +++ b/app/services/archetypeCacheService.js @@ -1,4 +1,4 @@ -angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource) { +angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource, notificationsService) { //private var isEntityLookupLoading = false; @@ -7,19 +7,78 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar var isDatatypeLookupLoading = false; var datatypeCache = []; - var notificationsCache = []; + var notificationQueue = []; + var notificationCache = []; + + var invalidationCache = []; + + function findItem(array, item) { + return _.find(array, function(value){ + return value == item; + }); + } return { - addNotification: function(key) { - notificationsCache.push(key); - }, - hasBeenNotified: function(key) { - var notification = _.find(notificationsCache, function(value){ - return value.toLowerCase() == key.toLowerCase(); - }); + notifyEditor: function() { + console.log("queue-v"); + console.log(notificationQueue); + console.log("sent-v"); + console.log(notificationCache); - return (typeof notification != 'undefined'); + if(this.shouldBeNotified("minFieldsets") && !this.hasBeenNotified("minFieldsets")) { + notificationsService.error("Error", "Some of your properties do not contain enough fieldsets."); + + this.removeNotification("minFieldsets"); + + notificationCache.push("minFieldsets"); + } }, + + clearInvalidations: function() { + invalidationCache = []; + }, + + addInvalidation: function(key) { + if(!this.hasBeenInvalidated(invalidationCache, key)) { + invalidationCache.push(key); + } + }, + + removeInvalidation: function(key) { + invalidationCache = _.reject(invalidationCache, function(value){ + return value == key; + }); + }, + + hasBeenInvalidated: function(key) { + return (typeof findItem(invalidationCache, key) != 'undefined'); + }, + + clearNotifications: function() { + notificationCache = []; + + }, + + addNotification: function(key) { + if(!this.shouldBeNotified(key) && !this.hasBeenNotified(notificationCache, key)) { + notificationQueue.push(key); + } + }, + + removeNotification: function(key) { + notificationQueue = _.reject(notificationQueue, function(value){ + return value == key; + }); + }, + + shouldBeNotified: function(key) { + return (typeof findItem(notificationQueue, key) != 'undefined'); + }, + + hasBeenNotified: function(key) { + return (typeof findItem(notificationCache, key) != 'undefined'); + }, + getDataTypeFromCache: function(guid) { return _.find(datatypeCache, function (dt){ return dt.dataTypeGuid == guid; diff --git a/app/services/archetypeService.js b/app/services/archetypeService.js index ddc61ca..f34efeb 100644 --- a/app/services/archetypeService.js +++ b/app/services/archetypeService.js @@ -100,17 +100,14 @@ angular.module('umbraco.services').factory('archetypeService', function (archety property.isValid = true; this.setFieldsetValidity(fieldset); }, - validateMinFieldsets: function(scope, ngModelCtrl, notificationsService) { - ngModelCtrl.$setValidity('propertyForm', true); + validateMinFieldsets: function(scope) { + //ngModelCtrl.$setValidity('propertyForm', true); + archetypeCacheService.removeInvalidation("minFieldsets"); - //min fieldsets if(scope.archetypeConfig.minFieldsets && scope.archetypeRenderModel.fieldsets.length < scope.archetypeConfig.minFieldsets) { - ngModelCtrl.$setValidity('propertyForm', false); - - if(!archetypeCacheService.hasBeenNotified("minFieldsets")) { - notificationsService.error("Error", "Please check your input as it requires at least " + scope.archetypeConfig.minFieldsets + " fieldsets."); - archetypeCacheService.addNotification("minFieldsets"); - } + //ngModelCtrl.$setValidity('propertyForm', false); + archetypeCacheService.addInvalidation("minFieldsets"); + archetypeCacheService.addNotification("minFieldsets"); } } }