aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.js
blob: 4f25dcb8b61fbffbab43bfbfab3e465b0801bcbc (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
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, 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.currentModule = inModules[0];
                    this.doModuleChanged({module: this.currentModule});
                    this.handlePassage();
                    //this.$.moduleLabel.setContent(this.currentModule.config.moduleKey);
                    this.modules = inModules;
                    var mods = [];
                    this.modules.forEach(enyo.bind(this, function (mod, idx) {
                        if (this.currentModule.modKey === mod.modKey || idx === 0)
                            mods.push({content: mod.config.moduleKey, index: idx, active: true});
                        else
                            mods.push({content: mod.config.moduleKey, index: idx});
                        }));
                    this.$.moduleMenu.createComponents(mods, {owner: this.$.moduleMenu});
                    this.$.moduleMenu.render();
                }
            } else {
                this.handleError(inError);
            }
		}));
	},

	moduleSelected: function (inSender, inEvent) {
		this.currentModule = this.modules[inEvent.originator.index];
		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();
    }
});