aboutsummaryrefslogtreecommitdiffstats
path: root/source/dataView.js
blob: 1c328f82bcd211e081968698302b14744a6ea95d (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
enyo.kind({
    name: "biblez.dataView",
    kind: "enyo.FittableRows",
    fit: true,
    events: {
        onBack: "",
        onVerse: ""
    },
    published: {
        section: ""
    },
    components: [
        {name: "messagePopup", kind: "onyx.Popup", centered: true, floating: true, classes: "message-popup"},
        {kind: "onyx.MoreToolbar", layoutKind:"FittableColumnsLayout", classes: "main-toolbar", components: [
            {name: "btBack", kind: "onyx.IconButton", src: "assets/back.png", ontap: "handleBack"},
            {name: "btHide", kind: "onyx.IconButton", src: "assets/hide.png", showing: false, ontap: "handleBack"},
            {kind: "onyx.RadioGroup", onActivate:"sectionActivated", classes: "center", fit: true, defaultKind: "onyx.IconButton", components: [
                {name: "rbBm", src: "assets/bookmarksTB.png", section: "bookmarks", style: "margin: 0 10px;"},
                {name: "rbNotes", src: "assets/notesTB.png", section: "notes", style: "margin: 0 10px;"},
                {name: "rbHl", src: "assets/highlightsTB.png", section: "highlights", style: "margin: 0 10px;"}
            ]},
        ]},
        {name: "noData", classes: "center", style: "margin-top: 10px;", showing: false},
        {name: "dataList", kind: "AroundList", fit: true, onSetupItem: "setupItem", aboveComponents: [
            {kind: "onyx.Toolbar", layoutKind: "FittableColumnsLayout", components: [
                {name: "searchField", kind: "onyx.InputDecorator", fit: true, noStretch: true, layoutKind: "FittableColumnsLayout", components: [
                    {kind: "onyx.Input", name: "searchInput", placeholder: $L("Search..."), fit: true, oninput: "searchInputChange"},
                    {kind: "Image", src: "assets/search-input.png", style: "height: 20px; width: 20px;"}
                ]},
                {name: "colorSelector", showing: false, kind: "Group", classes: "center", style: "width: 100%;", defaultKind: "onyx.Button", highlander: false, components: [
                    {caption: " ", ontap: "searchHighlights", classes: "color-button", color: "rgba(255,99,71,0.5)", style: "background-color: red;"},
                    {caption: " ", ontap: "searchHighlights", classes: "color-button", color: "rgba(255,255,0,0.5)", style: "background-color: yellow;"},
                    {caption: " ", ontap: "searchHighlights", classes: "color-button", color: "rgba(152,251,152,0.5)", style: "background-color: green;"},
                    {caption: " ", ontap: "searchHighlights", classes: "color-button", color: "rgba(238,130,238,0.5)", style: "background-color: violet;"},
                    {caption: " ", ontap: "searchHighlights", classes: "color-button", color: "rgba(255,165,0,0.5)", style: "background-color: orange;"}
                ]}
            ]},
        ], components: [
            {name: "item", classes: "item", ontap: "handleListTap", components: [
                //{kind: "enyo.FittableRows", components: [
                    {name: "itemOsis", classes: ""},
                    {name: "itemText", classes: "item-text", allowHtml: true}
                //]}
            ]}
        ]}
    ],

    data: [],
    filtered: [],

    showHideButton: function () {
        this.$.btHide.show();
        this.$.btBack.hide();
    },

    sectionActivated: function (inSender, inEvent) {
        if (inEvent.originator.getActive()) {
            this.setSection(inEvent.originator.section);
        }
        return true;
    },

    updateSection: function (inSection) {
        if(inSection)
            this.section = inSection;
        this.sectionChanged();
    },

    sectionChanged: function (inSender, inEvent) {
        this.filter = null;
        //this.$.searchInput.setValue("");
        if (this.section === "bookmarks") {
            this.$.rbBm.setActive(true);
            this.$.searchField.show();
            this.$.colorSelector.hide();
            api.getAllBookmarks(enyo.bind(this, function (inError, inData) {
                if(!inError) {
                    this.data = inData;
                    if(this.$.searchInput.getValue() !== "")
                        this.searchInputChange(this.$.searchInput);
                    else
                        this.updateList();
                } else
                    this.handleError(inError);
            }));
        } else if (this.section === "notes") {
            this.$.rbNotes.setActive(true);
            this.$.searchField.show();
            this.$.colorSelector.hide();
            api.getAllNotes(enyo.bind(this, function (inError, inData) {
                if(!inError) {
                    this.data = inData;
                    if(this.$.searchInput.getValue() !== "")
                        this.searchInputChange(this.$.searchInput);
                    else
                        this.updateList();
                } else
                    this.handleError(inError);
            }));
        } else if (this.section === "highlights") {
            this.$.rbHl.setActive(true);
            this.$.searchField.hide();
            this.$.colorSelector.show();
            api.getAllHighlights(enyo.bind(this, function (inError, inData) {
                if(!inError) {
                    this.data = inData;
                    this.updateList();
                    this.searchHighlights();
                } else
                    this.handleError(inError);
            }));
        }
    },

    updateList: function () {
        this.$.dataList.setCount(this.data.length);
        if(this.data.length === 0) {
            this.$.dataList.hide();
            this.$.noData.show();
            if(this.section === "bookmarks")
                this.$.noData.setContent($L("No Bookmarks.") + " " + $L("Tap on a verse number to add one."));
            else if(this.section === "notes")
                this.$.noData.setContent($L("No Notes.") + " " + $L("Tap on a verse number to add one."));
            else if(this.section === "highlights")
                this.$.noData.setContent($L("No Highlights.") + " " + $L("Tap on a verse number to add one."));
        } else {
            this.$.noData.hide();
            this.$.dataList.show();
        }
        this.$.dataList.reset();
        this.$.dataList.reflow();
        this.$.dataList.scrollToRow(0);
    },

    setupItem: function(inSender, inEvent) {
        var data = this.filter ? this.filtered[inEvent.index] : this.data[inEvent.index];
        this.$.itemOsis.setContent(api.formatOsis(data.osisRef));
        if(this.section === "highlights") {
            this.$.item.applyStyle("background-color", data.color);
            this.$.itemOsis.addRemoveClass("list-selected-bold", inSender.isSelected(inEvent.index));
        } else {
            this.$.item.applyStyle("background-color", null);
            this.$.itemOsis.addRemoveClass("list-selected-bold", inSender.isSelected(inEvent.index));
        }
        if(this.section === "notes")
            this.$.itemText.setContent(data.text);
        else
            this.$.itemText.setContent("");
        this.$.item.addRemoveClass("list-selected", inSender.isSelected(inEvent.index));

    },

    handleListTap: function (inSender, inEvent) {
        this.doVerse({osisRef: this.data[inEvent.index].osisRef});
    },

    searchInputChange: function(inSender) {
        enyo.job(this.id + ":search", this.bindSafely("filterList", inSender.getValue()), 200);
        return true;
    },

    searchHighlights: function (inSender, inEvent) {
        var filter = "";
        if (inSender) {
            inSender.addRemoveClass("active", !inSender.hasClass("active"));
            if (!inSender.hasClass("active")) {
                inSender.active = false;
            }
        }

        var c = this.$.colorSelector.getClientControls();
        c.forEach(function(item) {
            if(item.active)
                filter += (filter === "") ? api.escapeRegExp(item.color) : "|" + api.escapeRegExp(item.color);
        });
        this.filterList(filter);
    },

    filterList: function(inFilter) {
        if (inFilter != this.filter) {
            this.filter = inFilter;
            this.filtered = this.generateFilteredData(inFilter);
            this.$.dataList.setCount(this.filtered.length);
            this.$.dataList.reset();
        }
    },
    generateFilteredData: function(inFilter) {
        var re = new RegExp(inFilter, "i");
        var r = [];
        for (var i=0, d; (d=this.data[i]); i++) {
            if (d.osisRef.match(re) || api.formatOsis(d.osisRef).match(re) || (d.text && d.text.match(re)) || (d.color && d.color.match(re))) {
                r.push(d);
            }
        }
        return r;
    },

    handleBack: function() {
        this.doBack();
    },

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