From 179b7d261c3364db04b3ca5fe6deee83a8aa5c5c Mon Sep 17 00:00:00 2001 From: ParidelPooya Date: Wed, 4 Mar 2015 16:28:40 -0800 Subject: [PATCH 1/4] Fix array editing problem. Add json-change-key attribute to directive so now it is possible to switch on/off changing key Add json-change-order attribute to directive so now it is possible to switch on/off changing ordering of array child Add json-add attribute to directive so now it is possible to switch on/off adding new capability Add json-delete attribute to directive so now it is possible to switch on/off deleting capability --- index.html | 3 ++- js/JSONedit.js | 4 ++++ js/directives.js | 37 ++++++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/index.html b/index.html index 9d98f02..e89ce29 100644 --- a/index.html +++ b/index.html @@ -18,13 +18,14 @@

JSONedit

- +

JSON not well-formed! +
diff --git a/js/JSONedit.js b/js/JSONedit.js index 1d08eff..6056c0c 100644 --- a/js/JSONedit.js +++ b/js/JSONedit.js @@ -9,6 +9,10 @@ function MainViewCtrl($scope, $filter) { Name: "Joe", "Last Name": "Miller", Address: {Street: "Neverland 42"}, Hobbies: ["doing stuff", "dreaming"] }; + $scope.showJsonString = function () { + alert($scope.jsonString); + }; + $scope.$watch('jsonData', function(json) { $scope.jsonString = $filter('json')(json); }, true); diff --git a/js/directives.js b/js/directives.js index e52a7f6..0845b13 100644 --- a/js/directives.js +++ b/js/directives.js @@ -25,8 +25,12 @@ angular.module('JSONedit', ['ui.sortable']) scope: { child: '=', type: '@', - defaultCollapsed: '=' - }, + defaultCollapsed: '=', + jsonAdd: '=', + jsonDelete: '=', + jsonChangeOrder: '=', + jsonChangeKey: '=' + }, link: function(scope, element, attributes) { var stringName = "Text"; var objectName = "Object"; @@ -155,7 +159,8 @@ angular.module('JSONedit', ['ui.sortable']) console.error("object to add to was " + obj); } }; - scope.possibleNumber = function(val) { + + scope.possibleNumber = function (val,child,key) { return isNumber(val) ? parseFloat(val) : val; }; @@ -170,13 +175,13 @@ angular.module('JSONedit', ['ui.sortable']) // recursion var switchTemplate = '' - + '' - + '' + + '' + + '' + '' + '' + '' + '' + + 'placeholder="Empty" ng-model-onblur ng-change="child[key] = possibleNumber(val,child,key)"/>' + '' + ''; @@ -185,9 +190,10 @@ angular.module('JSONedit', ['ui.sortable']) '
' + ''; if (scope.type == "object"){ - // input key - addItemTemplate += ' '; + // input key + addItemTemplate += ' '; + } addItemTemplate += // value type dropdown @@ -202,10 +208,11 @@ angular.module('JSONedit', ['ui.sortable']) + '' + '' // plus button - + '' + + '' + '' + '
'; + // start template if (scope.type == "object"){ var template = '' @@ -215,10 +222,10 @@ angular.module('JSONedit', ['ui.sortable']) + '' // object key + '' - + '' // delete button - + '' + + '' + '' // object value + '' + switchTemplate + '' @@ -233,10 +240,10 @@ angular.module('JSONedit', ['ui.sortable']) + '
' + '
    ' // repeat - + '
  1. ' + + '
  2. ' // delete button - + '' - + '' + + '' + + '' + '' + switchTemplate + '' + '
  3. ' // repeat end From fec6375dd6ff28d228b5b27727111a0ef1062a43 Mon Sep 17 00:00:00 2001 From: ParidelPooya Date: Wed, 4 Mar 2015 16:31:39 -0800 Subject: [PATCH 2/4] Some Cleanup --- js/directives.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/directives.js b/js/directives.js index 0845b13..38aa7c3 100644 --- a/js/directives.js +++ b/js/directives.js @@ -160,7 +160,7 @@ angular.module('JSONedit', ['ui.sortable']) } }; - scope.possibleNumber = function (val,child,key) { + scope.possibleNumber = function (val) { return isNumber(val) ? parseFloat(val) : val; }; @@ -181,7 +181,7 @@ angular.module('JSONedit', ['ui.sortable']) + '' + '' + '' + + 'placeholder="Empty" ng-model-onblur ng-change="child[key] = possibleNumber(val)"/>' + '' + ''; From 231f52d3c03a70861efdf13ba841eb1aae9d962a Mon Sep 17 00:00:00 2001 From: Pooya Paridel Date: Thu, 5 Mar 2015 09:19:17 -0800 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d0a52f3..5e808b3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ User friendly, visual JSON editor built as an AngularJS directive. Provides a basic GUI to edit JSON. -**[Give it a try!](http://mb21.github.io/JSONedit)** +**[Give it a try!](http://paridelpooya.github.io/JSONedit/)** #### Use as Angular module From 5563767b00e720e3edb84110ca5ce40f00c0927e Mon Sep 17 00:00:00 2001 From: ParidelPooya Date: Thu, 5 Mar 2015 10:30:06 -0800 Subject: [PATCH 4/4] Now it is possible to specify how many level you want to be expanded --- index.html | 2 +- js/JSONedit.js | 18 +++++++++++++++++- js/directives.js | 19 +++++++++++-------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index e89ce29..293bdb8 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@

    JSONedit

    - +

    diff --git a/js/JSONedit.js b/js/JSONedit.js index 6056c0c..8595733 100644 --- a/js/JSONedit.js +++ b/js/JSONedit.js @@ -6,7 +6,23 @@ function MainViewCtrl($scope, $filter) { // example JSON $scope.jsonData = { - Name: "Joe", "Last Name": "Miller", Address: {Street: "Neverland 42"}, Hobbies: ["doing stuff", "dreaming"] + x: + { + x: + { + x: + { + x: + { + x: + { + x:[true,false,3,4,5] + } + } + } + } + }, + Name: "Joe", "Last Name": "Miller", Address: { Street: "Neverland 42" }, Hobbies: ["doing stuff", "dreaming"] }; $scope.showJsonString = function () { diff --git a/js/directives.js b/js/directives.js index 38aa7c3..7b07323 100644 --- a/js/directives.js +++ b/js/directives.js @@ -25,7 +25,7 @@ angular.module('JSONedit', ['ui.sortable']) scope: { child: '=', type: '@', - defaultCollapsed: '=', + expandedLevel: '=', jsonAdd: '=', jsonDelete: '=', jsonChangeOrder: '=', @@ -42,10 +42,10 @@ angular.module('JSONedit', ['ui.sortable']) scope.sortableOptions = { axis: 'y' }; - if (scope.$parent.defaultCollapsed === undefined) { + if (scope.$parent.expandedLevel === undefined) { scope.collapsed = false; } else { - scope.collapsed = scope.defaultCollapsed; + scope.collapsed = scope.expandedLevel>0?false:true; } if (scope.collapsed) { scope.chevron = "glyphicon-chevron-right"; @@ -160,8 +160,11 @@ angular.module('JSONedit', ['ui.sortable']) } }; - scope.possibleNumber = function (val) { - return isNumber(val) ? parseFloat(val) : val; + scope.possibleNumber = function (val,oldVal) { + if (typeof oldVal == "number") + return isNumber(val) ? parseFloat(val) : val; + else + return val; }; ////// @@ -175,13 +178,13 @@ angular.module('JSONedit', ['ui.sortable']) // recursion var switchTemplate = '' - + '' - + '' + + '' + + '' + '' + '' + '' + '' + + 'placeholder="Empty" ng-blur="child[key] = possibleNumber(val,child[key])"/>' + '' + '';