1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
const Lang = imports.lang;
const GObject = imports.gi.GObject;
//Local Libraries
const Utils = imports.wikipedia.utils;
const CategoryModel = imports.wikipedia.models.category_model;
const ArticleModel = imports.wikipedia.models.article_model;
function _resourceUriToPath(uri) {
if(uri.startsWith('resource://'))
return uri.slice('resource://'.length);
throw new Error('Resource URI did not start with "resource://"');
}
const DomainWikiPresenter = new Lang.Class({
Name: "DomainWikiPresenter",
Extends: GObject.Object,
_init: function(model, view, filename) {
this._domain_wiki_model = model;
this._domain_wiki_view = view;
this._domain_wiki_view.set_presenter(this)
this._domain_wiki_view.connect('category-chosen', Lang.bind(this, this._onCategoryClicked));
this._domain_wiki_view.connect('article-chosen', Lang.bind(this, this._onArticleClicked));
this.initFromJsonFile(filename);
let categories = this._domain_wiki_model.getCategories();
this._domain_wiki_view.set_categories(categories);
},
initArticleModels: function(articles) {
let _articles = new Array();
for(let i = 0; i < articles.length; i++) {
let humanTitle = articles[i].title;
let wikipediaURL = articles[i].url;
let newArticle = new ArticleModel.ArticleModel({ title: humanTitle, uri: wikipediaURL});
_articles.push(newArticle);
}
return _articles;
},
initFromJsonFile: function(filename) {
let app_content = JSON.parse(Utils.load_file_from_resource(filename));
this._lang_code = filename.substring(0, 2);
let categories = app_content['categories'];
let cat_length = categories.length
let category_models = new Array();
for(let i = 0; i < cat_length; i++){
let category = categories[i];
let categoryModel = this.initCategory(category);
let articles = category['articles'];
let articleModels = [];
if(!(articles.length == 0)) {
//if the category has no articles, then we cannot initialize them.
//This happens if the main category isn't clickable.
articleModels = this.initArticleModels(articles);
}
categoryModel.addArticles(articleModels);
category_models.push(categoryModel);
}
this._domain_wiki_model.addCategories(category_models);
},
initCategory: function(category){
let image_uri = category['image_file'];
let image_thumbnail_uri = category['image_thumb_uri'];
let params = {description:category['content_text'], image_uri:image_uri,
image_thumbnail_uri:image_thumbnail_uri, title:category['category_name'],
is_main_category:category['is_main_category']};
return new CategoryModel.CategoryModel(params);
},
_onCategoryClicked: function(page, title, index) {
this._current_category = index;
let category = this._domain_wiki_model.getCategories()[index];
let articles = this._domain_wiki_model.getArticlesForCategoryIndex(index);
let titles = new Array();
for(let i = 0; i < articles.length; i++){
titles.push(articles[i].title);
}
this._domain_wiki_view.set_category_info(category, titles);
this._domain_wiki_view.transition_page(Endless.PageManagerTransitionType.SLIDE_LEFT, 'category');
},
_onArticleClicked: function(article_list, title, index) {
let articles = this._domain_wiki_model.getArticlesForCategoryIndex(this._current_category);
this._domain_wiki_view.set_article_info(articles[index]);
this._domain_wiki_view.transition_page(Endless.PageManagerTransitionType.SLIDE_LEFT, 'article');
},
_onCategoryBackClicked: function(button) {
this._window.page_manager.transition_type = Endless.PageManagerTransitionType.SLIDE_RIGHT;
this._window.page_manager.visible_page_name = 'front';
},
_onArticleBackClicked: function(button) {
this._window.page_manager.transition_type = Endless.PageManagerTransitionType.SLIDE_RIGHT;
this._window.page_manager.visible_page_name = 'category';
}
});
|