/
ionicList.js
162 lines (137 loc) · 5.31 KB
/
ionicList.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function() {
'use strict';
angular.module('ionic.ui.list', ['ngAnimate'])
.directive('ionItem', ['$timeout', '$parse', function($timeout, $parse) {
return {
restrict: 'E',
require: '?^ionList',
replace: true,
transclude: true,
scope: {
item: '=',
itemType: '@',
canDelete: '@',
canReorder: '@',
canSwipe: '@',
onDelete: '&',
optionButtons: '&',
deleteIcon: '@',
reorderIcon: '@'
},
template: '<div class="item item-complex">\
<div class="item-edit" ng-if="deleteClick !== undefined">\
<button class="button button-icon icon" ng-class="deleteIconClass" ng-click="deleteClick()" ion-stop-event="click"></button>\
</div>\
<a class="item-content" ng-href="{{ href }}" ng-transclude></a>\
<div class="item-drag" ng-if="reorderIconClass !== undefined">\
<button data-ionic-action="reorder" class="button button-icon icon" ng-class="reorderIconClass"></button>\
</div>\
<div class="item-options" ng-if="itemOptionButtons">\
<button ng-click="b.onTap(item, b)" ion-stop-event="click" class="button" ng-class="b.type" ng-repeat="b in itemOptionButtons" ng-bind="b.text"></button>\
</div>\
</div>',
link: function($scope, $element, $attr, list) {
if(!list) return;
var $parentScope = list.scope;
var $parentAttrs = list.attrs;
$attr.$observe('href', function(value) {
if(value) $scope.href = value.trim();
});
if(!$scope.itemType) {
$scope.itemType = $parentScope.itemType;
}
// Set this item's class, first from the item directive attr, and then the list attr if item not set
$element.addClass($scope.itemType || $parentScope.itemType);
$scope.itemClass = $scope.itemType;
// Decide if this item can do stuff, and follow a certain priority
// depending on where the value comes from
if(($attr.canDelete ? $scope.canDelete : $parentScope.canDelete) !== "false") {
if($attr.onDelete || $parentAttrs.onDelete) {
// only assign this method when we need to
// and use its existence to decide if the delete should show or not
$scope.deleteClick = function() {
if($attr.onDelete) {
// this item has an on-delete attribute
$scope.onDelete({ item: $scope.item });
} else if($parentAttrs.onDelete) {
// run the parent list's onDelete method
// if it doesn't exist nothing will happen
$parentScope.onDelete({ item: $scope.item });
}
};
// Set which icons to use for deleting
$scope.deleteIconClass = $scope.deleteIcon || $parentScope.deleteIcon || 'ion-minus-circled';
}
}
// set the reorder Icon Class only if the item or list set can-reorder="true"
if(($attr.canReorder ? $scope.canReorder : $parentScope.canReorder) === "true") {
$scope.reorderIconClass = $scope.reorderIcon || $parentScope.reorderIcon || 'ion-navicon';
}
// Set the option buttons which can be revealed by swiping to the left
// if canSwipe was set to false don't even bother
if(($attr.canSwipe ? $scope.canSwipe : $parentScope.canSwipe) !== "false") {
$scope.itemOptionButtons = $scope.optionButtons();
if(typeof $scope.itemOptionButtons === "undefined") {
$scope.itemOptionButtons = $parentScope.optionButtons();
}
}
}
};
}])
.directive('ionList', ['$timeout', function($timeout) {
return {
restrict: 'E',
replace: true,
transclude: true,
require: '^?$ionicScroll',
scope: {
itemType: '@',
canDelete: '@',
canReorder: '@',
canSwipe: '@',
showDelete: '=',
showReorder: '=',
onDelete: '&',
onReorder: '&',
optionButtons: '&',
deleteIcon: '@',
reorderIcon: '@'
},
template: '<div class="list" ng-class="{\'list-editing\': showDelete, \'list-reordering\': showReorder}" ng-transclude></div>',
controller: ['$scope', '$attrs', function($scope, $attrs) {
this.scope = $scope;
this.attrs = $attrs;
}],
link: function($scope, $element, $attr, ionicScrollCtrl) {
$scope.listView = new ionic.views.ListView({
el: $element[0],
listEl: $element[0].children[0],
scrollEl: ionicScrollCtrl && ionicScrollCtrl.element,
scrollView: ionicScrollCtrl && ionicScrollCtrl.scrollView,
onReorder: function(el, oldIndex, newIndex) {
$scope.$apply(function() {
$scope.onReorder({el: el, start: oldIndex, end: newIndex});
});
}
});
if($attr.animation) {
$element[0].classList.add($attr.animation);
}
var destroyShowReorderWatch = $scope.$watch('showReorder', function(val) {
if(val) {
$element[0].classList.add('item-options-hide');
} else if(val === false) {
// false checking is because it could be undefined
// if its undefined then we don't care to do anything
$timeout(function(){
$element[0].classList.remove('item-options-hide');
}, 250);
}
});
$scope.$on('$destroy', function () {
destroyShowReorderWatch();
});
}
};
}]);
})();