forked from dv336699/ng-isotope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-isotope.js
148 lines (131 loc) · 6.1 KB
/
ng-isotope.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
/**
* ng-isotope v0.0.2
* AngularJS directives for Isotope by Metafizzy
* http://isotope.metafizzy.co
* Copyright 2014 Diego Vieira <diego@protos.inf.br>
*/
'use strict';
angular.module('ng-isotope', [])
.directive('isotopeGrid', function ($timeout) {
return {
restrict: 'A',
scope: {
items: '=isotopeGrid',
sortBy: '=isotopeSort',
sortData: '=isotopeSortData'
},
link: function (scope, element, attrs) {
var options = {
itemSelector: attrs.isotopeItemSelector || 'div',
layoutMode: attrs.isotopeLayoutMode || 'fitRows',
getSortData: scope.sortData || {}
};
element.isotope(options);
scope.$watch('items', function () {
$timeout(function () {
element.isotope('reloadItems').isotope(options);
});
}, true);
if (typeof attrs.isotopeSortEvent !== 'undefined') {
scope.$on(attrs.isotopeSortEvent, function (data, sort) {
// @todo search direction
options.sortBy = sort.sort || 'original-order';
element.isotope(options);
});
}
if (typeof attrs.isotopeFilterEvent !== 'undefined') {
scope.$on(attrs.isotopeFilterEvent, function (data, filter) {
options.filter = filter.filter;
element.isotope(options);
});
}
scope.$watch('items', function () {
$timeout(function () {
element.isotope('reloadItems').isotope(options);
});
}, true);
if (typeof attrs.isotopeReloadEvent !== 'undefined') {
scope.$on(attrs.isotopeReloadEvent, function (data, filter) {
$timeout(function () {
element.isotope('reloadItems').isotope(options);
});
});
}
}
};
})
.directive('isotopeSortBy', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind('click', function () {
var elm = angular.element(element);
var parent = elm.parent();
parent.find('.active').removeClass('active');
elm.addClass('active');
var sortEvent = parent.attr('isotope-sort-event');
if (typeof sortEvent !== 'undefined') {
scope.$broadcast(sortEvent, { sort: elm.attr('isotope-sort-by') });
}
});
}
};
})
.directive('isotopeFilter', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
// @todo make this more user friendly
var elm = angular.element(element);
var elementType = elm.prop('tagName');
if (elementType == 'DIV') {
elm.find('*[isotope-filter-by]').bind('click', function (i, e) {
angular.element(this).parent().find('.active').removeClass('active');
angular.element(this).addClass('active');
var filter = angular.element(this).attr('isotope-filter-by');
var group = attrs.isotopeFilterGroup;
if (typeof group !== 'undefined') {
var filterArray = [];
angular.element('body').find('*[isotope-filter-group=' + group + ']').each(function (i, e) {
angular.element(e).find('*[isotope-filter-by]').each(function (i, e) {
if (angular.element(e).hasClass('active')) {
var selectedFilter = angular.element(e).attr('isotope-filter-by');
if (selectedFilter !== '*') {
filterArray.push(selectedFilter);
}
}
});
});
if (filterArray.length > 0) {
filter = filterArray.join('');
}
}
if (typeof attrs.isotopeFilterEvent !== 'undefined') {
scope.$broadcast(attrs.isotopeFilterEvent, { filter: filter });
}
});
}
if (elementType == 'SELECT') {
element.bind('change', function () {
var filter = elm.find('option:selected').attr('isotope-filter-by');
var group = attrs.isotopeFilterGroup;
if (typeof group !== 'undefined') {
var filterArray = [];
angular.element('body').find('*[isotope-filter-group=' + group + ']').each(function (i, e) {
var selectedFilter = angular.element(e).find('option:selected').attr('isotope-filter-by');
if (selectedFilter !== '*') {
filterArray.push(selectedFilter);
}
});
if (filterArray.length > 0) {
filter = filterArray.join('');
}
}
if (typeof attrs.isotopeFilterEvent !== 'undefined') {
scope.$broadcast(attrs.isotopeFilterEvent, { filter: filter });
}
});
}
}
};
});