diff options
author | Philip Chimento <philip@endlessm.com> | 2013-10-23 21:53:22 -0700 |
---|---|---|
committer | Philip Chimento <philip@endlessm.com> | 2013-10-23 22:00:01 -0700 |
commit | 2e5e489fa7b64e70f014e0a60bcbd2234b3fe4c1 (patch) | |
tree | 82c3bccd1b9bc39ea3c4e25ad1366acb26e42a6f /wikipedia/models/domain_wiki_model.js | |
parent | 48022e98522f56905986d0d6b3b50277554f974a (diff) |
wiki: Functionality unchanged with new JSON format
Reorganizes the various models, presenters, and views, so that the wiki
apps work unchanged with the new JSON export format. Also refactors so
that there is less linkage between models, presenters, and views, so
further updates will hopefully be less extensive.
[endlessm/eos-sdk#367]
Diffstat (limited to 'wikipedia/models/domain_wiki_model.js')
-rw-r--r-- | wikipedia/models/domain_wiki_model.js | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/wikipedia/models/domain_wiki_model.js b/wikipedia/models/domain_wiki_model.js index cb9071b..dbd0774 100644 --- a/wikipedia/models/domain_wiki_model.js +++ b/wikipedia/models/domain_wiki_model.js @@ -5,19 +5,44 @@ const GObject = imports.gi.GObject; const Lang = imports.lang; // Local libraries +const ArticleModel = imports.wikipedia.models.article_model; const CategoryModel = imports.wikipedia.models.category_model; -const Utils = imports.wikipedia.utils; const DomainWikiModel = new Lang.Class({ Name: "DomainWikiModel", Extends: GObject.Object, - //params should have the image-uri for the app's image, and the application name. _init: function(params) { + this._articles = []; + this._mainCategory = null; + this._categories = {}; this.parent(params); }, + loadFromJson: function (json) { + // Load list of articles + this._articles = json['articles'].map(function (article) { + return ArticleModel.newFromJson(article); + }); + + // First create flat list of category models, indexed by ID + json['categories'].forEach(function (category) { + let modelObj = CategoryModel.newFromJson(category); + if (modelObj.is_main_category) + this._mainCategory = modelObj; + this._categories[modelObj.id] = modelObj; + modelObj.has_articles = this._getCategoryHasArticles(modelObj.id); + }, this); + // Create links between all the category models in a tree + json['categories'].forEach(function (category) { + category['subcategories'].forEach(function(subcatId) { + this._categories[category['category_name']].addSubcategory( + this._categories[subcatId]); + }, this); + }, this); + }, + setLinkedArticles:function(articles){ this._linked_articles = articles; }, @@ -26,17 +51,29 @@ const DomainWikiModel = new Lang.Class({ return this._linked_articles; }, - //categories should be a list of category models, already populated with article models. - addCategories: function(categories){ - this._categories = categories; + getArticles: function () { + return this._articles; + }, + + getArticlesForCategory: function (id) { + return this._articles.filter(function (article) { + return article.getCategories().indexOf(id) != -1; + }); + }, + + // A faster version of getArticlesForCategory() that just returns true if + // there were any articles in this category + _getCategoryHasArticles: function (id) { + return this._articles.some(function (article) { + return article.getCategories().indexOf(id) != -1; + }); }, - getArticlesForCategoryIndex: function(index){ - let category = this.getCategories()[index]; - return category.getArticles(); + getCategory: function (id) { + return this._categories[id]; }, - getCategories: function() { - return this._categories; + getMainCategory: function () { + return this._mainCategory; } });
\ No newline at end of file |