Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // For an introduction to the Search Contract template, see the following documentation:
- // http://go.microsoft.com/fwlink/?LinkId=232512
- (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 = "/pages/search/searchResults.html";
- var homeURI = "/pages/home/html";
- //Search Constants
- var searchBase = 'http://www.xxx.com/web-service2.php?termID=';
- var searchFormat = 'JSON';
- var searchFormatPiece = '&format=' + searchFormat;
- // Display Variables
- var DefDiv = ".titlearea .pagetitle";
- var DescDiv = ".titlearea .pagesubtitle";
- var holder = [DefDiv, DescDiv];
- var content = [];
- // make this an object property/method eventually
- //var displayResult = function (queryResult) {
- // for (var i = 0; i < holder.length; i++) {
- // //document.querySelector(".item-content .title").textContent = "FilmApp";
- // document.querySelector(holder[i]).textContent = queryResult[i];
- // }};
- // #1 This method is called when the app loads, before any search is entered
- // Creates a new page control with members
- ui.Pages.define(searchPageURI, {
- _lastSearch: "",
- // #4
- // 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(".termTest").winControl; // listview variable = stores the control hosted by DOM object resultslist
- listView.itemTemplate = element.querySelector(".itemtemplate"); // sets the itemTemplate to DOM object .itemtemplate
- listView.oniteminvoked = this._itemInvoked; // When users clicks an item invokes this parameter
- this._handleQuery(element, options); // calls handle query parameter
- listView.element.focus(); // sets focus on resultslist DOM object
- },
- // This function updates the page layout in response to viewState changes.
- updateLayout: function (element, viewState, lastViewState) {
- var listView = element.querySelector(".termTest").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); // initialise layout
- if (firstVisible >= 0 && listView.itemDataSource.list.length > 0) {
- listView.indexOfFirstVisible = firstVisible;
- }
- }
- }
- },
- // This function executes each step required to perform a search.
- _handleQuery: function (element, args) {
- this._lastSearch = args.queryText;
- var originalResults = this._searchData(args.queryText);
- this._initializeLayout(element.querySelector(".termTest").winControl, Windows.UI.ViewManagement.ApplicationView.value); // send value to initialize list view
- },
- // 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 = "FilmApp";
- document.querySelector(".titlearea .pagesubtitle").textContent = "Results for “" + this._lastSearch + '”';
- }
- },
- _itemInvoked: function (args) {
- args.detail.itemPromise.done(function itemInvoked(item) {
- // TODO: Navigate to the item that was invoked.
- nav.navigate(homeURI);
- });
- },
- _searchData: function (queryText) {
- var originalResults;
- if (!window.Data) {
- var searchUrl = searchBase + queryText + searchFormatPiece;
- WinJS.xhr({ url: searchUrl }).done(this._lookUpSuccess, this._lookUpFail, this._lookUpProgress);
- }else{
- document.querySelector(".titlearea .pagetitle").textContent = "There has been a computer malfunction - sort it the **** out!";
- }
- return originalResults;
- },
- displayResult: function (queryResult) {
- for (var i = 0; i < holder.length; i++) {
- //document.querySelector(".item-content .title").textContent = "FilmApp";
- document.querySelector(holder[i]).textContent = queryResult[i];
- };
- },
- _lookUpSuccess: function xhrSucceed(Result) {
- var response = JSON.parse(Result.responseText);
- if (response.response[0].Message === "Found") {
- for (var x in response.response[1].term) {
- content.push(response.response[1].term[x]);
- };
- this.displayResult(content);
- return content;
- } else {
- content.push("Not Found", "Not Found");
- return content;
- }
- },
- _lookUpFail: function xhrFail(Result) {
- document.querySelector(".titlearea .pagetitle").textContent ="Got Error";
- },
- _lookUpProgress: function xhrProgress(Result) {
- document.querySelector(".titlearea .pagetitle").textContent = "No Result 2";
- },
- }); // End of UI.page.define
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement