-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathApp.controller.js
More file actions
188 lines (160 loc) · 4.86 KB
/
App.controller.js
File metadata and controls
188 lines (160 loc) · 4.86 KB
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
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator",
"sap/base/strings/formatMessage",
"sap/ui/core/BarColor",
"sap/ui/demo/todo/util/Helper",
"sap/ui/core/Element",
"sap/ui/Device",
"sap/ui/model/json/JSONModel",
], (Controller, Filter, FilterOperator, formatMessage, BarColor, Helper, Element, Device, JSONModel) => {
"use strict";
return Controller.extend("sap.ui.demo.todo.controller.App", {
onInit() {
this.aSearchFilters = [];
this.aTabFilters = [];
this.BarColor = BarColor;
this.getView().setModel(new JSONModel({
isMobile: Device.browser.mobile
}), "view");
},
onAfterRendering() {
const avatarDOM = jQuery("#container-todo---app--avatar-profile");
const avatarCtr = avatarDOM.control(0);
avatarCtr.setSrc(Helper.resolvePath('./img/logo_ui5.png'));
sap.ui.require(["sap/m/Button"], (Button) => {
const clearBtn = new Button({
id: "clearCompleted",
enabled: "{/itemsRemovable}",
icon: "sap-icon://delete",
text: "{i18n>CLEAR_COMPLETED}",
press: this.onClearCompleted.bind(this),
});
this.byId("toolbar").addContent(clearBtn);
});
},
/**
* Get the default model from the view
*
* @returns {sap.ui.model.json.JSONModel} The model containing the todo list, etc.
*/
getModel() {
return this.getView().getModel();
},
/**
* Adds a new todo item to the bottom of the list.
*/
addTodo() {
const oModel = this.getModel();
const aTodos = this.getTodos().map((oTodo) => Object.assign({}, oTodo));
aTodos.push({
title: oModel.getProperty("/newTodo"),
completed: false
});
oModel.setProperty("/todos", aTodos);
oModel.setProperty("/newTodo", "");
},
/**
* Trigger removal of all completed items from the todo list.
*/
onClearCompleted() {
const aTodos = this.getTodos().map((oTodo) => Object.assign({}, oTodo));
this.removeCompletedTodos(aTodos);
this.getModel().setProperty("/todos", aTodos);
},
/**
* Removes all completed items from the given todos.
*
* @param {object[]} aTodos
*/
removeCompletedTodos(aTodos) {
let i = aTodos.length;
while (i--) {
const oTodo = aTodos[i];
if (oTodo.completed) {
aTodos.splice(i, 1);
}
}
},
/**
* Determines the todo list
*
* @returns {object[]} The todo list
*/
getTodos(){
const oModel = this.getModel();
return oModel && oModel.getProperty("/todos") || [];
},
/**
* Updates the number of items not yet completed
*/
onUpdateItemsLeftCount() {
const iItemsLeft = this.getTodos().filter((oTodo) => oTodo.completed !== true).length;
this.getModel().setProperty("/itemsLeftCount", iItemsLeft);
},
/**
* Trigger search for specific items. The removal of items is disable as long as the search is used.
* @param {sap.ui.base.Event} oEvent Input changed event
*/
onSearch(oEvent) {
const oModel = this.getModel();
// First reset current filters
this.aSearchFilters = [];
// add filter for search
this.sSearchQuery = oEvent.getSource().getValue();
if (this.sSearchQuery && this.sSearchQuery.length > 0) {
oModel.setProperty("/itemsRemovable", false);
const filter = new Filter("title", FilterOperator.Contains, this.sSearchQuery);
this.aSearchFilters.push(filter);
} else {
oModel.setProperty("/itemsRemovable", true);
}
this._applyListFilters();
},
onFilter(oEvent) {
// First reset current filters
this.aTabFilters = [];
// add filter for search
this.sFilterKey = oEvent.getParameter("item").getKey();
switch (this.sFilterKey) {
case "active":
this.aTabFilters.push(new Filter("completed", FilterOperator.EQ, false));
break;
case "completed":
this.aTabFilters.push(new Filter("completed", FilterOperator.EQ, true));
break;
case "all":
default:
// Don't use any filter
}
this._applyListFilters();
},
_applyListFilters() {
const oList = Element.getElementById("container-todo---app--todoList");
// const oList = this.byId("todoList");
const oBinding = oList.getBinding("items");
oBinding.filter(this.aSearchFilters.concat(this.aTabFilters), "todos");
const sI18nKey = this.getI18NKey(this.sFilterKey, this.sSearchQuery);
this.byId("filterToolbar").setVisible(!!sI18nKey);
if (sI18nKey) {
this.byId("filterLabel").bindProperty("text", {
path: sI18nKey,
model: "i18n",
formatter: (textWithPlaceholder) => {
return formatMessage(textWithPlaceholder, [this.sSearchQuery]);
}
});
}
},
getI18NKey(sFilterKey, sSearchQuery) {
if (!sFilterKey || sFilterKey === "all") {
return sSearchQuery ? "ITEMS_CONTAINING" : undefined;
} else if (sFilterKey === "active") {
return "ACTIVE_ITEMS" + (sSearchQuery ? "_CONTAINING" : "");
} else {
return "COMPLETED_ITEMS" + (sSearchQuery ? "_CONTAINING" : "");
}
}
});
});