Advertisement
Guest User

Untitled

a guest
Jan 7th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // For an introduction to the Search Contract template, see the following documentation:
  2. // http://go.microsoft.com/fwlink/?LinkId=232512
  3.  
  4. (function () {
  5.     "use strict";
  6.  
  7.     WinJS.Binding.optimizeBindingReferences = true;
  8.  
  9.     var appModel            = Windows.ApplicationModel;
  10.     var appViewState        = Windows.UI.ViewManagement.ApplicationViewState;
  11.     var nav                 = WinJS.Navigation;
  12.     var ui                  = WinJS.UI;
  13.     var utils               = WinJS.Utilities;
  14.     var searchPageURI       = "/pages/search/searchResults.html";    
  15.     var homeURI             = "/pages/home/html";
  16.  
  17.     //Search Constants
  18.     var searchBase          = 'http://www.xxx.com/web-service2.php?termID=';
  19.     var searchFormat        = 'JSON';
  20.     var searchFormatPiece   = '&format=' + searchFormat;
  21.  
  22.     // Display Variables
  23.     var DefDiv              = ".titlearea .pagetitle";
  24.     var DescDiv             = ".titlearea .pagesubtitle";
  25.     var holder              = [DefDiv, DescDiv];
  26.     var content             = [];
  27.  
  28.     // make this an object property/method eventually
  29.     //var displayResult = function (queryResult) {
  30.     //    for (var i = 0; i < holder.length; i++) {
  31.     //        //document.querySelector(".item-content .title").textContent = "FilmApp";
  32.     //        document.querySelector(holder[i]).textContent = queryResult[i];
  33.     //   }};
  34.    
  35.     // #1 This method is called when the app loads, before any search is entered
  36.     // Creates a new page control with members
  37.     ui.Pages.define(searchPageURI, {
  38.         _lastSearch: "",
  39.  
  40.         // #4
  41.         // This function is called whenever a user navigates to this page. It
  42.         // populates the page elements with the app's data.
  43.         ready: function (element, options) {
  44.             var listView = element.querySelector(".termTest").winControl; // listview variable = stores the control hosted by DOM object resultslist
  45.             listView.itemTemplate = element.querySelector(".itemtemplate"); // sets the itemTemplate to DOM object .itemtemplate
  46.             listView.oniteminvoked  = this._itemInvoked;      // When users clicks an item invokes this parameter
  47.             this._handleQuery(element, options); // calls handle query parameter
  48.             listView.element.focus(); // sets focus on resultslist DOM object
  49.         },
  50.  
  51.         // This function updates the page layout in response to viewState changes.
  52.         updateLayout: function (element, viewState, lastViewState) {
  53.             var listView = element.querySelector(".termTest").winControl;
  54.             if (lastViewState !== viewState) {
  55.                 if (lastViewState === appViewState.snapped || viewState === appViewState.snapped) {
  56.                     var handler = function (e) {
  57.                         listView.removeEventListener("contentanimating", handler, false);
  58.                         e.preventDefault();
  59.                     }
  60.                     listView.addEventListener("contentanimating", handler, false);
  61.                     var firstVisible = listView.indexOfFirstVisible;
  62.                     this._initializeLayout(listView, viewState); // initialise layout
  63.                     if (firstVisible >= 0 && listView.itemDataSource.list.length > 0) {
  64.                         listView.indexOfFirstVisible = firstVisible;
  65.                     }
  66.                 }
  67.             }
  68.         },
  69.  
  70.         // This function executes each step required to perform a search.
  71.         _handleQuery: function (element, args) {
  72.             this._lastSearch        = args.queryText;
  73.             var originalResults     = this._searchData(args.queryText);
  74.             this._initializeLayout(element.querySelector(".termTest").winControl, Windows.UI.ViewManagement.ApplicationView.value); // send value to initialize list view
  75.         },
  76.  
  77.         // This function updates the ListView with new layouts
  78.         _initializeLayout: function (listView, viewState) {
  79.             /// <param name="listView" value="WinJS.UI.ListView.prototype" />
  80.             if (viewState === appViewState.snapped) {
  81.                 listView.layout = new ui.ListLayout();
  82.                 document.querySelector(".titlearea .pagetitle").textContent = '“' + this._lastSearch + '”';
  83.                 document.querySelector(".titlearea .pagesubtitle").textContent = "";
  84.             } else {
  85.                 listView.layout = new ui.GridLayout();
  86.                 // TODO: Change "App Name" to the name of your app.
  87.                 document.querySelector(".titlearea .pagetitle").textContent = "FilmApp";
  88.                 document.querySelector(".titlearea .pagesubtitle").textContent = "Results for “" + this._lastSearch + '”';
  89.             }
  90.         },
  91.  
  92.         _itemInvoked: function (args) {
  93.             args.detail.itemPromise.done(function itemInvoked(item) {
  94.                 // TODO: Navigate to the item that was invoked.
  95.                 nav.navigate(homeURI);
  96.             });
  97.         },
  98.  
  99.         _searchData: function (queryText) {
  100.             var originalResults;
  101.             if (!window.Data) {  
  102.                 var searchUrl = searchBase + queryText + searchFormatPiece;
  103.                 WinJS.xhr({ url: searchUrl }).done(this._lookUpSuccess, this._lookUpFail, this._lookUpProgress);
  104.  
  105.             }else{
  106.                 document.querySelector(".titlearea .pagetitle").textContent = "There has been a computer malfunction - sort it the **** out!";
  107.             }
  108.  
  109.             return originalResults;
  110.         },
  111.  
  112.         displayResult: function (queryResult) {
  113.             for (var i = 0; i < holder.length; i++) {
  114.                 //document.querySelector(".item-content .title").textContent = "FilmApp";
  115.                 document.querySelector(holder[i]).textContent = queryResult[i];
  116.             };
  117.         },
  118.  
  119.         _lookUpSuccess: function xhrSucceed(Result) {
  120.             var response = JSON.parse(Result.responseText);
  121.  
  122.             if (response.response[0].Message === "Found") {
  123.                 for (var x in response.response[1].term) {
  124.                     content.push(response.response[1].term[x]);
  125.                 };
  126.                 this.displayResult(content);
  127.                 return content;
  128.                
  129.             } else {
  130.                 content.push("Not Found", "Not Found");
  131.                 return content;
  132.             }
  133.          },
  134.  
  135.         _lookUpFail: function xhrFail(Result) {
  136.                 document.querySelector(".titlearea .pagetitle").textContent ="Got Error";
  137.          },
  138.  
  139.         _lookUpProgress: function xhrProgress(Result) {
  140.                 document.querySelector(".titlearea .pagetitle").textContent = "No Result 2";
  141.         },
  142.  
  143. }); // End of UI.page.define    
  144.  
  145.    
  146.  
  147. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement