Skip to content

Commit 57df36f

Browse files
author
Trinh Ho
committed
Fix wysiwyg-export plugin.
Add jasmine specs for plugins. Fix plugin initialization when passing in function literal.. In order for "getInstanceType" to work correctly, function literal must be declared in form "function SomeClass () {}".
1 parent 4a5c93d commit 57df36f

9 files changed

+124
-19
lines changed

GruntFile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
watch: {
130130
// Run unit test with karma
131131
karma: {
132-
files: ['build/ng-grid.debug.js', 'test/unit/**/*.js'],
132+
files: ['build/ng-grid.debug.js', 'test/unit/**/*.js', 'plugins/*.js'],
133133
tasks: ['karma:watch:run']
134134
},
135135
// Auto-build ng-grid.debug.js when source files change

config/karma.conf.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ files = [
1212

1313
// App code
1414
'build/ng-grid.debug.js',
15+
16+
// Plugins
17+
'plugins/*.js',
1518

1619
// Test specs
1720
'test/unit/**/*.js'

plugins/ng-grid-csv-export.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// download from a data-uri link
66
//
77
// Notes: This has not been adequately tested and is very much a proof of concept at this point
8-
ngGridCsvExportPlugin = function(opts) {
8+
function ngGridCsvExportPlugin (opts) {
99
var self = this;
1010
self.grid = null;
1111
self.scope = null;

plugins/ng-grid-flexible-height.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ngGridFlexibleHeightPlugin = function (opts) {
1+
function ngGridFlexibleHeightPlugin (opts) {
22
var self = this;
33
self.grid = null;
44
self.scope = null;

plugins/ng-grid-layout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ngGridLayoutPlugin = function() {
1+
function ngGridLayoutPlugin () {
22
var self = this;
33
this.grid = null;
44
this.scope = null;

plugins/ng-grid-reorderable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
DO NOT USE THIS PLUGIN. IT IS ONLY AN EXAMPLE FOR INSTRUCTIONAL PURPOSES.
33
*/
44

5-
ngGridReorderable = function(options) {
5+
function ngGridReorderable (options) {
66
var defaults = {
77
enableHeader: true,
88
enableRow: true

plugins/ng-grid-wysiwyg-export.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
ngGridWYSIWYGPlugin = function (filter) {
1+
function ngGridWYSIWYGPlugin (filter) {
22
var self = this;
33
self.grid = null;
44
self.scope = null;
5+
self.services = null;
56

6-
self.init = function (scope, grid) {
7+
self.init = function (scope, grid, services) {
78
self.grid = grid;
89
self.scope = scope;
10+
self.services = services;
911
};
1012

1113
self.export = function () {
@@ -15,27 +17,23 @@
1517
gridWidth: self.scope.totalRowWidth(),
1618
data: []
1719
};
20+
1821
angular.forEach(self.scope.columns, function (col) {
1922
if (col.visible) {
2023
ret.columns.push(col.displayName);
2124
ret.columnWidths.push(col.width);
2225
}
2326
});
24-
angular.forEach(self.grid.filteredRows, function (item) {
27+
angular.forEach(self.grid.filteredRows, function (row) {
28+
var item = row.entity;
2529
angular.forEach(self.scope.columns, function (col) {
2630
if (col.visible) {
27-
var obj = ng.utils.evalProperty(item, col.field);
28-
var val = null;
29-
if (col.cellFilter) {
30-
val = filter(col.cellFilter)(obj);
31-
} else {
32-
val = obj;
33-
}
31+
var obj = self.services.UtilityService.evalProperty(item, col.field);
32+
var val = col.cellFilter && filter ? filter(col.cellFilter)(obj) : obj;
3433
ret.data.push(val ? val.toString() : '');
3534
}
3635
});
3736
});
3837
return ret;
3938
};
40-
return self;
41-
};
39+
}

src/directives/ng-grid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
options.gridId = grid.gridId;
135135
options.ngGrid = grid;
136136
options.$gridScope = $scope;
137-
options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService };
137+
options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService, UtilityService: $utils };
138138
$scope.$on('ngGridEventDigestGrid', function(){
139139
domUtilityService.digest($scope.$parent);
140140
});
@@ -149,7 +149,7 @@
149149
//initialize plugins.
150150
angular.forEach(options.plugins, function (p) {
151151
if (typeof p === 'function') {
152-
p = p.call(this);
152+
p = new p(); //If p is a function, then we assume it is a class.
153153
}
154154
p.init($scope.$new(), grid, options.$gridServices);
155155
options.plugins[$utils.getInstanceType(p)] = p;

test/unit/pluginsSpec.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
3+
/* jasmine specs for plugins go here */
4+
5+
describe('plugins', function () {
6+
var elm, scope;
7+
8+
var initalizeHelper = function(gridOptions) {
9+
return inject(function($rootScope, $compile) {
10+
elm = angular.element(
11+
'<div ng-grid="gridOptions" style="width: 1000px; height: 1000px"></div>'
12+
);
13+
14+
scope = $rootScope;
15+
scope.myData = [{ name: "Moroni", age: 50 },
16+
{ name: "Tiancum", age: 43 },
17+
{ name: "Jacob", age: 27 },
18+
{ name: "Nephi", age: 29 },
19+
{ name: "Enos", age: 34 }];
20+
21+
scope.gridOptions = $.extend(true, { data: 'myData' }, gridOptions);
22+
23+
$compile(elm)(scope);
24+
25+
scope.$digest();
26+
});
27+
};
28+
// Load the ngGrid module
29+
beforeEach(module('ngGrid'));
30+
31+
describe('ng-grid-wysiwyg-export', function () {
32+
describe('initialization', function () {
33+
var testData = function(data) {
34+
expect(data.columns.length).toBe(2);
35+
expect(data.columns[0]).toBe('name');
36+
expect(data.columns[1]).toBe('age');
37+
38+
expect(data.data.length / data.columns.length).toBe(scope.myData.length);
39+
expect(data.data.length % data.columns.length).toBe(0);
40+
};
41+
42+
describe('function-literal', function() {
43+
beforeEach(initalizeHelper({ plugins: [ngGridWYSIWYGPlugin] }));
44+
45+
it('should work if the plugin being passed in is a function literal', function() {
46+
testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
47+
});
48+
});
49+
50+
describe('instance', function() {
51+
beforeEach(initalizeHelper({ plugins: [new ngGridWYSIWYGPlugin()] }));
52+
53+
it('should work if the plugin being passed in is an instance', function() {
54+
testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
55+
});
56+
});
57+
});
58+
//describe('filtering', function () {
59+
// var testData = function (data) {
60+
// expect(data.columns.length).toBe(2);
61+
// expect(data.columns[0]).toBe('name');
62+
// expect(data.columns[1]).toBe('age');
63+
64+
// expect(data.data.length).toBe(2);
65+
// };
66+
67+
// describe('function-literal', function () {
68+
// beforeEach(initalizeHelper({
69+
// plugins: [ngGridWYSIWYGPlugin],
70+
// filterOptions: { filterText: '', useExternalFilter: false },
71+
// }));
72+
73+
// it('should work if the plugin being passed in is a function literal', function () {
74+
// testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
75+
// });
76+
// });
77+
78+
// describe('instance', function () {
79+
// beforeEach(initalizeHelper({
80+
// plugins: [new ngGridWYSIWYGPlugin()],
81+
// filterOptions: { filterText: 'Jacob', useExternalFilter: false },
82+
// }));
83+
84+
// it('should work if the plugin being passed in is an instance', function () {
85+
// var flag = false;
86+
// runs(function () {
87+
// scope.$digest();
88+
// setTimeout(function () {
89+
// flag = true;
90+
// scope.$digest();
91+
// }, 500);
92+
// });
93+
// waitsFor(function () {
94+
// return flag;
95+
// });
96+
// runs(function () {
97+
// testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
98+
// });
99+
// });
100+
// });
101+
//});
102+
});
103+
104+
});

0 commit comments

Comments
 (0)