1
//// Copyright (c) Microsoft Corporation. All rights reserved
3
// This file is a part of the SDK sample framework. For code demonstrating scenarios in this particular sample,
4
// please see the html, css and js folders.
9
// Helper controls used in the sample pages
12
// The ScenarioInput control inserts the appropriate markup to get labels & controls
13
// hooked into the input section of a scenario page so that it's not repeated in
18
var ScenarioInput = WinJS.Class.define(
19
function (element, options) {
20
element.winControl = this;
21
this.element = element;
23
new WinJS.Utilities.QueryCollection(element)
24
.setAttribute("role", "main")
25
.setAttribute("aria-labelledby", "inputLabel");
28
this.addInputLabel(element);
29
this.addDetailsElement(element);
30
this.addScenariosPicker(element);
32
addInputLabel: function (element) {
33
var label = document.createElement("h2");
34
label.textContent = "Input";
35
label.id = "inputLabel";
36
element.parentNode.insertBefore(label, element);
38
addScenariosPicker: function (parentElement) {
39
var scenarios = document.createElement("div");
40
scenarios.id = "scenarios";
41
var control = new ScenarioSelect(scenarios);
43
parentElement.insertBefore(scenarios, parentElement.childNodes[0]);
46
addDetailsElement: function (sourceElement) {
47
var detailsDiv = this._createDetailsDiv();
48
while (sourceElement.childNodes.length > 0) {
49
detailsDiv.appendChild(sourceElement.removeChild(sourceElement.childNodes[0]));
51
sourceElement.appendChild(detailsDiv);
53
_createDetailsDiv: function () {
54
var detailsDiv = document.createElement("div");
56
new WinJS.Utilities.QueryCollection(detailsDiv)
58
.setAttribute("role", "region")
59
.setAttribute("aria-labelledby", "descLabel")
60
.setAttribute("aria-live", "assertive");
62
var label = document.createElement("h3");
63
label.textContent = "Description";
64
label.id = "descLabel";
66
detailsDiv.appendChild(label);
72
// The ScenarioOutput control inserts the appropriate markup to get labels & controls
73
// hooked into the output section of a scenario page so that it's not repeated in
76
var ScenarioOutput = WinJS.Class.define(
77
function (element, options) {
78
element.winControl = this;
79
this.element = element;
80
new WinJS.Utilities.QueryCollection(element)
81
.setAttribute("role", "region")
82
.setAttribute("aria-labelledby", "outputLabel")
83
.setAttribute("aria-live", "assertive");
84
element.id = "output";
86
this._addOutputLabel(element);
87
this._addStatusOutput(element);
89
_addOutputLabel: function (element) {
90
var label = document.createElement("h2");
91
label.id = "outputLabel";
92
label.textContent = "Output";
93
element.parentNode.insertBefore(label, element);
95
_addStatusOutput: function (element) {
96
var statusDiv = document.createElement("div");
97
statusDiv.id = "statusMessage";
98
statusDiv.setAttribute("role", "textbox");
99
element.insertBefore(statusDiv, element.childNodes[0]);
105
// Sample infrastructure internals
107
var currentScenarioUrl = null;
109
WinJS.Navigation.addEventListener("navigating", function (evt) {
110
currentScenarioUrl = evt.detail.location;
113
WinJS.log = function (message, tag, type) {
114
var isError = (type === "error");
115
var isStatus = (type === "status");
117
if (isError || isStatus) {
118
var statusDiv = /* @type(HTMLElement) */ document.getElementById("statusMessage");
120
statusDiv.innerText = message;
123
statusDiv.style.color = "blue";
124
} else if (isStatus) {
125
lastStatus = message;
126
statusDiv.style.color = "green";
132
// Control that populates and runs the scenario selector
134
var ScenarioSelect = WinJS.UI.Pages.define("/sample-utils/scenario-select.html", {
135
ready: function (element, options) {
137
var selectElement = WinJS.Utilities.query("#scenarioSelect", element);
138
this._selectElement = selectElement[0];
140
SdkSample.scenarios.forEach(function (s, index) {
141
that._addScenario(index, s);
144
selectElement.listen("change", function (evt) {
145
var select = evt.target;
146
if (select.selectedIndex >= 0) {
147
var newUrl = select.options[select.selectedIndex].value;
148
WinJS.Navigation.navigate(newUrl);
151
selectElement[0].size = (SdkSample.scenarios.length > 5 ? 5 : SdkSample.scenarios.length);
152
if (SdkSample.scenarios.length === 1) {
153
// Avoid showing down arrow when there is only one scenario
154
selectElement[0].setAttribute("multiple", "multiple");
157
// Use setImmediate to ensure that the select element is set as active only after
158
// the scenario page has been constructed.
159
setImmediate(function () {
160
that._selectElement.setActive();
164
_addScenario: function (index, info) {
165
var option = document.createElement("option");
166
if (info.url === currentScenarioUrl) {
167
option.selected = "selected";
169
option.text = (index + 1) + ") " + info.title;
170
option.value = info.url;
171
this._selectElement.appendChild(option);
175
function activated(e) {
176
WinJS.Utilities.query("#featureLabel")[0].textContent = SdkSample.sampleTitle;
179
WinJS.Application.addEventListener("activated", activated, false);
181
// Export public methods & controls
182
WinJS.Namespace.define("SdkSample", {
183
ScenarioInput: ScenarioInput,
184
ScenarioOutput: ScenarioOutput
187
// SDK Sample Test helper
188
document.TestSdkSample = {
189
getLastError: function () {
193
getLastStatus: function () {
197
selectScenario: function (scenarioID) {
198
scenarioID = scenarioID >> 0;
199
var select = document.getElementById("scenarioSelect");
200
var newUrl = select.options[scenarioID - 1].value;
201
WinJS.Navigation.navigate(newUrl);