aboutsummaryrefslogtreecommitdiffstats
path: root/source/moduleManager.js
blob: 0c5badbbc48f1c5a1d1572165695558ba21f3548 (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
enyo.kind({
    name: "biblez.moduleManager",
    kind: "enyo.FittableRows",
    fit: true,
    events: {
        onBack: "",
        onInstalled: ""
    },
    components: [
        {name: "messagePopup", kind: "onyx.Popup", centered: true, floating: true, classes: "message-popup"},
        {name: "scrim", kind: "onyx.Scrim", classes: "onyx-scrim-translucent"},
        {kind: "onyx.MoreToolbar", components: [
            {kind: "onyx.IconButton", src: "assets/back.png", ontap: "handleBack"},
            {content: $L("Module Manager")}
        ]},
        {name: "panel", arrangerKind: "CollapsingArranger", fit: true, kind: "Panels", classes: "app-panels", components: [
            {name: "panelLang", kind: "enyo.FittableRows", components: [
                {classes: "center", components: [{kind: "onyx.Spinner", name: "spinner", classes: "onyx-light center"}]},
                {name: "langList", kind: "List", fit: true, touch: true, onSetupItem: "setupLangItem", components: [
                    {classes: "item", ontap: "handleLanguage", components: [
                        {kind: "enyo.FittableColumns", components: [
                            {name: "langShort", classes: "item-left"},
                            {name: "langName", style: "font-style: italic;"}
                        ]}

                    ]}
                ]}
            ]},
            {name: "panelModules", kind: "enyo.FittableRows", components: [
                {name: "modList", kind: "List", fit: true, touch: true, onSetupItem: "setupModItem", components: [
                    {classes: "item", ontap: "handleModule", components: [
                        {name: "modName"}
                    ]}
                ]}
            ]},
            {name: "panelDescription", kind: "enyo.FittableRows", components: [
                {kind: enyo.Scroller, touch: true, fit: true, components: [
                    {name: "detailsContainer", showing: false, classes: "content-container", components: [
                        {name: "detailsName", classes: "title"},
                        //{kind: "onyx.ProgressBar", name: "progressBar", progress: 0, showing: false, showStripes: false},
                        {kind: "onyx.Button", ontap: "installModule", name: "btnInstall", classes: "onyx-affirmative", content: $L("Install Module"), style: "margin-left: 10px;"},
                        {kind: "onyx.Button", ontap: "removeModule", name: "btnRemove", showing: false, classes: "onyx-negative", content: $L("Remove Module"), style: "margin-left: 10px;"},
                        {name: "detailsDescription", allowHtml: true, classes: "nice-padding"}
                    ]}
                ]}
            ]}
        ]},
        {kind: "onyx.MoreToolbar", name: "bottomTB", components: [
            {kind: "onyx.PickerDecorator", components: [
                {},
                {name: "repoPicker", kind: "onyx.Picker", onSelect: "handleRepoChange"}
            ]},
            {kind: "onyx.ProgressBar", name: "progressBar", progress: 0, showing: false, showStripes: false, fit: true}
        ]}
    ],

    lang: [],
    started: false,
    repos: [],
    modules: [],
    langModules: [],
    currentModule: null,
    installedModules: [],

    start: function () {
        if (!this.started) {
            //this.$.scrim.show();
            this.$.spinner.show();
            api.get("repos", enyo.bind(this, function (inError, inData) {
                if(!inData)
                    this.getRepos();
                else
                    this.setupRepoPicker(inData.repos, inData.currentRepo);
            }));
        }
        this.getInstalledModules();
        this.started = true;
    },

    getInstalledModules: function () {
        sword.moduleMgr.getModules(enyo.bind(this, function (inError, inModules) {
            //console.log(inModules);
            if(!inError && inModules.length !== 0) {
                this.installedModules = inModules;
            }
        }));
    },

    handleBack: function() {
        if(enyo.Panels.isScreenNarrow()) {
            if(this.$.panel.getIndex() !== 0)
                this.$.panel.previous();
            else
                this.doBack();
        } else this.doBack();
    },

    handleRepoChange: function (inSender, inEvent) {
        this.$.detailsContainer.hide();

        this.$.modList.setCount(0);
        this.$.modList.refresh();
        this.$.langList.setCount(0);
        this.$.langList.refresh();
        this.$.spinner.show();
        this.$.panel.setIndex(0);

        api.get("repos", enyo.bind(this, function(inError, inRepos) {
            if(!inError) {
                inRepos["currentRepo"] = this.repos[inEvent.selected.index];
                api.put(inRepos);
            } else
                this.handleError(inError);
        }));
        //console.log(inEvent.selected.index);
        this.getRemoteModules(this.repos[inEvent.selected.index]);
    },

    getRepos: function () {
        if(navigator.onLine)
            sword.installMgr.getRepositories(enyo.bind(this, function (inError, inRepos) {
                if (!inError) {
                    api.put({id: "repos", repos: inRepos, lastRepoUpdate: {time: new Date().getTime()}},
                        enyo.bind(this, function (inError, inId) {
                            if(!inError)
                                this.setupRepoPicker(inRepos);
                            else
                                this.handleError(inError);
                        })
                    );

                } else {
                    this.handleError(inError);
                }
            }));
        else {
            this.$.spinner.stop();
            this.handleError({message: $L("You need an internet connection to download modules!")});
        }
    },

    setupRepoPicker: function (inRepos, currentRepo) {
        var items = [],
            cw = null;
        inRepos.forEach(function(repo, idx) {
            if ((currentRepo && repo.name === currentRepo.name) || repo.name === "CrossWire") {
                items.push({content: repo.name, index: idx, active: true});
                cw = repo;
            } else items.push({content: repo.name, index: idx});
        });

        this.repos = inRepos;
        this.$.repoPicker.createComponents(items, {owner: this});
        this.$.repoPicker.render();

        if (currentRepo)
            this.getRemoteModules(currentRepo);
        else
            this.getRemoteModules(cw);
    },

    getRemoteModules: function (inRepo) {
        //console.log(inRepo);
        api.get("downloadedModules", enyo.bind(this, function (inError, allModules) {
            //console.log(inRepo, allModules, this.repos);
            if(!inError) {
                if(allModules && allModules.hasOwnProperty(inRepo.name.replace(" ", ""))) {
                    this.modules = allModules[inRepo.name.replace(" ", "")].modules;
                    this.prepareLangList(this.modules);
                } else {
                    if (navigator.onLine)
                        sword.installMgr.getModules(inRepo, enyo.bind(this, function (inError, inModules) {
                            //enyo.log(inError, inModules, inModules.length);
                            if(!inError) {
                                if(!allModules) allModules = {id: "downloadedModules"};
                                allModules[inRepo.name.replace(" ", "")] = {modules: inModules, name: inRepo.name};
                                api.put(allModules, enyo.bind(this, function (inError, inId) {
                                    if(inError)
                                        this.handleError(inError);
                                }));
                                this.modules = inModules;
                                this.prepareLangList(this.modules);

                            } else {
                                this.handleError((inError.message) ? inError.message : inError);
                            }
                        }));
                    else {
                        this.$.spinner.stop();
                        this.handleError({message: $L("You need an internet connection to download modules!")});
                    }
                }
            } else
                this.handleError(inError);
        }));

    },

    prepareLangList: function (inModules) {
        this.$.spinner.hide();
        this.lang = [];
        inModules.forEach(enyo.bind(this, function(module, idx) {
            //console.log(module.Lang, inModules[idx+1].Lang);
            if (idx === 0) {
                this.lang.push({lang: module.Lang});
            } else if (idx > 0 && module.Lang !== inModules[idx-1].Lang) {
                this.lang.push({lang: module.Lang});
            }
        }));
        this.$.panelLang.reflow();
        this.$.langList.setCount(this.lang.length);
        this.$.langList.refresh();


    },

    setupLangItem: function(inSender, inEvent) {
        var data = this.lang[inEvent.index];
        this.$.langShort.setContent(data.lang);
        this.$.langName.setContent(languages[data.lang]);
        //this.$.index.setContent(inEvent.index);
    },

    handleLanguage: function(inSender, inEvent) {
        if(enyo.Panels.isScreenNarrow()) {
            this.$.panel.next();
        }
        this.langModules = [];
        this.modules.forEach(enyo.bind(this, function (module, idx) {
            if(module.Lang === this.lang[inEvent.index].lang)
                this.langModules.push(module);
        }));
        this.$.modList.setCount(this.langModules.length);
        this.$.modList.refresh();
    },

    // Module List //
    setupModItem: function (inSender, inEvent) {
        var data = this.langModules[inEvent.index];
        this.$.modName.setContent(data.Description);
    },

    handleModule: function (inSender, inEvent) {
        if(enyo.Panels.isScreenNarrow()) {
            this.$.panel.next();
        }
        this.$.btnInstall.show();
        this.$.btnRemove.hide();
        this.currentModule = this.langModules[inEvent.index];
        this.installedModules.forEach(enyo.bind(this, function(mod) {
            if(mod.modKey === this.currentModule.moduleKey) {
                this.$.btnInstall.hide();
                this.$.btnRemove.show();
            }
        }));
        this.$.detailsContainer.show();
        this.$.detailsName.setContent(this.currentModule.Description);
        this.$.detailsDescription.setContent(this.currentModule.About.replace(/\\par/g, "<br>"));
    },

    installModule: function (inSender, inEvent) {
        //console.log(this.currentModule.url);
        this.$.btnInstall.setDisabled(true);
        this.$.progressBar.show();
        this.$.bottomTB.render();
        sword.installMgr.installModule(this.currentModule.url, enyo.bind(this, function (inError, inModule) {
            if (!inError) {
                this.doInstalled();
                this.getInstalledModules();
            } else {
                this.handleError(inError);
            }
            //console.log(inError, inModule);
            this.$.progressBar.hide();
            this.$.progressBar.setProgress(0);
            this.$.btnInstall.setDisabled(false);
            this.$.btnInstall.hide();
            this.$.btnRemove.show();
        }),
        enyo.bind(this, function (inEvent) {
            this.$.progressBar.animateProgressTo(inEvent.loaded/inEvent.total*100);
        }));
    },

    removeModule: function (inSender, inEvent) {
        sword.installMgr.removeModule(this.currentModule.moduleKey, enyo.bind(this, function (inError) {
            if(!inError) {
                this.doInstalled();
                this.$.btnInstall.show();
                this.$.btnRemove.hide();
            } else {
                this.handleError(inError);
            }
        }));
    },

    clearDB: function () {
        sword.dataMgr.clearDatabase();
    },

    handleError: function (inMessage) {
        if (inMessage.message)
            inMessage = inMessage.message;
        this.$.messagePopup.setContent(inMessage);
        this.$.messagePopup.show();
    }
});