Skip to content

Commit

Permalink
Merge pull request #431 from marmelab/remove_angular_filters
Browse files Browse the repository at this point in the history
[RFR] Move orderElement angular filter to es6 class and remove angular enabled filter
  • Loading branch information
manuquentin committed May 15, 2015
2 parents e53e6ac + 5d1fe68 commit b68168a
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 81 deletions.
8 changes: 3 additions & 5 deletions src/javascripts/ng-admin/Crud/list/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
define(function () {
'use strict';

var ListController = function ($scope, $stateParams, $filter, $location, $anchorScroll, RetrieveQueries, progression, view, dataStore, totalItems) {
var ListController = function ($scope, $stateParams, $location, $anchorScroll, RetrieveQueries, progression, view, dataStore, totalItems) {
this.$scope = $scope;
this.$stateParams = $stateParams;
this.$filter = $filter;
this.$location = $location;
this.$anchorScroll = $anchorScroll;
this.RetrieveQueries = RetrieveQueries;
Expand All @@ -18,7 +17,7 @@ define(function () {
this.actions = view.actions();
this.batchActions = view.batchActions();
this.loadingPage = false;
this.filters = this.$filter('orderElement')(view.filters());
this.filters = view.filters();
this.hasFilters = Object.keys(this.filters).length > 0;
this.dataStore = dataStore;
this.fields = view.fields();
Expand Down Expand Up @@ -64,13 +63,12 @@ define(function () {
ListController.prototype.destroy = function () {
this.$scope = undefined;
this.$stateParams = undefined;
this.$filter = undefined;
this.$location = undefined;
this.$anchorScroll = undefined;
this.dataStore = undefined;
};

ListController.$inject = ['$scope', '$stateParams', '$filter', '$location', '$anchorScroll', 'RetrieveQueries', 'progression', 'view', 'dataStore', 'totalItems'];
ListController.$inject = ['$scope', '$stateParams', '$location', '$anchorScroll', 'RetrieveQueries', 'progression', 'view', 'dataStore', 'totalItems'];

return ListController;
});
2 changes: 0 additions & 2 deletions src/javascripts/ng-admin/Main/MainModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ define(function (require) {

MainModule.provider('NgAdminConfiguration', require('ng-admin/Main/component/provider/NgAdminConfiguration'));

MainModule.filter('enabled', require('ng-admin/Main/component/filter/Enabled'));
MainModule.filter('orderElement', require('ng-admin/Main/component/filter/OrderElement'));
MainModule.filter('stripTags', require('ng-admin/Main/component/filter/StripTags'));

MainModule.directive('maDashboardPanel', require('ng-admin/Main/component/directive/maDashboardPanel'));
Expand Down
24 changes: 0 additions & 24 deletions src/javascripts/ng-admin/Main/component/filter/Enabled.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ define(function () {

/**
* @param {$q} $q
* @param {$filter} $filter
* @param {$location} $location
* @param {RetrieveQueries} RetrieveQueries
* @param {Configuration} Configuration
* @param {AdminDescription} AdminDescription
*
* @constructor
*/
function PanelBuilder($q, $filter, $location, RetrieveQueries, Configuration, AdminDescription) {
function PanelBuilder($q, $location, RetrieveQueries, Configuration, AdminDescription) {
this.$q = $q;
this.$filter = $filter;
this.$location = $location;
this.RetrieveQueries = RetrieveQueries;
this.dataStore = AdminDescription.getDataStore();
Expand All @@ -35,9 +33,6 @@ define(function () {
self = this,
i;

dashboardViews = this.$filter('enabled')(dashboardViews);
dashboardViews = this.$filter('orderElement')(dashboardViews);

for (i in dashboardViews) {
dashboardView = dashboardViews[i];
promises.push(self.RetrieveQueries.getAll(dashboardView, 1, {}, dashboardView.getSortFieldName(), dashboardView.sortDir()));
Expand Down Expand Up @@ -73,7 +68,7 @@ define(function () {
});
};

PanelBuilder.$inject = ['$q', '$filter', '$location', 'RetrieveQueries', 'NgAdminConfiguration', 'AdminDescription'];
PanelBuilder.$inject = ['$q', '$location', 'RetrieveQueries', 'NgAdminConfiguration', 'AdminDescription'];

return PanelBuilder;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Main/view/dashboard-panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<ma-datagrid name="{{ viewName }}"
entries="entries"
fields="fields() | orderElement"
fields="fields()"
entity="entity()"
list-actions="false"
sort-field="sortField"
Expand Down
6 changes: 5 additions & 1 deletion src/javascripts/ng-admin/es6/lib/Application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Menu from './Menu/Menu';
import orderElement from "./Utils/orderElement";

class Application {
constructor(title='ng-admin', debug=true) {
Expand Down Expand Up @@ -28,7 +29,10 @@ class Application {
}

getViewsOfType(type) {
return this._entities.map(entity => entity.views[type]);
return orderElement.order(
this.entities.map(entity => entity.views[type])
.filter(view => view.enabled)
);
}

getRouteFor(entity, viewUrl, viewType, identifierValue, identifierName) {
Expand Down
12 changes: 12 additions & 0 deletions src/javascripts/ng-admin/es6/lib/Utils/orderElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
order: function (input) {
var results = [],
objectKey;

for (objectKey in input) {
results.push(input[objectKey]);
}

return results.sort((e1, e2) => e1.order() - e2.order());
}
};
3 changes: 2 additions & 1 deletion src/javascripts/ng-admin/es6/lib/View/ListView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import View from './View';
import orderElement from "../Utils/orderElement";

class ListView extends View {
constructor(name) {
Expand Down Expand Up @@ -94,7 +95,7 @@ class ListView extends View {
return this._filters;
}

this._filters = filters;
this._filters = orderElement.order(filters);

return this;
}
Expand Down
48 changes: 42 additions & 6 deletions src/javascripts/ng-admin/es6/tests/lib/ApplicationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,20 @@ describe('Application', function() {

});

describe('getViewsOfType', function() {
it('should return empty array if no entity set', function() {
var application = new Application();
describe('getViewsOfType', () => {
it('should return empty array if no entity set', () => {
let application = new Application();
assert.equal(0, application.getViewsOfType('dashboard').length);
});

it('should return only views of type', function() {
var application = new Application();
it('should return only views of type', () => {
let application = new Application();
application
.addEntity(new Entity('post'))
.addEntity(new Entity('comment'));

var views = application.getViewsOfType('DashboardView');
let views = application.getViewsOfType('DashboardView');

assert.equal(2, views.length);

assert.equal('post', views[0].entity.name());
Expand All @@ -116,6 +117,41 @@ describe('Application', function() {
assert.equal('comment', views[1].entity.name());
assert.equal('DashboardView', views[1].type);
});

it('should return only enabled views of type', () => {
let application = new Application();
let comment = new Entity('comment');
comment.views.DashboardView.disable();
application
.addEntity(new Entity('post'))
.addEntity(comment);

let views = application.getViewsOfType('DashboardView');

assert.equal(1, views.length);
assert.equal('post', views[0].entity.name());
});

it('should return ordered views of type', function() {
let application = new Application();
let [post, comment, tag] = [new Entity('post'), new Entity('comment'), new Entity('tag')];
post.views.DashboardView.order(2);
comment.views.DashboardView.order(1);
tag.views.DashboardView.order(3);

application
.addEntity(post)
.addEntity(comment)
.addEntity(tag);

let views = application.getViewsOfType('DashboardView');

assert.equal(3, views.length);

assert.equal('comment', views[0].entity.name());
assert.equal('post', views[1].entity.name());
assert.equal('tag', views[2].entity.name());
});
});

describe('layout', function() {
Expand Down
25 changes: 25 additions & 0 deletions src/javascripts/ng-admin/es6/tests/lib/Utils/orderElementTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require('chai').assert;

import orderElement from "../../../lib/Utils/orderElement";

describe('Menu', () => {

describe("order()", () => {

it('should order all elements', () => {
var elements = [
{order: function () { return 1; }, name: 'field1'},
{order: function () { return 0; }, name: 'field2'},
{order: function () { return 3; }, name: 'field3'}
];

var orderedElements = orderElement.order(elements);

// Check that elements are ordered
assert.equal(orderedElements.length, 3);
assert.equal(orderedElements[0].name, 'field2');
assert.equal(orderedElements[1].name, 'field1');
assert.equal(orderedElements[2].name, 'field3');
});
});
});
2 changes: 0 additions & 2 deletions src/javascripts/test/unit/Crud/list/maDatagridSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ define(function (require) {
Entry = require('ng-admin/es6/lib/Entry'),
Field = require('ng-admin/es6/lib/Field/Field'),
TextField = require('ng-admin/es6/lib/Field/TextField'),
orderElement = require('ng-admin/Main/component/filter/OrderElement'),
$compile,
scope,
directiveUsage = '<ma-datagrid name="{{ name }}" entries="entries" fields="fields" list-actions="listActions"' +
'entity="entity" next-page="nextPage" per-page="itemsPerPage" total-items="{{ totalItems }}" infinite-pagination="infinitePagination">' +
'</ma-datagrid>';

angular.module('testapp_Datagrid', [])
.filter('orderElement', orderElement)
.directive('maDatagrid', directive);
require('angular-mocks');

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,17 @@ define(function (require) {

function getPanelBuilder(dashboardViews, responses) {
var q = { all: function() { return mixins.buildPromise(responses); } };
var filter = function() { return function(a) { return a; } };
var Configuration = function() {
return {
getViewsOfType: function() {
return dashboardViews;
}
}
};
};
var location = { search: function() { return {}; } };
var retrieveQueries = { getAll: function() {} };
var AdminDescription = { getDataStore: function() { return new DataStore(); } };

return new PanelBuilder(q, filter, location, retrieveQueries, Configuration, AdminDescription);
return new PanelBuilder(q, location, retrieveQueries, Configuration, AdminDescription);
}
});

0 comments on commit b68168a

Please sign in to comment.