angular-table 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0300b2bec8da95a7c4f5bb5b8355f49dad326d18
4
- data.tar.gz: 68aee2657f079eddb91d28480908c2f258b9032d
3
+ metadata.gz: 8f34ccc9db8030436e8628fab32695362d239038
4
+ data.tar.gz: 9d8413dbb98bc5ebc55cefe4a7f3dccf7db22fe3
5
5
  SHA512:
6
- metadata.gz: 46a472c4ddb98017d75f3d3a8aa892f8c4db07edb7ae8c78fdf313ae78b3ef6df215513f7de83b714de8d937e419d8ff3367ed3393f4148dd0207ff378b45622
7
- data.tar.gz: a2a270d4de0e8d5b8aa2a7807fef71a3faffcbd6df600c87bfe91c3680f06613ea3412105d1b6df4f6c0d7e631d58bea10a90a4bcc17ede1220e35ef288c13f7
6
+ metadata.gz: 8fd879c37f7f56c7effd08e467e4e4e5cd0886d6647f60731e1b0f3388e4d6fbf622937e4c409babefc89aa2fd6ca91e3a973db89dc8775b3bc15af1b84af169
7
+ data.tar.gz: b744213c5cc55ee51bd544137a614a61c2c4e34d53327c1a39b6e3a8d94859424a61f5e5f9bc5001512c5ae3d9012cdf047d9a538427a871b40fead4ca6b5074
@@ -0,0 +1,343 @@
1
+ // author: Samuel Mueller
2
+ // version: 0.0.4
3
+ // license: MIT
4
+ // homepage: http://github.com/ssmm/angular-table
5
+ (function() {
6
+ angular.module("angular-table", []);
7
+
8
+ angular.module("angular-table").directive("atTable", [
9
+ "metaCollector", "setupFactory", function(metaCollector, setupFactory) {
10
+ var constructHeader, validateInput;
11
+
12
+ constructHeader = function(customHeaderMarkup, bodyDefinitions) {
13
+ var icon, td, th, title, tr, _i, _len;
14
+
15
+ tr = angular.element("<tr></tr>");
16
+ for (_i = 0, _len = bodyDefinitions.length; _i < _len; _i++) {
17
+ td = bodyDefinitions[_i];
18
+ th = angular.element("<th style='cursor: pointer;'></th>");
19
+ title = customHeaderMarkup[td.attribute] || td.title;
20
+ th.html("" + title);
21
+ if (td.sortable) {
22
+ th.attr("ng-click", "predicate = '" + td.attribute + "'; descending = !descending;");
23
+ icon = angular.element("<i style='margin-left: 10px;'></i>");
24
+ icon.attr("ng-class", "getSortIcon('" + td.attribute + "')");
25
+ th.append(icon);
26
+ }
27
+ th.attr("width", td.width);
28
+ tr.append(th);
29
+ }
30
+ return tr;
31
+ };
32
+ validateInput = function(attributes) {
33
+ if (attributes.pagination && attributes.list) {
34
+ throw "You can not specify a list if you have specified a pagination. The list defined for the pagnination will automatically be used.";
35
+ }
36
+ if (!attributes.pagination && !attributes.list) {
37
+ throw "Either a list or pagination must be specified.";
38
+ }
39
+ };
40
+ return {
41
+ restrict: "AC",
42
+ scope: true,
43
+ compile: function(element, attributes, transclude) {
44
+ var bodyDefinition, customHeaderMarkup, setup, tbody, tds, thead, tr;
45
+
46
+ validateInput(attributes);
47
+ thead = element.find("thead");
48
+ tbody = element.find("tbody");
49
+ tds = element.find("td");
50
+ if (thead[0]) {
51
+ customHeaderMarkup = metaCollector.collectCustomHeaderMarkup(thead);
52
+ bodyDefinition = metaCollector.collectBodyDefinition(tbody);
53
+ tr = thead.find("tr");
54
+ tr.remove();
55
+ thead.append(constructHeader(customHeaderMarkup, bodyDefinition.tds));
56
+ }
57
+ setup = setupFactory(attributes);
58
+ setup.compile(element, attributes, transclude);
59
+ return {
60
+ post: function($scope, $element, $attributes) {
61
+ if (bodyDefinition.initialSorting) {
62
+ $scope.predicate = bodyDefinition.initialSorting.predicate;
63
+ $scope.descending = bodyDefinition.initialSorting.direction === "desc";
64
+ }
65
+ $scope.getSortIcon = function(predicate) {
66
+ if (predicate !== $scope.predicate) {
67
+ return "icon-minus";
68
+ }
69
+ if ($scope.descending) {
70
+ return "icon-chevron-down";
71
+ } else {
72
+ return "icon-chevron-up";
73
+ }
74
+ };
75
+ return setup.link($scope, $element, $attributes);
76
+ }
77
+ };
78
+ }
79
+ };
80
+ }
81
+ ]);
82
+
83
+ angular.module("angular-table").directive("atImplicit", [
84
+ function() {
85
+ return {
86
+ restrict: "AC",
87
+ compile: function(element, attributes, transclude) {
88
+ var attribute;
89
+
90
+ attribute = element.attr("attribute");
91
+ if (!attribute) {
92
+ throw "at-implicit specified without attribute: " + (element.html());
93
+ }
94
+ return element.append("{{item." + attribute + "}}");
95
+ }
96
+ };
97
+ }
98
+ ]);
99
+
100
+ angular.module("angular-table").directive("atPagination", [
101
+ function() {
102
+ return {
103
+ replace: true,
104
+ restrict: "E",
105
+ template: " <div class='pagination' style='margin: 0px;'> <ul> <li ng-class='{disabled: currentPage <= 0}'> <a href='' ng-click='goToPage(currentPage - 1)'>&laquo;</a> </li> <li ng-class='{active: currentPage == page}' ng-repeat='page in pages'> <a href='' ng-click='goToPage(page)'>{{page + 1}}</a> </li> <li ng-class='{disabled: currentPage >= numberOfPages - 1}'> <a href='' ng-click='goToPage(currentPage + 1); normalize()'>&raquo;</a> </li> </ul> </div>",
106
+ scope: {
107
+ itemsPerPage: "@",
108
+ instance: "=",
109
+ list: "="
110
+ },
111
+ link: function($scope, $element, $attributes) {
112
+ $scope.instance = $scope;
113
+ $scope.currentPage = 0;
114
+ $scope.update = function() {
115
+ var x;
116
+
117
+ $scope.currentPage = 0;
118
+ if ($scope.list) {
119
+ if ($scope.list.length > 0) {
120
+ $scope.numberOfPages = Math.ceil($scope.list.length / $scope.itemsPerPage);
121
+ $scope.pages = (function() {
122
+ var _i, _ref, _results;
123
+
124
+ _results = [];
125
+ for (x = _i = 0, _ref = $scope.numberOfPages - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; x = 0 <= _ref ? ++_i : --_i) {
126
+ _results.push(x);
127
+ }
128
+ return _results;
129
+ })();
130
+ } else {
131
+ $scope.numberOfPages = 1;
132
+ $scope.pages = [0];
133
+ }
134
+ return $scope.list = $scope.list;
135
+ }
136
+ };
137
+ $scope.fromPage = function() {
138
+ if ($scope.list) {
139
+ return $scope.itemsPerPage * $scope.currentPage - $scope.list.length;
140
+ }
141
+ };
142
+ $scope.getFillerArray = function() {
143
+ var fillerLength, itemCountOnLastPage, x, _i, _ref, _ref1, _results;
144
+
145
+ if ($scope.currentPage === $scope.numberOfPages - 1) {
146
+ itemCountOnLastPage = $scope.list.length % $scope.itemsPerPage;
147
+ if (itemCountOnLastPage !== 0 || $scope.list.length === 0) {
148
+ fillerLength = $scope.itemsPerPage - itemCountOnLastPage - 1;
149
+ _results = [];
150
+ for (x = _i = _ref = $scope.list.length, _ref1 = $scope.list.length + fillerLength; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; x = _ref <= _ref1 ? ++_i : --_i) {
151
+ _results.push(x);
152
+ }
153
+ return _results;
154
+ } else {
155
+ return [];
156
+ }
157
+ }
158
+ };
159
+ $scope.goToPage = function(page) {
160
+ page = Math.max(0, page);
161
+ page = Math.min($scope.numberOfPages - 1, page);
162
+ return $scope.currentPage = page;
163
+ };
164
+ $scope.update();
165
+ return $scope.$watch("list", function() {
166
+ return $scope.update();
167
+ });
168
+ }
169
+ };
170
+ }
171
+ ]);
172
+
173
+ angular.module("angular-table").service("metaCollector", [
174
+ function() {
175
+ var capitaliseFirstLetter, extractWidth, getInitialSortDirection, isSortable;
176
+
177
+ capitaliseFirstLetter = function(string) {
178
+ if (string) {
179
+ return string.charAt(0).toUpperCase() + string.slice(1);
180
+ } else {
181
+ return "";
182
+ }
183
+ };
184
+ extractWidth = function(classes) {
185
+ var width;
186
+
187
+ width = /([0-9]+px)/i.exec(classes);
188
+ if (width) {
189
+ return width[0];
190
+ } else {
191
+ return "";
192
+ }
193
+ };
194
+ isSortable = function(classes) {
195
+ var sortable;
196
+
197
+ sortable = /(sortable)/i.exec(classes);
198
+ if (sortable) {
199
+ return true;
200
+ } else {
201
+ return false;
202
+ }
203
+ };
204
+ getInitialSortDirection = function(td) {
205
+ var initialSorting;
206
+
207
+ initialSorting = td.attr("initial-sorting");
208
+ if (initialSorting) {
209
+ if (initialSorting === "asc" || initialSorting === "desc") {
210
+ return initialSorting;
211
+ }
212
+ throw "Invalid value for initial-sorting: " + initialSorting + ". Allowed values are 'asc' or 'desc'.";
213
+ }
214
+ return void 0;
215
+ };
216
+ return {
217
+ collectCustomHeaderMarkup: function(thead) {
218
+ var customHeaderMarkup, th, tr, _i, _len, _ref;
219
+
220
+ customHeaderMarkup = {};
221
+ tr = thead.find("tr");
222
+ _ref = tr.find("th");
223
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
224
+ th = _ref[_i];
225
+ th = angular.element(th);
226
+ customHeaderMarkup[th.attr("attribute")] = th.html();
227
+ }
228
+ return customHeaderMarkup;
229
+ },
230
+ collectBodyDefinition: function(tbody) {
231
+ var attribute, bodyDefinition, initialSortDirection, sortable, td, title, width, _i, _len, _ref;
232
+
233
+ bodyDefinition = {};
234
+ bodyDefinition.tds = [];
235
+ bodyDefinition.initialSorting = void 0;
236
+ _ref = tbody.find("td");
237
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
238
+ td = _ref[_i];
239
+ td = angular.element(td);
240
+ attribute = td.attr("attribute");
241
+ title = td.attr("title") || capitaliseFirstLetter(td.attr("attribute"));
242
+ sortable = td[0].attributes.sortable || isSortable(td.attr("class"));
243
+ width = extractWidth(td.attr("class"));
244
+ bodyDefinition.tds.push({
245
+ attribute: attribute,
246
+ title: title,
247
+ sortable: sortable,
248
+ width: width
249
+ });
250
+ initialSortDirection = getInitialSortDirection(td);
251
+ if (initialSortDirection) {
252
+ if (!attribute) {
253
+ throw "initial-sorting specified without attribute.";
254
+ }
255
+ bodyDefinition.initialSorting = {};
256
+ bodyDefinition.initialSorting.direction = initialSortDirection;
257
+ bodyDefinition.initialSorting.predicate = attribute;
258
+ }
259
+ }
260
+ return bodyDefinition;
261
+ }
262
+ };
263
+ }
264
+ ]);
265
+
266
+ angular.module("angular-table").factory("setupFactory", [
267
+ function() {
268
+ var PaginationSetup, StandardSetup, limitToExpression, orderByExpression, setupTr;
269
+
270
+ orderByExpression = "| orderBy:predicate:descending";
271
+ limitToExpression = "| limitTo:fromPage() | limitTo:toPage()";
272
+ setupTr = function(element, repeatString) {
273
+ var tbody, tr;
274
+
275
+ tbody = element.find("tbody");
276
+ tr = tbody.find("tr");
277
+ tr.attr("ng-repeat", repeatString);
278
+ return tbody;
279
+ };
280
+ StandardSetup = function(attributes) {
281
+ var repeatString;
282
+
283
+ repeatString = "item in " + attributes.list + " " + orderByExpression;
284
+ this.compile = function(element, attributes, transclude) {
285
+ return setupTr(element, repeatString);
286
+ };
287
+ this.link = function() {};
288
+ };
289
+ PaginationSetup = function(attributes) {
290
+ var repeatString, sortContext;
291
+
292
+ sortContext = attributes.sortContext || "global";
293
+ if (sortContext === "global") {
294
+ repeatString = "item in " + attributes.pagination + ".list " + orderByExpression + " " + limitToExpression;
295
+ } else if (sortContext === "page") {
296
+ repeatString = "item in " + attributes.pagination + ".list " + limitToExpression + " " + orderByExpression + " ";
297
+ } else {
298
+ throw "Invalid sort-context: " + sortContext + ".";
299
+ }
300
+ this.compile = function(element, attributes, transclude) {
301
+ var fillerTr, tbody, td, tdString, tds, _i, _len;
302
+
303
+ tbody = setupTr(element, repeatString);
304
+ if (typeof attributes.fillLastPage !== "undefined") {
305
+ tds = element.find("td");
306
+ tdString = "";
307
+ for (_i = 0, _len = tds.length; _i < _len; _i++) {
308
+ td = tds[_i];
309
+ tdString += "<td>&nbsp;</td>";
310
+ }
311
+ fillerTr = angular.element("<tr>" + tdString + "</tr>");
312
+ fillerTr.attr("ng-repeat", "item in " + attributes.pagination + ".getFillerArray() ");
313
+ return tbody.append(fillerTr);
314
+ }
315
+ };
316
+ this.link = function($scope, $element, $attributes) {
317
+ var paginationName;
318
+
319
+ paginationName = attributes.pagination;
320
+ $scope.fromPage = function() {
321
+ if ($scope[paginationName]) {
322
+ return $scope[paginationName].fromPage();
323
+ }
324
+ };
325
+ return $scope.toPage = function() {
326
+ if ($scope[paginationName]) {
327
+ return $scope[paginationName].itemsPerPage;
328
+ }
329
+ };
330
+ };
331
+ };
332
+ return function(attributes) {
333
+ if (attributes.list) {
334
+ return new StandardSetup(attributes);
335
+ }
336
+ if (attributes.pagination) {
337
+ return new PaginationSetup(attributes);
338
+ }
339
+ };
340
+ }
341
+ ]);
342
+
343
+ }).call(this);
@@ -1,3 +1,3 @@
1
1
  module AngularTable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Mueller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-22 00:00:00.000000000 Z
