From ec8df1d4659934e484eb368a610277cab73952d6 Mon Sep 17 00:00:00 2001 From: kgiszewski Date: Tue, 18 Aug 2015 13:03:11 -0400 Subject: [PATCH] Housekeeping --- app/controllers/config.controller.js | 2 +- app/controllers/controller.js | 4 ++ app/directives/archetypeproperty.js | 80 ++++++++-------------------- app/services/archetypeService.js | 34 +++++++++++- 4 files changed, 61 insertions(+), 59 deletions(-) diff --git a/app/controllers/config.controller.js b/app/controllers/config.controller.js index ebf2111..2eae9bd 100644 --- a/app/controllers/config.controller.js +++ b/app/controllers/config.controller.js @@ -104,7 +104,7 @@ angular.module("umbraco").controller("Imulus.ArchetypeConfigController", functio //ini the properties _.each($scope.archetypeConfigRenderModel.fieldsets, function(fieldset){ - $scope.focusProperty(fieldset.properties); + $scope.focusProperty(fieldset.properties); }); //setup JSON.stringify helpers diff --git a/app/controllers/controller.js b/app/controllers/controller.js index 09c23d6..924e90e 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -88,9 +88,13 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc $scope.model.value.fieldsets.push(newFieldset); } } + $scope.setDirty(); + $scope.$broadcast("archetypeAddFieldset", {index: $index, visible: countVisible()}); + newFieldset.collapse = $scope.model.config.enableCollapsing ? true : false; + $scope.focusFieldset(newFieldset); } } diff --git a/app/directives/archetypeproperty.js b/app/directives/archetypeproperty.js index eb7fb11..58430f3 100644 --- a/app/directives/archetypeproperty.js +++ b/app/directives/archetypeproperty.js @@ -40,64 +40,12 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c var mergedConfig = _.extend(defaultConfigObj, config); - loadView(pathToView, mergedConfig, defaultValue, alias, propertyAlias, scope, element, ngModelCtrl, propertyValueChanged); + loadView(pathToView, mergedConfig, defaultValue, alias, propertyAlias, scope, element, ngModelCtrl, configFieldsetModel); }); }); - - scope.$on("archetypeFormSubmitting", function (ev, args) { - // validate all fieldset properties - _.each(scope.fieldset.properties, function (property) { - validateProperty(scope.fieldset, property); - }); - - var validationKey = "validation-f" + scope.fieldsetIndex; - ngModelCtrl.$setValidity(validationKey, scope.fieldset.isValid); - }); - - scope.$on("archetypeRemoveFieldset", function (ev, args) { - var validationKey = "validation-f" + args.index; - ngModelCtrl.$setValidity(validationKey, true); - }); - - - // called when the value of any property in a fieldset changes - function propertyValueChanged(fieldset, property) { - // it's the Umbraco way to hide the invalid state when altering an invalid property, even if the new value isn't valid either - property.isValid = true; - setFieldsetValidity(fieldset); - } - - // validate a property in a fieldset - function validateProperty(fieldset, property) { - var propertyConfig = archetypeService.getPropertyByAlias(configFieldsetModel, property.alias); - if (propertyConfig) { - // use property.value !== property.value to check for NaN values on numeric inputs - if (propertyConfig.required && (property.value == null || property.value === "" || property.value !== property.value)) { - property.isValid = false; - } - // issue 116: RegEx validate property value - // Only validate the property value if anything has been entered - RegEx is considered a supplement to "required". - if (property.isValid == true && propertyConfig.regEx && property.value) { - var regEx = new RegExp(propertyConfig.regEx); - if (regEx.test(property.value) == false) { - property.isValid = false; - } - } - } - - setFieldsetValidity(fieldset); - } - - function setFieldsetValidity(fieldset) { - // mark the entire fieldset as invalid if there are any invalid properties in the fieldset, otherwise mark it as valid - fieldset.isValid = - _.find(fieldset.properties, function (property) { - return property.isValid == false - }) == null; - } } - function loadView(view, config, defaultValue, alias, propertyAlias, scope, element, ngModelCtrl, propertyValueChanged) { + function loadView(view, config, defaultValue, alias, propertyAlias, scope, element, ngModelCtrl, configFieldsetModel) { if (view) { $http.get(view, { cache: true }).success(function (data) { @@ -120,6 +68,7 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c archetypeService.getFieldset(scope).properties.push(JSON.parse('{"alias": "' + alias + '", "value": "' + defaultValue + '"}')); scope.renderModelPropertyIndex = archetypeService.getPropertyIndexByAlias(archetypeService.getFieldset(scope).properties, alias); } + scope.renderModel = {}; scope.model.value = archetypeService.getFieldsetProperty(scope).value; @@ -158,13 +107,23 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c //watch for changes since there is no two-way binding with the local model.value scope.$watch('model.value', function (newValue, oldValue) { + archetypeService.getFieldsetProperty(scope).value = newValue; // notify the linker that the property value changed - propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); + archetypeService.propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); }); scope.$on('archetypeFormSubmitting', function (ev, args) { + // validate all fieldset properties + _.each(scope.fieldset.properties, function (property) { + archetypeService.validateProperty(scope.fieldset, property, configFieldsetModel); + }); + + var validationKey = "validation-f" + scope.fieldsetIndex; + + ngModelCtrl.$setValidity(validationKey, scope.fieldset.isValid); + // did the value change (if it did, it most likely did so during the "formSubmitting" event) var property = archetypeService.getFieldsetProperty(scope); @@ -174,7 +133,7 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c archetypeService.getFieldsetProperty(scope).value = scope.model.value; // notify the linker that the property value changed - propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); + archetypeService.propertyValueChanged(archetypeService.getFieldset(scope), archetypeService.getFieldsetProperty(scope)); } }); @@ -182,7 +141,9 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c scope.$on("filesSelected", function (event, args) { // populate the fileNames collection on the property editor state var property = archetypeService.getFieldsetProperty(scope); + property.editorState.fileNames = []; + _.each(args.files, function (item) { property.editorState.fileNames.push(item.name); }); @@ -195,6 +156,11 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c scope.archetypeRenderModel.setFiles(args.files); }); + scope.$on("archetypeRemoveFieldset", function (ev, args) { + var validationKey = "validation-f" + args.index; + ngModelCtrl.$setValidity(validationKey, true); + }); + element.html(data).show(); $compile(element.contents())(scope); @@ -225,4 +191,4 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c umbracoForm: '=' } } -}); +}); \ No newline at end of file diff --git a/app/services/archetypeService.js b/app/services/archetypeService.js index 083f120..157db3f 100644 --- a/app/services/archetypeService.js +++ b/app/services/archetypeService.js @@ -1,5 +1,4 @@ angular.module('umbraco.services').factory('archetypeService', function () { - //public return { //helper that returns a JS ojbect from 'value' string or the original string @@ -67,6 +66,39 @@ angular.module('umbraco.services').factory('archetypeService', function () { }, getFieldsetProperty: function (scope) { return this.getFieldset(scope).properties[scope.renderModelPropertyIndex]; + }, + setFieldsetValidity: function (fieldset) { + // mark the entire fieldset as invalid if there are any invalid properties in the fieldset, otherwise mark it as valid + fieldset.isValid = + _.find(fieldset.properties, function (property) { + return property.isValid == false + }) == null; + }, + validateProperty: function (fieldset, property, configFieldsetModel) { + var propertyConfig = this.getPropertyByAlias(configFieldsetModel, property.alias); + + if (propertyConfig) { + // use property.value !== property.value to check for NaN values on numeric inputs + if (propertyConfig.required && (property.value == null || property.value === "" || property.value !== property.value)) { + property.isValid = false; + } + // issue 116: RegEx validate property value + // Only validate the property value if anything has been entered - RegEx is considered a supplement to "required". + if (property.isValid == true && propertyConfig.regEx && property.value) { + var regEx = new RegExp(propertyConfig.regEx); + if (regEx.test(property.value) == false) { + property.isValid = false; + } + } + } + + this.setFieldsetValidity(fieldset); + }, + // called when the value of any property in a fieldset changes + propertyValueChanged: function (fieldset, property) { + // it's the Umbraco way to hide the invalid state when altering an invalid property, even if the new value isn't valid either + property.isValid = true; + this.setFieldsetValidity(fieldset); } } }); \ No newline at end of file