aboutsummaryrefslogtreecommitdiffstats
path: root/source/moduleManager.js
blob: 6326717ebdaefa555b2da803539a50f2a80e9837 (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
enyo.kind({
    name: "biblez.moduleManager",
    kind: "enyo.FittableRows",
    fit: true,
    events: {
        onBack: ""
    },
    components: [
        {kind: "onyx.MoreToolbar", components: [
            {kind: "onyx.Button", content: $L("<"), ontap: "doBack"},
            {content: $L("Module Manager")},
            {kind: "onyx.PickerDecorator", components: [
                {},
                {name: "repoPicker", kind: "onyx.Picker", onSelect: "handleRepoChange"}
            ]}
        ]},
        {name: "panel", arrangerKind: "CollapsingArranger", fit: true, kind: "Panels", classes: "app-panels", components: [
            {name: "panelLang", kind: "enyo.FittableRows", components: [
                {name: "langList", kind: "List", fit: true, touch: true, onSetupItem: "setupLangItem", components: [
                    {classes: "item", ontap: "handleLanguage", components: [
                        {name: "langName"}
                    ]}
                ]}
            ]},
            {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.Button", onTap: "installModule", name: "btnInstall", content: $L("Install Module"), style: "margin-left: 10px;"},
                        {name: "detailsDescription", allowHtml: true, classes: "nice-padding"}
                    ]}
                ]}
            ]}
        ]}
    ],

    lang: [],
    repos: [],
    modules: [],
    langModules: [],

    rendered: function () {
        this.inherited(arguments);
        if (!api.get("lastRepoUpdate"))
            this.getRepos();
        else
            this.setupRepoPicker();
    },

    handleRepoChange: function (inSender, inEvent) {
        this.$.detailsContainer.hide();
        api.set("currentRepo", this.repos[inEvent.selected.index]);
    },

    getRepos: function () {
        sword.installMgr.getRepositories(enyo.bind(this, function (inError, inRepos) {
            console.log(inError, inRepos);
            api.set("repos", inRepos);
            api.set("lastRepoUpdate", {time: new Date().getTime()});
            this.setupRepoPicker(inRepos);
        }));
    },

    setupRepoPicker: function (inRepos) {
        if(!inRepos)
            inRepos = api.get("repos");

        var items = [],
            cw = null;
        var currentRepo = api.get("currentRepo");
        inRepos.forEach(function(repo,idx) {
            if (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();

        enyo.log("currentRepo:", currentRepo);
        if(currentRepo) this.getRemoteModules(currentRepo);
        else this.getRemoteModules(cw);
    },

    getRemoteModules: function (inRepo) {
        var currentModules = api.get("currentModules");
        if(currentModules && inRepo.name === currentModules.name) {
            this.modules = currentModules.modules;
            this.prepareLangList(this.modules);
        } else {
            sword.installMgr.getModules(inRepo.confUrl, enyo.bind(this, function (inError, inModules) {
                enyo.log(inError, inModules, inModules.length);
                if(!inError) {
                    api.set("currentModules", {modules: inModules, name: inRepo.name});
                    this.modules = inModules;
                    //this.$.langList.setCount(inModules.length);
                    this.prepareLangList(this.modules);

                }
            }));
        }
    },

    prepareLangList: function (inModules) {
        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.$.langList.setCount(this.lang.length);
        this.$.langList.refresh();
    },

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

    handleLanguage: function(inSender, inEvent) {
        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) {
        this.$.detailsContainer.show();
        var data = this.langModules[inEvent.index];
        this.$.detailsName.setContent(data.Description);
        this.$.detailsDescription.setContent(data.About.replace(/\\par/g, "<br></br>"));
    }
});