-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchResults.js
201 lines (169 loc) · 9.54 KB
/
searchResults.js
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
// For an introduction to the Search Contract template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232512
// TODO: Add the following script tag to the start page's head to
// subscribe to search contract events.
//
// <script src="/searchResults.js"></script>
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var appModel = Windows.ApplicationModel;
var appViewState = Windows.UI.ViewManagement.ApplicationViewState;
var nav = WinJS.Navigation;
var ui = WinJS.UI;
var utils = WinJS.Utilities;
var searchPageURI = "/searchResults.html";
ui.Pages.define(searchPageURI, {
_filters: [],
_lastSearch: "",
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
var listView = element.querySelector(".resultslist").winControl;
listView.itemTemplate = element.querySelector(".itemtemplate");
listView.oniteminvoked = this._itemInvoked;
this._handleQuery(element, options);
listView.element.focus();
},
// This function updates the page layout in response to viewState changes.
updateLayout: function (element, viewState, lastViewState) {
/// <param name="element" domElement="true" />
var listView = element.querySelector(".resultslist").winControl;
if (lastViewState !== viewState) {
if (lastViewState === appViewState.snapped || viewState === appViewState.snapped) {
var handler = function (e) {
listView.removeEventListener("contentanimating", handler, false);
e.preventDefault();
}
listView.addEventListener("contentanimating", handler, false);
var firstVisible = listView.indexOfFirstVisible;
this._initializeLayout(listView, viewState);
if (firstVisible >= 0 && listView.itemDataSource.list.length > 0) {
listView.indexOfFirstVisible = firstVisible;
}
}
}
},
// This function filters the search data using the specified filter.
_applyFilter: function (filter, originalResults) {
if (filter.results === null) {
filter.results = originalResults.createFiltered(filter.predicate);
}
return filter.results;
},
// This function responds to a user selecting a new filter. It updates the
// selection list and the displayed results.
_filterChanged: function (element, filterIndex) {
var filterBar = element.querySelector(".filterbar");
var listView = element.querySelector(".resultslist").winControl;
utils.removeClass(filterBar.querySelector(".highlight"), "highlight");
utils.addClass(filterBar.childNodes[filterIndex], "highlight");
element.querySelector(".filterselect").selectedIndex = filterIndex;
listView.itemDataSource = this._filters[filterIndex].results.dataSource;
},
_generateFilters: function () {
this._filters = [];
this._filters.push({ results: null, text: "All", predicate: function (item) { return true; } });
// TODO: Replace or remove example filters.
this._filters.push({ results: null, text: "Group 1", predicate: function (item) { return item.group.key === "group1"; } });
this._filters.push({ results: null, text: "Group 2+", predicate: function (item) { return item.group.key !== "group1"; } });
},
// This function executes each step required to perform a search.
_handleQuery: function (element, args) {
var originalResults;
this._lastSearch = args.queryText;
WinJS.Namespace.define("searchResults", { markText: WinJS.Binding.converter(this._markText.bind(this)) });
this._initializeLayout(element.querySelector(".resultslist").winControl, Windows.UI.ViewManagement.ApplicationView.value);
this._generateFilters();
originalResults = this._searchData(args.queryText);
if (originalResults.length === 0) {
document.querySelector('.filterarea').style.display = "none";
} else {
document.querySelector('.resultsmessage').style.display = "none";
}
this._populateFilterBar(element, originalResults);
this._applyFilter(this._filters[0], originalResults);
},
// This function updates the ListView with new layouts
_initializeLayout: function (listView, viewState) {
/// <param name="listView" value="WinJS.UI.ListView.prototype" />
if (viewState === appViewState.snapped) {
listView.layout = new ui.ListLayout();
document.querySelector(".titlearea .pagetitle").textContent = '“' + this._lastSearch + '”';
document.querySelector(".titlearea .pagesubtitle").textContent = "";
} else {
listView.layout = new ui.GridLayout();
// TODO: Change "App Name" to the name of your app.
document.querySelector(".titlearea .pagetitle").textContent = "Resultados de: “" + this._lastSearch + '”';
}
},
_itemInvoked: function (args) {
args.detail.itemPromise.done(function itemInvoked(item) {
// TODO: Navigate to the item that was invoked.
var item = Data.items.getAt(args.detail.itemIndex), idx = Data.items.indexOf(item.data, 0);
WinJS.Navigation.navigate("/pages/itemDetail/itemDetail.html", { item: Data.getItemReference(item), selectedIndex: idx });
});
},
// This function colors the search term. Referenced in /searchResults.html
// as part of the ListView item templates.
_markText: function (text) {
return text.replace(this._lastSearch, "<mark>" + this._lastSearch + "</mark>");
},
// This function generates the filter selection list.
_populateFilterBar: function (element, originalResults) {
var filterBar = element.querySelector(".filterbar");
var listView = element.querySelector(".resultslist").winControl;
var li, option, filterIndex;
filterBar.innerHTML = "";
for (filterIndex = 0; filterIndex < this._filters.length; filterIndex++) {
this._applyFilter(this._filters[filterIndex], originalResults);
li = document.createElement("li");
li.filterIndex = filterIndex;
li.tabIndex = 0;
li.textContent = this._filters[filterIndex].text + " (" + this._filters[filterIndex].results.length + ")";
li.onclick = function (args) { this._filterChanged(element, args.target.filterIndex); }.bind(this);
li.onkeyup = function (args) {
if (args.key === "Enter" || args.key === "Spacebar")
this._filterChanged(element, args.target.filterIndex);
}.bind(this);
utils.addClass(li, "win-type-interactive");
utils.addClass(li, "win-type-x-large");
filterBar.appendChild(li);
if (filterIndex === 0) {
utils.addClass(li, "highlight");
listView.itemDataSource = this._filters[filterIndex].results.dataSource;
}
option = document.createElement("option");
option.value = filterIndex;
option.textContent = this._filters[filterIndex].text + " (" + this._filters[filterIndex].results.length + ")";
element.querySelector(".filterselect").appendChild(option);
}
element.querySelector(".filterselect").onchange = function (args) { this._filterChanged(element, args.currentTarget.value); }.bind(this);
},
// This function populates a WinJS.Binding.List with search results for the
// provided query.
_searchData: function (queryText) {
var originalResults;
// TODO: Perform the appropriate search on your data.
if (window.Data) {
originalResults = Data.items.createFiltered(function (item) {
return (item.title.indexOf(queryText) >= 0 || item.subtitle.indexOf(queryText) >= 0 || item.description.indexOf(queryText) >= 0);
});
} else {
originalResults = new WinJS.Binding.List();
}
return originalResults;
}
});
WinJS.Application.addEventListener("activated", function (args) {
if (args.detail.kind === appModel.Activation.ActivationKind.search) {
args.setPromise(ui.processAll().then(function () {
if (!nav.location) {
nav.history.current = { location: Application.navigator.home, initialState: {} };
}
return nav.navigate(searchPageURI, { queryText: args.detail.queryText });
}));
}
});
appModel.Search.SearchPane.getForCurrentView().onquerysubmitted = function (args) { nav.navigate(searchPageURI, args); };
})();