aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.js
blob: 16c6c991af92371bb652f452660c4ed748db92c2 (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
enyo.kind({
    name: "biblez.main",
    kind: "FittableRows",
    fit: true,
    events: {
        onOpenModuleManager: "",
        onModuleChanged: "",
        onOpenBC: ""
    },
    published: {
        passage: ""
    },
    components:[
        //{kind: "Signals", onSwordReady: "getBible"},
        {name: "messagePopup", kind: "onyx.Popup", centered: true, floating: true, classes: "message-popup"},
        {kind: "onyx.MoreToolbar", components: [
            {kind: "onyx.MenuDecorator", onSelect: "moduleSelected", components: [
                {kind: "onyx.IconButton", src: "assets/modules.png"},
                {kind: "onyx.Menu", name: "moduleMenu"}
            ]},
            {kind: "onyx.Button", name: "btnPassage", ontap: "doOpenBC"}
            /*{kind: "onyx.InputDecorator", components: [
                {kind: "onyx.Input", placeholder: "Enter a passage...", onchange: "handlePassage", name: "passageInput", value: "Matt 1"}
            ]}*/
        ]},
        {kind: "enyo.Scroller", touch: true, fit: true, classes: "background", components: [
            {kind: "onyx.Spinner", name: "spinner", classes: "onyx-light"},
            {name: "main", classes: "nice-padding", allowHtml: true}
        ]},
        {kind: "onyx.MoreToolbar", components: [
            {kind: "onyx.IconButton", src: "assets/add.png", ontap: "doOpenModuleManager"},
            {kind: "onyx.Button", content: "Delete all Modules", ontap: "clearDB"}
            //{kind: "onyx.Button", content: "Install ESV", esv: true, ontap: "handleInstallTap"},
            //{kind: "Input", type: "file", onchange: "handleInstallTap"}
        ]}
    ],

    currentModule: null,
    currentPassage: "Matt 1",
    modules: [],

    create: function () {
        this.inherited(arguments);
        this.$.spinner.stop();
        this.getInstalledModules();
    },

    rendered: function () {
        this.inherited(arguments);
    },

    getInstalledModules: function (inSender, inEvent) {
        sword.moduleMgr.getModules(enyo.bind(this, function(inError, inModules) {
            if (!inError) {
                if(inModules.length !== 0) {
                    this.modules = inModules;
                    this.renderModuleMenu(this.modules);
                } else {
                    this.$.main.setContent($L("You have no modules installed. Tap on the '+' to install one!"));
                }
            } else {
                this.handleError(inError);
            }
        }));
    },

    renderModuleMenu: function (inModules) {
        if(!inModules)
            inModules = this.modules;
        var lastModule = api.get("lastModule");
        this.$.moduleMenu.destroyClientControls();
        var mods = [];
        this.modules.forEach(enyo.bind(this, function (mod, idx) {
            if ((lastModule && lastModule === mod.modKey)) {
                //mods.push({content: mod.config.moduleKey, index: idx, active: true, style: "background-color: lightblue"});
                mods.push({active: true, components: [
                    {content: mod.config.moduleKey, index: idx},
                    {kind: "onyx.IconButton", src: "assets/checkmark.png", style: "float: right;"}
                ]});
                this.currentModule = mod;
            } else
                mods.push({content: mod.config.moduleKey, index: idx});
        }));
        if(this.currentModule === null) {
            this.currentModule = this.modules[0];
            mods[0]["active"] = true;
        }
        this.$.moduleMenu.createComponents(mods, {owner: this.$.moduleMenu});
        this.$.moduleMenu.render();

        this.doModuleChanged({module: this.currentModule});
        this.handlePassage();
    },

    moduleSelected: function (inSender, inEvent) {
        //console.log(inEvent.originator.index);
        if (!isNaN(inEvent.originator.index)) {
            this.currentModule = this.modules[inEvent.originator.index];
            api.set("lastModule", this.currentModule.modKey);
            this.renderModuleMenu();
            //this.doModuleChanged({module: this.currentModule});
            //this.handlePassage();
        }
    },

    handleInstallTap: function (inSender, inEvent) {
        this.$.spinner.start();
        self = this;
        self.$.main.setContent("Installing Module...");
        var blob = "ESV.zip";
        if (!inSender.esv)
            blob = inEvent.target.files[0];
        sword.installMgr.installModule(blob, function (inError, inId) {
            //console.log(inError, inId);
            if(!inError)
                sword.moduleMgr.getModule(inId, function (inError, inModule) {
                    //console.log(inError, inModule);
                    self.currentModule = inModule;
                    self.$.spinner.stop();
                    if(!inError) {
                        self.$.main.setContent(enyo.json.stringify(inModule.config));
                        self.$.moduleLabel.setContent(inModule.config.moduleKey);
                    } else
                        this.handleError(inError);
                });
            else
                this.handleError(inError);
        });
    },

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

    passageChanged: function (inSender, inEvent) {
        this.currentPassage = inEvent.book.abbrev + " " + inEvent.chapter;
        this.handlePassage(inEvent.osis);
    },

    handlePassage: function (passage) {
        //console.log("PASSAGE", inSender.getValue());
        if(!passage)
            passage = this.currentPassage;
        this.$.btnPassage.setContent(this.currentPassage);
        this.currentModule.renderText(passage, {oneVersePerLine: true}, enyo.bind(this, function (inError, inText) {
            console.log(inError);
            if(!inError)
                this.$.main.setContent(inText);
            else
                this.handleError(inError);
        }));
    },

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