Skip to content

Commit

Permalink
Fix wysiwyg-export plugin.
Browse files Browse the repository at this point in the history
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
Trinh Ho committed May 14, 2013
1 parent 4a5c93d commit 57df36f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 19 deletions.
2 changes: 1 addition & 1 deletion GruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ files = [

// App code
'build/ng-grid.debug.js',

// Plugins
'plugins/*.js',

// Test specs
'test/unit/**/*.js'
Expand Down
2 changes: 1 addition & 1 deletion plugins/ng-grid-csv-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion plugins/ng-grid-flexible-height.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ngGridFlexibleHeightPlugin = function (opts) {
function ngGridFlexibleHeightPlugin (opts) {
var self = this;
self.grid = null;
self.scope = null;
Expand Down
2 changes: 1 addition & 1 deletion plugins/ng-grid-layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ngGridLayoutPlugin = function() {
function ngGridLayoutPlugin () {
var self = this;
this.grid = null;
this.scope = null;
Expand Down
2 changes: 1 addition & 1 deletion plugins/ng-grid-reorderable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions plugins/ng-grid-wysiwyg-export.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions src/directives/ng-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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;
Expand Down
104 changes: 104 additions & 0 deletions test/unit/pluginsSpec.js
Original file line number Diff line number Diff line change
@@ -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.