Permalink
Browse files

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 () {}".
  • Loading branch information...
1 parent 4a5c93d commit 57df36fe670048b74422080c4f8e85aaaf05ea47 @ineedfat ineedfat committed May 14, 2013
View
2 GruntFile.js
@@ -129,7 +129,7 @@
watch: {
// Run unit test with karma
karma: {
- files: ['build/ng-grid.debug.js', 'test/unit/**/*.js'],
+ files: ['build/ng-grid.debug.js', 'test/unit/**/*.js', 'plugins/*.js'],
tasks: ['karma:watch:run']
},
// Auto-build ng-grid.debug.js when source files change
View
3 config/karma.conf.js
@@ -12,6 +12,9 @@ files = [
// App code
'build/ng-grid.debug.js',
+
+ // Plugins
+ 'plugins/*.js',
// Test specs
'test/unit/**/*.js'
View
2 plugins/ng-grid-csv-export.js
@@ -5,7 +5,7 @@
// download from a data-uri link
//
// Notes: This has not been adequately tested and is very much a proof of concept at this point
-ngGridCsvExportPlugin = function(opts) {
+function ngGridCsvExportPlugin (opts) {
var self = this;
self.grid = null;
self.scope = null;
View
2 plugins/ng-grid-flexible-height.js
@@ -1,4 +1,4 @@
-ngGridFlexibleHeightPlugin = function (opts) {
+function ngGridFlexibleHeightPlugin (opts) {
var self = this;
self.grid = null;
self.scope = null;
View
2 plugins/ng-grid-layout.js
@@ -1,4 +1,4 @@
-ngGridLayoutPlugin = function() {
+function ngGridLayoutPlugin () {
var self = this;
this.grid = null;
this.scope = null;
View
2 plugins/ng-grid-reorderable.js
@@ -2,7 +2,7 @@
DO NOT USE THIS PLUGIN. IT IS ONLY AN EXAMPLE FOR INSTRUCTIONAL PURPOSES.
*/
-ngGridReorderable = function(options) {
+function ngGridReorderable (options) {
var defaults = {
enableHeader: true,
enableRow: true
View
22 plugins/ng-grid-wysiwyg-export.js
@@ -1,11 +1,13 @@
-ngGridWYSIWYGPlugin = function (filter) {
+function ngGridWYSIWYGPlugin (filter) {
var self = this;
self.grid = null;
self.scope = null;
+ self.services = null;
- self.init = function (scope, grid) {
+ self.init = function (scope, grid, services) {
self.grid = grid;
self.scope = scope;
+ self.services = services;
};
self.export = function () {
@@ -15,27 +17,23 @@
gridWidth: self.scope.totalRowWidth(),
data: []
};
+
angular.forEach(self.scope.columns, function (col) {
if (col.visible) {
ret.columns.push(col.displayName);
ret.columnWidths.push(col.width);
}
});
- angular.forEach(self.grid.filteredRows, function (item) {
+ angular.forEach(self.grid.filteredRows, function (row) {
+ var item = row.entity;
angular.forEach(self.scope.columns, function (col) {
if (col.visible) {
- var obj = ng.utils.evalProperty(item, col.field);
- var val = null;
- if (col.cellFilter) {
- val = filter(col.cellFilter)(obj);
- } else {
- val = obj;
- }
+ var obj = self.services.UtilityService.evalProperty(item, col.field);
+ var val = col.cellFilter && filter ? filter(col.cellFilter)(obj) : obj;
ret.data.push(val ? val.toString() : '');
}
});
});
return ret;
};
- return self;
-};
+}
View
4 src/directives/ng-grid.js
@@ -134,7 +134,7 @@
options.gridId = grid.gridId;
options.ngGrid = grid;
options.$gridScope = $scope;
- options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService };
+ options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService, UtilityService: $utils };
$scope.$on('ngGridEventDigestGrid', function(){
domUtilityService.digest($scope.$parent);
});
@@ -149,7 +149,7 @@
//initialize plugins.
angular.forEach(options.plugins, function (p) {
if (typeof p === 'function') {
- p = p.call(this);
+ p = new p(); //If p is a function, then we assume it is a class.
}
p.init($scope.$new(), grid, options.$gridServices);
options.plugins[$utils.getInstanceType(p)] = p;
View
104 test/unit/pluginsSpec.js
@@ -0,0 +1,104 @@
+'use strict';
+
+/* jasmine specs for plugins go here */
+
+describe('plugins', function () {
+ var elm, scope;
+
+ var initalizeHelper = function(gridOptions) {
+ return inject(function($rootScope, $compile) {
+ elm = angular.element(
+ '<div ng-grid="gridOptions" style="width: 1000px; height: 1000px"></div>'
+ );
+
+ scope = $rootScope;
+ scope.myData = [{ name: "Moroni", age: 50 },
+ { name: "Tiancum", age: 43 },
+ { name: "Jacob", age: 27 },
+ { name: "Nephi", age: 29 },
+ { name: "Enos", age: 34 }];
+
+ scope.gridOptions = $.extend(true, { data: 'myData' }, gridOptions);
+
+ $compile(elm)(scope);
+
+ scope.$digest();
+ });
+ };
+ // Load the ngGrid module
+ beforeEach(module('ngGrid'));
+
+ describe('ng-grid-wysiwyg-export', function () {
+ describe('initialization', function () {
+ var testData = function(data) {
+ expect(data.columns.length).toBe(2);
+ expect(data.columns[0]).toBe('name');
+ expect(data.columns[1]).toBe('age');
+
+ expect(data.data.length / data.columns.length).toBe(scope.myData.length);
+ expect(data.data.length % data.columns.length).toBe(0);
+ };
+
+ describe('function-literal', function() {
+ beforeEach(initalizeHelper({ plugins: [ngGridWYSIWYGPlugin] }));
+
+ it('should work if the plugin being passed in is a function literal', function() {
+ testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
+ });
+ });
+
+ describe('instance', function() {
+ beforeEach(initalizeHelper({ plugins: [new ngGridWYSIWYGPlugin()] }));
+
+ it('should work if the plugin being passed in is an instance', function() {
+ testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
+ });
+ });
+ });
+ //describe('filtering', function () {
+ // var testData = function (data) {
+ // expect(data.columns.length).toBe(2);
+ // expect(data.columns[0]).toBe('name');
+ // expect(data.columns[1]).toBe('age');
+
+ // expect(data.data.length).toBe(2);
+ // };
+
+ // describe('function-literal', function () {
+ // beforeEach(initalizeHelper({
+ // plugins: [ngGridWYSIWYGPlugin],
+ // filterOptions: { filterText: '', useExternalFilter: false },
+ // }));
+
+ // it('should work if the plugin being passed in is a function literal', function () {
+ // testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
+ // });
+ // });
+
+ // describe('instance', function () {
+ // beforeEach(initalizeHelper({
+ // plugins: [new ngGridWYSIWYGPlugin()],
+ // filterOptions: { filterText: 'Jacob', useExternalFilter: false },
+ // }));
+
+ // it('should work if the plugin being passed in is an instance', function () {
+ // var flag = false;
+ // runs(function () {
+ // scope.$digest();
+ // setTimeout(function () {
+ // flag = true;
+ // scope.$digest();
+ // }, 500);
+ // });
+ // waitsFor(function () {
+ // return flag;
+ // });
+ // runs(function () {
+ // testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export());
+ // });
+ // });
+ // });
+ //});
+ });
+
+});

0 comments on commit 57df36f

Please sign in to comment.