summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <DeepDiver1975@users.noreply.github.com>2016-04-21 07:51:56 +0200
committerThomas Müller <DeepDiver1975@users.noreply.github.com>2016-04-21 07:51:56 +0200
commitd2f31a99881c447fe584e1d4d69ce82ed0a300e7 (patch)
treebb2d1ff2e646f00282a6f7351031feb618aa00a6
parent7c107314b1ee068b04f3e55eb62f16af545abe2a (diff)
parent7005470a7f08f26464f7bff053a210b734de8622 (diff)
Merge pull request #378 from owncloud/locale-orderby
Sort contact list with respect to the locale
-rw-r--r--js/filters/localeOrderBy_filter.js42
-rw-r--r--js/tests/filters/localeOrderBy_filter.js18
-rw-r--r--templates/contactList.html2
3 files changed, 61 insertions, 1 deletions
diff --git a/js/filters/localeOrderBy_filter.js b/js/filters/localeOrderBy_filter.js
new file mode 100644
index 00000000..ecb2ad73
--- /dev/null
+++ b/js/filters/localeOrderBy_filter.js
@@ -0,0 +1,42 @@
+angular.module('contactsApp')
+.filter('localeOrderBy', [function () {
+ return function (array, sortPredicate, reverseOrder) {
+ if (!Array.isArray(array)) return array;
+ if (!sortPredicate) return array;
+
+ var isString = function (value) {
+ return (typeof value === 'string');
+ };
+
+ var isNumber = function (value) {
+ return (typeof value === 'number');
+ };
+
+ var isBoolean = function (value) {
+ return (typeof value === 'boolean');
+ };
+
+ var arrayCopy = [];
+ angular.forEach(array, function (item) {
+ arrayCopy.push(item);
+ });
+
+ arrayCopy.sort(function (a, b) {
+ var valueA = a[sortPredicate];
+ var valueB = b[sortPredicate];
+
+ if (isString(valueA)) {
+ return !reverseOrder ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
+ }
+
+ if (isNumber(valueA) || isBoolean(valueA)) {
+ return !reverseOrder ? valueA - valueB : valueB - valueA;
+ }
+
+ return 0;
+ });
+
+ return arrayCopy;
+ };
+}]);
+
diff --git a/js/tests/filters/localeOrderBy_filter.js b/js/tests/filters/localeOrderBy_filter.js
new file mode 100644
index 00000000..697db5a9
--- /dev/null
+++ b/js/tests/filters/localeOrderBy_filter.js
@@ -0,0 +1,18 @@
+describe('localeOrderBy filter', function() {
+
+ var $filter;
+
+ beforeEach(module('contactsApp'));
+
+ beforeEach(inject(function(_$filter_){
+ $filter = _$filter_;
+ }));
+
+ it('should return the array properly sorted', function() {
+ var localeOrderBy = $filter('localeOrderBy');
+ var sorted = $filter('localeOrderBy')([{name:'Mb'}, {name:'Mab'}, {name:'Máa'}], 'name', false);
+ var expected = [{name:'Máa'}, {name:'Mab'}, {name:'Mb'}];
+
+ expect(sorted).to.deep.equal(expected);
+ });
+});
diff --git a/templates/contactList.html b/templates/contactList.html
index f3b0150f..5245133e 100644
--- a/templates/contactList.html
+++ b/templates/contactList.html
@@ -2,7 +2,7 @@
<button ng-show="!ctrl.loading && !ctrl.invalid" class="app-content-list-button" id="new-contact-button"
type="button" name="button" ng-click="ctrl.createContact()">{{ctrl.t.addContact}}</button>
<div class="app-content-list-item"
- ng-repeat="contact in ctrl.contactList = (ctrl.contacts | contactGroupFilter:ctrl.routeParams.gid | orderBy:'displayName()' | filter:query) track by contact.uid()"
+ ng-repeat="contact in ctrl.contactList = (ctrl.contacts | contactGroupFilter:ctrl.routeParams.gid | localeOrderBy:'displayName()' | filter:query) track by contact.uid()"
contact data="contact"
ng-click="setSelected(contact.uid())"
ng-class="{active: contact.uid() === ctrl.getSelectedId()}">