From 23b799ca268f2beb71c882c0040e12bffdc7c3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20SICRE?= Date: Fri, 7 Jul 2017 17:12:13 +0200 Subject: [PATCH 1/3] protection against missing $scope.options --- ng-jsoneditor.js | 57 +++++++++++++++++++++++++++----------------- ng-jsoneditor.min.js | 2 +- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/ng-jsoneditor.js b/ng-jsoneditor.js index b11f9c7..3cd985a 100644 --- a/ng-jsoneditor.js +++ b/ng-jsoneditor.js @@ -49,29 +49,46 @@ var instance = new JSONEditor(element[0], theOptions); if ($scope.ngJsoneditor instanceof Function) { - $timeout(function () { $scope.ngJsoneditor(instance);}); + $timeout(function () { + $scope.ngJsoneditor(instance); + }); } return instance; } $scope.$watch('options', function (newValue, oldValue) { - for (var k in newValue) { - if (newValue.hasOwnProperty(k)) { - var v = newValue[k]; - - if (newValue[k] !== oldValue[k]) { - if (k === 'mode') { - editor.setMode(v); - } else if (k === 'name') { - editor.setName(v); - } else { //other settings cannot be changed without re-creating the JsonEditor - editor = _createEditor(newValue); - $scope.updateJsonEditor(); - return; + if (editor) { + for (var k in newValue) { + if (newValue.hasOwnProperty(k)) { + var v = newValue[k]; + + if (!oldValue || newValue[k] !== oldValue[k]) { + if (k === 'mode') { + editor.setMode(v); + } else if (k === 'name') { + editor.setName(v); + } else if (k === 'expanded') { + if (newValue[k]) { + editor.expandAll && editor.expandAll() + } else { + editor.collapseAll && editor.collapseAll() + } + } else { //other settings cannot be changed without re-creating the JsonEditor + editor = _createEditor(newValue); + $scope.updateJsonEditor(); + return; + } } } } + } else { + editor = _createEditor($scope.options); + if ($scope.options) { + editor.expandAll && editor.expandAll() + } else { + editor.collapseAll && editor.collapseAll() + } } }, true); @@ -96,17 +113,13 @@ } else { editor.set(ngModel.$viewValue || {}); } - }, $scope.options.timeout || 100); + }, ($scope.options && $scope.options.timeout) || 100); }; - editor = _createEditor($scope.options); - - if ($scope.options.hasOwnProperty('expanded')) { - $timeout($scope.options.expanded ? function () {editor.expandAll()} : function () {editor.collapseAll()}, ($scope.options.timeout || 100) + 100); - } - ngModel.$render = $scope.updateJsonEditor; - $scope.$watch(function () { return ngModel.$modelValue; }, $scope.updateJsonEditor, true); //if someone changes ng-model from outside + $scope.$watch(function () { + return ngModel.$modelValue; + }, $scope.updateJsonEditor, true); //if someone changes ng-model from outside } }; }]); diff --git a/ng-jsoneditor.min.js b/ng-jsoneditor.min.js index 68c29d4..d1d8947 100644 --- a/ng-jsoneditor.min.js +++ b/ng-jsoneditor.min.js @@ -1 +1 @@ -(function(){var module=angular.module("ng.jsoneditor",[]);module.constant("ngJsoneditorConfig",{});module.directive("ngJsoneditor",["ngJsoneditorConfig","$timeout",function(ngJsoneditorConfig,$timeout){var defaults=ngJsoneditorConfig||{};return{restrict:"A",require:"ngModel",scope:{options:"=",ngJsoneditor:"=",preferText:"="},link:function($scope,element,attrs,ngModel){var debounceTo,debounceFrom;var editor;var internalTrigger=false;if(!angular.isDefined(window.JSONEditor)){throw new Error("Please add the jsoneditor.js script first!")}function _createEditor(options){var settings=angular.extend({},defaults,options);var theOptions=angular.extend({},settings,{change:function(){if(typeof debounceTo!=="undefined"){$timeout.cancel(debounceTo)}debounceTo=$timeout(function(){if(editor){internalTrigger=true;var error=undefined;try{ngModel.$setViewValue($scope.preferText===true?editor.getText():editor.get());}catch(err){error=err;}if(settings&&settings.hasOwnProperty("change")){settings.change(error);}}},settings.timeout||100);}});element.html("");var instance=new JSONEditor(element[0],theOptions);if($scope.ngJsoneditor instanceof Function){$timeout(function(){$scope.ngJsoneditor(instance)})}return instance}$scope.$watch("options",function(newValue,oldValue){for(var k in newValue){if(newValue.hasOwnProperty(k)){var v=newValue[k];if(newValue[k]!==oldValue[k]){if(k==="mode"){editor.setMode(v)}else if(k==="name"){editor.setName(v)}else{editor=_createEditor(newValue);$scope.updateJsonEditor();return}}}}},true);$scope.$on("$destroy",function(){});$scope.updateJsonEditor=function(newValue){if(internalTrigger){internalTrigger=false;return;}if(typeof debounceFrom!=="undefined"){$timeout.cancel(debounceFrom)}debounceFrom=$timeout(function(){if($scope.preferText===true&&!angular.isObject(ngModel.$viewValue)){editor.setText(ngModel.$viewValue||"{}")}else{editor.set(ngModel.$viewValue||{})}},$scope.options.timeout||100)};editor=_createEditor($scope.options);if($scope.options.hasOwnProperty("expanded")){$timeout($scope.options.expanded?function(){editor.expandAll()}:function(){editor.collapseAll()},($scope.options.timeout||100)+100)}ngModel.$render=$scope.updateJsonEditor;$scope.$watch(function(){return ngModel.$modelValue},$scope.updateJsonEditor,true)}}}])})(); +(function(){var module=angular.module("ng.jsoneditor",[]);module.constant("ngJsoneditorConfig",{}),module.directive("ngJsoneditor",["ngJsoneditorConfig","$timeout",function(ngJsoneditorConfig,$timeout){var defaults=ngJsoneditorConfig||{};return{restrict:"A",require:"ngModel",scope:{options:"=",ngJsoneditor:"=",preferText:"="},link:function($scope,element,attrs,ngModel){function _createEditor(options){var settings=angular.extend({},defaults,options),theOptions=angular.extend({},settings,{change:function(){debounceTo!==void 0&&$timeout.cancel(debounceTo),debounceTo=$timeout(function(){if(editor){internalTrigger=!0;var error=void 0;try{ngModel.$setViewValue($scope.preferText===!0?editor.getText():editor.get())}catch(err){error=err}settings&&settings.hasOwnProperty("change")&&settings.change(error)}},settings.timeout||100)}});element.html("");var instance=new JSONEditor(element[0],theOptions);return $scope.ngJsoneditor instanceof Function&&$timeout(function(){$scope.ngJsoneditor(instance)}),instance}var debounceTo,debounceFrom,editor,internalTrigger=!1;if(!angular.isDefined(window.JSONEditor))throw Error("Please add the jsoneditor.js script first!");$scope.$watch("options",function(newValue,oldValue){if(editor){for(var k in newValue)if(newValue.hasOwnProperty(k)){var v=newValue[k];if(!oldValue||newValue[k]!==oldValue[k])if("mode"===k)editor.setMode(v);else if("name"===k)editor.setName(v);else{if("expanded"!==k)return editor=_createEditor(newValue),$scope.updateJsonEditor(),void 0;newValue[k]?editor.expandAll&&editor.expandAll():editor.collapseAll&&editor.collapseAll()}}}else editor=_createEditor($scope.options),$scope.options?editor.expandAll&&editor.expandAll():editor.collapseAll&&editor.collapseAll()},!0),$scope.$on("$destroy",function(){}),$scope.updateJsonEditor=function(){return internalTrigger?(internalTrigger=!1,void 0):(debounceFrom!==void 0&&$timeout.cancel(debounceFrom),debounceFrom=$timeout(function(){$scope.preferText!==!0||angular.isObject(ngModel.$viewValue)?editor.set(ngModel.$viewValue||{}):editor.setText(ngModel.$viewValue||"{}")},$scope.options&&$scope.options.timeout||100),void 0)},ngModel.$render=$scope.updateJsonEditor,$scope.$watch(function(){return ngModel.$modelValue},$scope.updateJsonEditor,!0)}}}])})(); \ No newline at end of file From 2923a409ea73cdfec8c0e1e8cbf1b2057999294b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20SICRE?= Date: Fri, 6 Oct 2017 13:44:28 +0200 Subject: [PATCH 2/3] change => onChange --- ng-jsoneditor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ng-jsoneditor.js b/ng-jsoneditor.js index 3cd985a..7d036e8 100644 --- a/ng-jsoneditor.js +++ b/ng-jsoneditor.js @@ -21,7 +21,7 @@ function _createEditor(options) { var settings = angular.extend({}, defaults, options); var theOptions = angular.extend({}, settings, { - change: function () { + onChange: function () { if (typeof debounceTo !== 'undefined') { $timeout.cancel(debounceTo); } @@ -36,8 +36,8 @@ error = err; } - if (settings && settings.hasOwnProperty('change')) { - settings.change(error); + if (settings && settings.hasOwnProperty('onChange')) { + settings.onChange(error); } } }, settings.timeout || 100); From dc56387bf9a2ba5cd0acc6fd01ee7d2440f60883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20SICRE?= Date: Fri, 6 Oct 2017 13:49:02 +0200 Subject: [PATCH 3/3] fix package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d46229..0b03859 100644 --- a/package.json +++ b/package.json @@ -27,5 +27,5 @@ "type": "git", "url": "git+https://github.com/angular-tools/ng-jsoneditor.git" }, - "version": "1.0.0" + "version": "1.0.1" }