11
+ date: 2013-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,7 +48,7 @@ extra_rdoc_files: []
48
48
  files:
49
49
  - lib/angular-table/version.rb
50
50
  - lib/angular-table.rb
51
- - vendor/assets/javascripts/angular-table.js.coffee
51
+ - app/assets/javascripts/angular-table.js
52
52
  homepage: https://github.com/ssmm/angular-table
53
53
  licenses:
54
54
  - MIT
@@ -1,223 +0,0 @@
1
- # author: Samuel Mueller http://github.com/ssmm
2
-
3
- angular.module "angular-table", []
4
-
5
- angular.module("angular-table").service "attributeExtractor", () ->
6
- {
7
- extractWidth: (classes) ->
8
- width = /([0-9]+px)/i.exec classes
9
- if width then width[0] else ""
10
-
11
- isSortable: (classes) ->
12
- sortable = /(sortable)/i.exec classes
13
- if sortable then true else false
14
-
15
- extractTitle: (td) ->
16
- td.attr("title") || td.attr("attribute")
17
-
18
- extractAttribute: (td) ->
19
- td.attr("attribute")
20
-
21
- }
22
-
23
- angular.module("angular-table").directive "atTable", ["attributeExtractor", (attributeExtractor) ->
24
-
25
- capitaliseFirstLetter = (string) ->
26
- if string then string.charAt(0).toUpperCase() + string.slice(1) else ""
27
-
28
-
29
- constructHeader = (thead, tds) ->
30
- tr = thead.find "tr"
31
- existingThMarkup = {}
32
- for th in tr.find "th"
33
- th = angular.element(th)
34
- existingThMarkup[th.attr("attribute")] = th.html()
35
-
36
- tr.remove()
37
- tr = angular.element("<tr></tr>")
38
-
39
- for td in tds
40
- td = angular.element(td)
41
- attribute = attributeExtractor.extractAttribute(td)
42
- th = angular.element("<th style='cursor: pointer;'></th>")
43
- title = existingThMarkup[attribute] || capitaliseFirstLetter(attributeExtractor.extractTitle(td))
44
- th.html("#{title}")
45
-
46
- sortable = td[0].attributes.sortable || attributeExtractor.isSortable(td.attr("class"))
47
- if sortable
48
- th.attr("ng-click", "predicate = '#{attribute}'; descending = !descending;")
49
- icon = angular.element("<i style='margin-left: 10px;'></i>")
50
- icon.attr("ng-class", "getSortIcon('#{attribute}')")
51
- th.append(icon)
52
-
53
- width = attributeExtractor.extractWidth(td.attr("class"))
54
- th.attr("width", width)
55
- tr.append(th)
56
-
57
- thead.append tr
58
-
59
- validateInput = (attributes) ->
60
- if attributes.pagination and attributes.list
61
- throw "You can not specify a list if you have specified a pagination. The list defined for the pagnination will automatically be used."
62
- if not attributes.pagination and not attributes.list
63
- throw "Either a list or pagination must be specified."
64
-
65
- setupTr = (element, repeatString) ->
66
- tbody = element.find "tbody"
67
- tr = tbody.find "tr"
68
- tr.attr("ng-repeat", repeatString)
69
- tbody
70
-
71
-
72
- orderByExpression = "| orderBy:predicate:descending"
73
- limitToExpression = "| limitTo:fromPage() | limitTo:toPage()"
74
-
75
- StandardSetup = (attributes) ->
76
- @repeatString = "item in #{attributes.list} #{orderByExpression}"
77
- @compile = (element, attributes, transclude) ->
78
- setupTr element, @repeatString
79
-
80
- @link = () ->
81
- return
82
-
83
- PaginationSetup = (attributes) ->
84
-
85
- sortContext = attributes.sortContext || "global"
86
-
87
- if sortContext == "global"
88
- @repeatString = "item in #{attributes.pagination}.list #{orderByExpression} #{limitToExpression}"
89
- else if sortContext == "page"
90
- @repeatString = "item in #{attributes.pagination}.list #{limitToExpression} #{orderByExpression} "
91
- else
92
- throw "Invalid sort-context: #{sortContext}."
93
-
94
- @compile = (element, attributes, transclude) ->
95
- tbody = setupTr element, @repeatString
96
-
97
- if typeof attributes.fillLastPage != "undefined"
98
- tds = element.find("td")
99
- tdString = ""
100
- for td in tds
101
- tdString += "<td>&nbsp;</td>"
102
-
103
- fillerTr = angular.element("<tr>#{tdString}</tr>")
104
- fillerTr.attr("ng-repeat", "item in #{attributes.pagination}.getFillerArray() ")
105
-
106
- tbody.append(fillerTr)
107
-
108
- @link = ($scope, $element, $attributes) ->
109
- paginationName = attributes.pagination
110
- $scope.fromPage = () ->
111
- if $scope[paginationName] then $scope[paginationName].fromPage()
112
-
113
- $scope.toPage = () ->
114
- if $scope[paginationName] then $scope[paginationName].itemsPerPage
115
-
116
- return
117
-
118
- createSetup = (attributes) ->
119
- validateInput attributes
120
- if attributes.list
121
- return new StandardSetup(attributes)
122
- if attributes.pagination
123
- return new PaginationSetup(attributes)
124
- return
125
-
126
- {
127
- restrict: "AC"
128
- scope: true
129
- compile: (element, attributes, transclude) ->
130
- setup = createSetup attributes
131
-
132
- thead = element.find "thead"
133
- tds = element.find "td"
134
- constructHeader(thead, tds) if thead[0]
135
-
136
- setup.compile(element, attributes, transclude)
137
- {
138
- post: ($scope, $element, $attributes) ->
139
-
140
- $scope.getSortIcon = (predicate) ->
141
- return "icon-minus" if predicate != $scope.predicate
142
- if $scope.descending then "icon-chevron-down" else "icon-chevron-up"
143
-
144
- setup.link($scope, $element, $attributes)
145
- }
146
- }
147
- ]
148
-
149
- angular.module("angular-table").directive "atImplicit", ["attributeExtractor", (attributeExtractor) ->
150
- {
151
- restrict: "AC"
152
- compile: (element, attributes, transclude) ->
153
- attribute = attributeExtractor.extractAttribute element
154
- element.append "{{item.#{attribute}}}"
155
- }
156
- ]
157
-
158
- angular.module("angular-table").directive "atPagination", ["attributeExtractor", (attributeExtractor) ->
159
- {
160
- replace: true
161
- restrict: "E"
162
- template: "
163
- <div class='pagination' style='margin: 0px;'>
164
- <ul>
165
- <li ng-class='{disabled: currentPage <= 0}'>
166
- <a href='' ng-click='goToPage(currentPage - 1)'>&laquo;</a>
167
- </li>
168
- <li ng-class='{active: currentPage == page}' ng-repeat='page in pages'>
169
- <a href='' ng-click='goToPage(page)'>{{page + 1}}</a>
170
- </li>
171
- <li ng-class='{disabled: currentPage >= numberOfPages - 1}'>
172
- <a href='' ng-click='goToPage(currentPage + 1); normalize()'>&raquo;</a>
173
- </li>
174
- </ul>
175
- </div>"
176
- scope: {
177
- itemsPerPage: "@"
178
- instance: "="
179
- list: "="
180
- }
181
- link: ($scope, $element, $attributes) ->
182
-
183
- $scope.instance = $scope
184
- $scope.currentPage = 0
185
-
186
- $scope.update = () ->
187
- $scope.currentPage = 0
188
- if $scope.list
189
- if $scope.list.length > 0
190
- $scope.numberOfPages = Math.ceil($scope.list.length / $scope.itemsPerPage)
191
- $scope.pages = for x in [0..($scope.numberOfPages - 1)]
192
- x
193
- else
194
- $scope.numberOfPages = 0
195
- $scope.pages = []
196
- $scope.list = $scope.list
197
-
198
- $scope.fromPage = () ->
199
- if $scope.list
200
- $scope.itemsPerPage * $scope.currentPage - $scope.list.length
201
-
202
- $scope.getFillerArray = () ->
203
- if $scope.currentPage == $scope.numberOfPages - 1
204
- itemCountOnLastPage = $scope.list.length % $scope.itemsPerPage
205
- if itemCountOnLastPage != 0
206
- fillerLength = $scope.itemsPerPage - itemCountOnLastPage - 1
207
- x for x in [($scope.list.length)..($scope.list.length + fillerLength)]
208
- else
209
- []
210
-
211
- $scope.goToPage = (page) ->
212
- page = Math.max(0, page)
213
- page = Math.min($scope.numberOfPages - 1, page)
214
-
215
- $scope.currentPage = page
216
-
217
- $scope.update()
218
-
219
- $scope.$watch "list", () ->
220
- $scope.update()
221
-
222
- }
223
- ]