This repository has been archived by the owner on Apr 5, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ion-tree-list.js
74 lines (65 loc) · 2.19 KB
/
ion-tree-list.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
"use strict";
/* global angular */
var CONF = {
baseUrl: 'lib/ion-tree-list',
digestTtl: 35
};
function addDepthToTree(obj, depth, collapsed) {
for (var key in obj) {
if (obj[key] && typeof(obj[key]) == 'object') {
obj[key].depth = depth;
obj[key].collapsed = collapsed;
addDepthToTree(obj[key], key === 'tree' ? ++ depth : depth, collapsed)
}
}
return obj
}
function toggleCollapse(obj) {
if (obj.tree) {
obj.tree.collapsed = !obj.tree.collapsed;
for (var i = 0; i < obj.tree.length; i++) {
obj.tree[i].collapsed = !obj.tree[i].collapsed;
}
}
return obj
}
angular.module('ion-tree-list', [], ['$rootScopeProvider', function($rootScopeProvider){
$rootScopeProvider.digestTtl(CONF.digestTtl)
}])
.directive('ionTreeList', [function() {
return {
restrict: 'E',
scope: {
items: '=',
collapsed: '=',
templateUrl: '@',
showReorder: '='
},
templateUrl: CONF.baseUrl + '/ion-tree-list.tmpl.html',
controller: ['$scope', function($scope) {
$scope.baseUrl = CONF.baseUrl;
$scope.toggleCollapse = function(item) {
if (item && item.collapsible !== false) {
toggleCollapse(item);
}
};
$scope.emitEvent = function(item){
$scope.$emit('$ionTreeList:ItemClicked', item)
};
$scope.moveItem = function(item, fromIndex, toIndex) {
$scope.items.splice(fromIndex, 1);
$scope.items.splice(toIndex, 0, item)
};
$scope.$watch('collapsed', function() {
$scope.toggleCollapse($scope.items)
});
$scope.$watch('items', function() {
$scope.items = addDepthToTree($scope.items, 1, $scope.collapsed);
$scope.$emit('$ionTreeList:LoadComplete', $scope.items)
})
}],
compile: function(element, attrs){
attrs.templateUrl = attrs.templateUrl ? attrs.templateUrl : 'item_default_renderer';
}
}
}]);