• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// Copyright (c) Microsoft Corporation. All rights reserved
2
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.
5
6(function () {
7
8    //
9    // Helper controls used in the sample pages
10    //
11
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
14    // every one.
15
16    var lastError = "";
17    var lastStatus = "";
18    var ScenarioInput = WinJS.Class.define(
19        function (element, options) {
20        element.winControl = this;
21        this.element = element;
22
23        new WinJS.Utilities.QueryCollection(element)
24                    .setAttribute("role", "main")
25                    .setAttribute("aria-labelledby", "inputLabel");
26        element.id = "input";
27
28        this.addInputLabel(element);
29        this.addDetailsElement(element);
30        this.addScenariosPicker(element);
31    }, {
32        addInputLabel: function (element) {
33            var label = document.createElement("h2");
34            label.textContent = "Input";
35            label.id = "inputLabel";
36            element.parentNode.insertBefore(label, element);
37        },
38        addScenariosPicker: function (parentElement) {
39            var scenarios = document.createElement("div");
40            scenarios.id = "scenarios";
41            var control = new ScenarioSelect(scenarios);
42
43            parentElement.insertBefore(scenarios, parentElement.childNodes[0]);
44        },
45
46        addDetailsElement: function (sourceElement) {
47            var detailsDiv = this._createDetailsDiv();
48            while (sourceElement.childNodes.length > 0) {
49                detailsDiv.appendChild(sourceElement.removeChild(sourceElement.childNodes[0]));
50            }
51            sourceElement.appendChild(detailsDiv);
52        },
53        _createDetailsDiv: function () {
54            var detailsDiv = document.createElement("div");
55
56            new WinJS.Utilities.QueryCollection(detailsDiv)
57                        .addClass("details")
58                        .setAttribute("role", "region")
59                        .setAttribute("aria-labelledby", "descLabel")
60                        .setAttribute("aria-live", "assertive");
61
62            var label = document.createElement("h3");
63            label.textContent = "Description";
64            label.id = "descLabel";
65
66            detailsDiv.appendChild(label);
67            return detailsDiv;
68        },
69    }
70    );
71
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
74    // every one.
75
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";
85
86        this._addOutputLabel(element);
87        this._addStatusOutput(element);
88    }, {
89        _addOutputLabel: function (element) {
90            var label = document.createElement("h2");
91            label.id = "outputLabel";
92            label.textContent = "Output";
93            element.parentNode.insertBefore(label, element);
94        },
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]);
100        }
101    }
102    );
103
104
105    // Sample infrastructure internals
106
107    var currentScenarioUrl = null;
108
109    WinJS.Navigation.addEventListener("navigating", function (evt) {
110        currentScenarioUrl = evt.detail.location;
111    });
112
113    WinJS.log = function (message, tag, type) {
114        var isError = (type === "error");
115        var isStatus = (type === "status");
116
117        if (isError || isStatus) {
118            var statusDiv = /* @type(HTMLElement) */ document.getElementById("statusMessage");
119            if (statusDiv) {
120                statusDiv.innerText = message;
121                if (isError) {
122                    lastError = message;
123                    statusDiv.style.color = "blue";
124                } else if (isStatus) {
125                    lastStatus = message;
126                    statusDiv.style.color = "green";
127                }
128            }
129        }
130    };
131
132    // Control that populates and runs the scenario selector
133
134    var ScenarioSelect = WinJS.UI.Pages.define("/sample-utils/scenario-select.html", {
135        ready: function (element, options) {
136            var that = this;
137            var selectElement = WinJS.Utilities.query("#scenarioSelect", element);
138            this._selectElement = selectElement[0];
139
140            SdkSample.scenarios.forEach(function (s, index) {
141                that._addScenario(index, s);
142            });
143
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);
149                }
150            });
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");
155            }
156
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();
161            });
162        },
163
164        _addScenario: function (index, info) {
165            var option = document.createElement("option");
166            if (info.url === currentScenarioUrl) {
167                option.selected = "selected";
168            }
169            option.text = (index + 1) + ") " + info.title;
170            option.value = info.url;
171            this._selectElement.appendChild(option);
172        }
173    });
174
175    function activated(e) {
176        WinJS.Utilities.query("#featureLabel")[0].textContent = SdkSample.sampleTitle;
177    }
178
179    WinJS.Application.addEventListener("activated", activated, false);
180
181    // Export public methods & controls
182    WinJS.Namespace.define("SdkSample", {
183        ScenarioInput: ScenarioInput,
184        ScenarioOutput: ScenarioOutput
185    });
186
187    // SDK Sample Test helper
188    document.TestSdkSample = {
189        getLastError: function () {
190            return lastError;
191        },
192
193        getLastStatus: function () {
194            return lastStatus;
195        },
196
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);
202        }
203    };
204})();
205