summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-08-13 23:03:12 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-08-13 23:03:12 -0400
commit76f93b05f8bfb771fc38763fa2d7166570038b48 (patch)
tree6741dc9e9d80c67701d21b97c8c64aba805d9688 /js
parent6bf6a7be174456290272718040071963c7ed0861 (diff)
Better encapsulate JS data types.
* js/guix-packages.js (guix.Sorter, guix.controller): Use module pattern to encapsulate data type creation.
Diffstat (limited to 'js')
-rw-r--r--js/guix-packages.js186
1 files changed, 98 insertions, 88 deletions
diff --git a/js/guix-packages.js b/js/guix-packages.js
index 5903754..afbf4fb 100644
--- a/js/guix-packages.js
+++ b/js/guix-packages.js
@@ -35,100 +35,110 @@ guix.Packages = function() {
return m.request({ method: "GET", url: "packages.json" });
};
-guix.Sorter = function(field, isDescending) {
- this.field = field;
- this.isDescending = _.isUndefined(isDescending) ? false : isDescending;
-};
-
-guix.Sorter.prototype.sort = function(array) {
- var result = _.sortBy(array, this.field);
-
- return this.isDescending ? result.reverse() : result;
-};
-
-guix.Sorter.prototype.reverse = function() {
- return new guix.Sorter(this.field, !this.isDescending);
-};
-
-guix.controller = function() {
- var self = this;
-
- this.packages = guix.Packages();
- this.pages = m.prop([]);
- this.currentPageIndex = 0;
- this.pageSize = 20;
- this.searchTerm = m.prop("");
- this.columns = [
- { header: "Name", sortField: "name" },
- { header: "Version", sortField: "version" },
- { header: "Synopsis", sortField: "synopsis" },
- { header: "Home Page", sortField: "homepage" }, {
- header: "License",
- sortField: function(package) {
- if(_.isArray(package.license)) {
- // Concatenate all license names together for sorting.
- return package.license.reduce(function(memo, l) {
- return memo.concat(l.name);
- }, "");
+guix.Sorter = (function() {
+ function Sorter(field, isDescending) {
+ this.field = field;
+ this.isDescending = _.isUndefined(isDescending) ? false : isDescending;
+ };
+
+ Sorter.prototype.sort = function(array) {
+ var result = _.sortBy(array, this.field);
+
+ return this.isDescending ? result.reverse() : result;
+ };
+
+ Sorter.prototype.reverse = function() {
+ return new guix.Sorter(this.field, !this.isDescending);
+ };
+
+ return Sorter;
+})();
+
+guix.controller = (function() {
+ function controller() {
+ var self = this;
+
+ this.packages = guix.Packages();
+ this.pages = m.prop([]);
+ this.currentPageIndex = 0;
+ this.pageSize = 20;
+ this.searchTerm = m.prop("");
+ this.columns = [
+ { header: "Name", sortField: "name" },
+ { header: "Version", sortField: "version" },
+ { header: "Synopsis", sortField: "synopsis" },
+ { header: "Home Page", sortField: "homepage" }, {
+ header: "License",
+ sortField: function(package) {
+ if(_.isArray(package.license)) {
+ // Concatenate all license names together for sorting.
+ return package.license.reduce(function(memo, l) {
+ return memo.concat(l.name);
+ }, "");
+ }
+
+ return package.license.name;
}
-
- return package.license.name;
}
+ ];
+ this.sorter = m.prop(new guix.Sorter("name"));
+
+ // All packages are visible initially
+ this.packages.then(function(packages) {
+ self.pages(self.paginate(packages, self.pageSize));
+ });
+ };
+
+
+ controller.prototype.paginate = function(array, pageSize) {
+ return guix.chunk(this.sorter().sort(array), pageSize);
+ };
+
+ controller.prototype.currentPage = function() {
+ return this.pages()[this.currentPageIndex] || [];
+ };
+
+ controller.prototype.isFirstPage = function() {
+ return this.currentPageIndex === 0;
+ };
+
+ controller.prototype.isLastPage = function() {
+ return this.currentPageIndex === this.pages().length - 1;
+ };
+
+ controller.prototype.isCurrentPage = function(i) {
+ return this.currentPageIndex === i;
+ };
+
+ controller.prototype.packageCount = function() {
+ return this.pages().reduce(function(memo, page) {
+ return memo + page.length;
+ }, 0);
+ };
+
+ controller.prototype.doSearch = function() {
+ var regexp = new RegExp(this.searchTerm(), "i");
+
+ this.pages(this.paginate(this.packages().filter(function(package) {
+ return regexp.test(package.name) ||
+ regexp.test(package.synopsis);
+ }), this.pageSize));
+ };
+
+ controller.prototype.sortBy = function(field) {
+ if(this.sorter().field === field) {
+ // Reverse sort order if the field is the same as before.
+ this.sorter(this.sorter().reverse());
+ } else {
+ this.sorter(new guix.Sorter(field));
}
- ];
- this.sorter = m.prop(new guix.Sorter("name"));
-
- // All packages are visible initially
- this.packages.then(function(packages) {
- self.pages(self.paginate(packages, self.pageSize));
- });
-};
-
-guix.controller.prototype.paginate = function(array, pageSize) {
- return guix.chunk(this.sorter().sort(array), pageSize);
-};
-guix.controller.prototype.currentPage = function() {
- return this.pages()[this.currentPageIndex] || [];
-};
-
-guix.controller.prototype.isFirstPage = function() {
- return this.currentPageIndex === 0;
-};
-
-guix.controller.prototype.isLastPage = function() {
- return this.currentPageIndex === this.pages().length - 1;
-};
-
-guix.controller.prototype.isCurrentPage = function(i) {
- return this.currentPageIndex === i;
-};
+ this.doSearch();
+ };
-guix.controller.prototype.packageCount = function() {
- return this.pages().reduce(function(memo, page) {
- return memo + page.length;
- }, 0);
-};
-
-guix.controller.prototype.doSearch = function() {
- var regexp = new RegExp(this.searchTerm(), "i");
-
- this.pages(this.paginate(this.packages().filter(function(package) {
- return regexp.test(package.name) ||
- regexp.test(package.synopsis);
- }), this.pageSize));
-};
+ return controller;
+})();
-guix.controller.prototype.sortBy = function(field) {
- if(this.sorter().field === field) {
- // Reverse sort order if the field is the same as before.
- this.sorter(this.sorter().reverse());
- } else {
- this.sorter(new guix.Sorter(field));
- }
-
- this.doSearch();
-};
guix.view = function(ctrl) {
function renderName(package) {