forked from sarfarazansari/ng-jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ng-jsoneditor.js
113 lines (94 loc) · 4.78 KB
/
ng-jsoneditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(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, {
onChange: 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('onChange')) {
settings.onChange(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 (k !== 'completer' && 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;
}
}
}
}
}, true);
$scope.$on('$destroy', function () {
//remove jsoneditor?
});
$scope.updateJsonEditor = function (newValue) {
if (internalTrigger) {
//ignore if called by $setViewValue (after debounceTo)
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); //if someone changes ng-model from outside
}
};
}]);
})();