sample-utils.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //// Copyright (c) Microsoft Corporation. All rights reserved
  2. // This file is a part of the SDK sample framework. For code demonstrating scenarios in this particular sample,
  3. // please see the html, css and js folders.
  4. (function () {
  5. //
  6. // Helper controls used in the sample pages
  7. //
  8. // The ScenarioInput control inserts the appropriate markup to get labels & controls
  9. // hooked into the input section of a scenario page so that it's not repeated in
  10. // every one.
  11. var lastError = "";
  12. var lastStatus = "";
  13. var ScenarioInput = WinJS.Class.define(
  14. function (element, options) {
  15. element.winControl = this;
  16. this.element = element;
  17. new WinJS.Utilities.QueryCollection(element)
  18. .setAttribute("role", "main")
  19. .setAttribute("aria-labelledby", "inputLabel");
  20. element.id = "input";
  21. this.addInputLabel(element);
  22. this.addDetailsElement(element);
  23. this.addScenariosPicker(element);
  24. }, {
  25. addInputLabel: function (element) {
  26. var label = document.createElement("h2");
  27. label.textContent = "Input";
  28. label.id = "inputLabel";
  29. element.parentNode.insertBefore(label, element);
  30. },
  31. addScenariosPicker: function (parentElement) {
  32. var scenarios = document.createElement("div");
  33. scenarios.id = "scenarios";
  34. var control = new ScenarioSelect(scenarios);
  35. parentElement.insertBefore(scenarios, parentElement.childNodes[0]);
  36. },
  37. addDetailsElement: function (sourceElement) {
  38. var detailsDiv = this._createDetailsDiv();
  39. while (sourceElement.childNodes.length > 0) {
  40. detailsDiv.appendChild(sourceElement.removeChild(sourceElement.childNodes[0]));
  41. }
  42. sourceElement.appendChild(detailsDiv);
  43. },
  44. _createDetailsDiv: function () {
  45. var detailsDiv = document.createElement("div");
  46. new WinJS.Utilities.QueryCollection(detailsDiv)
  47. .addClass("details")
  48. .setAttribute("role", "region")
  49. .setAttribute("aria-labelledby", "descLabel")
  50. .setAttribute("aria-live", "assertive");
  51. var label = document.createElement("h3");
  52. label.textContent = "Description";
  53. label.id = "descLabel";
  54. detailsDiv.appendChild(label);
  55. return detailsDiv;
  56. },
  57. }
  58. );
  59. // The ScenarioOutput control inserts the appropriate markup to get labels & controls
  60. // hooked into the output section of a scenario page so that it's not repeated in
  61. // every one.
  62. var ScenarioOutput = WinJS.Class.define(
  63. function (element, options) {
  64. element.winControl = this;
  65. this.element = element;
  66. new WinJS.Utilities.QueryCollection(element)
  67. .setAttribute("role", "region")
  68. .setAttribute("aria-labelledby", "outputLabel")
  69. .setAttribute("aria-live", "assertive");
  70. element.id = "output";
  71. this._addOutputLabel(element);
  72. this._addStatusOutput(element);
  73. }, {
  74. _addOutputLabel: function (element) {
  75. var label = document.createElement("h2");
  76. label.id = "outputLabel";
  77. label.textContent = "Output";
  78. element.parentNode.insertBefore(label, element);
  79. },
  80. _addStatusOutput: function (element) {
  81. var statusDiv = document.createElement("div");
  82. statusDiv.id = "statusMessage";
  83. statusDiv.setAttribute("role", "textbox");
  84. element.insertBefore(statusDiv, element.childNodes[0]);
  85. }
  86. }
  87. );
  88. // Sample infrastructure internals
  89. var currentScenarioUrl = null;
  90. WinJS.Navigation.addEventListener("navigating", function (evt) {
  91. currentScenarioUrl = evt.detail.location;
  92. });
  93. WinJS.log = function (message, tag, type) {
  94. var isError = (type === "error");
  95. var isStatus = (type === "status");
  96. if (isError || isStatus) {
  97. var statusDiv = /* @type(HTMLElement) */ document.getElementById("statusMessage");
  98. if (statusDiv) {
  99. statusDiv.innerText = message;
  100. if (isError) {
  101. lastError = message;
  102. statusDiv.style.color = "blue";
  103. } else if (isStatus) {
  104. lastStatus = message;
  105. statusDiv.style.color = "green";
  106. }
  107. }
  108. }
  109. };
  110. // Control that populates and runs the scenario selector
  111. var ScenarioSelect = WinJS.UI.Pages.define("/sample-utils/scenario-select.html", {
  112. ready: function (element, options) {
  113. var that = this;
  114. var selectElement = WinJS.Utilities.query("#scenarioSelect", element);
  115. this._selectElement = selectElement[0];
  116. SdkSample.scenarios.forEach(function (s, index) {
  117. that._addScenario(index, s);
  118. });
  119. selectElement.listen("change", function (evt) {
  120. var select = evt.target;
  121. if (select.selectedIndex >= 0) {
  122. var newUrl = select.options[select.selectedIndex].value;
  123. WinJS.Navigation.navigate(newUrl);
  124. }
  125. });
  126. selectElement[0].size = (SdkSample.scenarios.length > 5 ? 5 : SdkSample.scenarios.length);
  127. if (SdkSample.scenarios.length === 1) {
  128. // Avoid showing down arrow when there is only one scenario
  129. selectElement[0].setAttribute("multiple", "multiple");
  130. }
  131. // Use setImmediate to ensure that the select element is set as active only after
  132. // the scenario page has been constructed.
  133. setImmediate(function () {
  134. that._selectElement.setActive();
  135. });
  136. },
  137. _addScenario: function (index, info) {
  138. var option = document.createElement("option");
  139. if (info.url === currentScenarioUrl) {
  140. option.selected = "selected";
  141. }
  142. option.text = (index + 1) + ") " + info.title;
  143. option.value = info.url;
  144. this._selectElement.appendChild(option);
  145. }
  146. });
  147. function activated(e) {
  148. WinJS.Utilities.query("#featureLabel")[0].textContent = SdkSample.sampleTitle;
  149. }
  150. WinJS.Application.addEventListener("activated", activated, false);
  151. // Export public methods & controls
  152. WinJS.Namespace.define("SdkSample", {
  153. ScenarioInput: ScenarioInput,
  154. ScenarioOutput: ScenarioOutput
  155. });
  156. // SDK Sample Test helper
  157. document.TestSdkSample = {
  158. getLastError: function () {
  159. return lastError;
  160. },
  161. getLastStatus: function () {
  162. return lastStatus;
  163. },
  164. selectScenario: function (scenarioID) {
  165. scenarioID = scenarioID >> 0;
  166. var select = document.getElementById("scenarioSelect");
  167. var newUrl = select.options[scenarioID - 1].value;
  168. WinJS.Navigation.navigate(newUrl);
  169. }
  170. };
  171. })();