-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-simple-handsontable.js
132 lines (114 loc) · 4.84 KB
/
angular-simple-handsontable.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var module = angular.module('simpleHandsontable', []);
module.factory('simpleHandsontableFactory', [
function () {
return {
containerClassName: 'handsontable-container',
instance: null,
/**
* Append handsontable container div and initialize handsontable instance inside element
*
* @param element
* @param htSettings
*/
initialize: function (element, htSettings) {
var container = document.createElement('DIV');
container.className = this.containerClassName;
element[0].appendChild(container);
this.instance = new Handsontable(container, htSettings);
return this.getInstance();
},
/**
* Getter for instance
* @returns {null}
*/
getInstance: function () {
return this.instance;
}
};
}
]
);
module.directive('simpleHandsontable', ['simpleHandsontableFactory', '$parse', '$timeout', function (simpleHandsontableFactory, $parse, $timeout) {
return {
restrict: 'EA',
scope: {
settings: '=',
data: '=',
deepWatch: '@'
},
controller: ['$scope', function ($scope) {
}],
link: function (scope, element, attrs) {
var data = $parse(attrs.data);
var settings = $parse(attrs.settings);
function parseAttributes() {
var attrSettings = settings(scope.$parent);
var settingsArgument = {};
// If data is not supplied via the settings attribute, copy the settings attribute so that it stays unpolluted by the data property
if (!attrSettings['data']) {
settingsArgument = angular.copy(attrSettings);
scope.htData = data(scope.$parent);
settingsArgument['data'] = scope.htData;
} else {
scope.htData = attrSettings['data'];
settingsArgument = attrSettings;
}
scope.htSettings = settingsArgument;
}
parseAttributes();
scope.handsontableInstance = simpleHandsontableFactory.initialize(element, scope.htSettings);
/**
* Check if settings has been changed in parent scope.
* If so, update the settings for the handsontable instance
*/
scope.$parent.$watch(
attrs.settings,
function () {
parseAttributes();
scope.handsontableInstance.updateSettings(scope.htSettings);
},
true
);
/**
* Check if data has been changed in parent scope.
* If so, run render() on the handsontable instance
* http://docs.handsontable.com/0.19.0/tutorial-data-binding.html
* Defaults to use a deep watch, which can be disabled
* (for performance reasons) by setting deep-watch="false"
*/
if (typeof attrs.deepWatch === 'undefined' || attrs.deepWatch) {
scope.$parent.$watch(
attrs.data,
function () {
$timeout(function () {
scope.handsontableInstance.render();
});
},
true
);
} else {
scope.$parent.$watchCollection(
attrs.data,
function () {
$timeout(function () {
scope.handsontableInstance.render();
});
}
);
}
/**
* Make sure changes made within the handsontable are advocated to angular scopes
*/
scope.handsontableInstance.addHook('afterChange', function (changes, source) {
if (source === 'loadData') {
return; // we only need to apply changes made after the initial data loading
}
// inform angular that we have updated the data by implicitly calling $apply via $timeout
$timeout(function () {
});
});
}
};
}
]
);