Path: blob/master/samples/winrt/JavaScript/sample-utils/sample-utils.js
16338 views
//// Copyright (c) Microsoft Corporation. All rights reserved12// 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.45(function () {67//8// Helper controls used in the sample pages9//1011// The ScenarioInput control inserts the appropriate markup to get labels & controls12// hooked into the input section of a scenario page so that it's not repeated in13// every one.1415var lastError = "";16var lastStatus = "";17var ScenarioInput = WinJS.Class.define(18function (element, options) {19element.winControl = this;20this.element = element;2122new WinJS.Utilities.QueryCollection(element)23.setAttribute("role", "main")24.setAttribute("aria-labelledby", "inputLabel");25element.id = "input";2627this.addInputLabel(element);28this.addDetailsElement(element);29this.addScenariosPicker(element);30}, {31addInputLabel: function (element) {32var label = document.createElement("h2");33label.textContent = "Input";34label.id = "inputLabel";35element.parentNode.insertBefore(label, element);36},37addScenariosPicker: function (parentElement) {38var scenarios = document.createElement("div");39scenarios.id = "scenarios";40var control = new ScenarioSelect(scenarios);4142parentElement.insertBefore(scenarios, parentElement.childNodes[0]);43},4445addDetailsElement: function (sourceElement) {46var detailsDiv = this._createDetailsDiv();47while (sourceElement.childNodes.length > 0) {48detailsDiv.appendChild(sourceElement.removeChild(sourceElement.childNodes[0]));49}50sourceElement.appendChild(detailsDiv);51},52_createDetailsDiv: function () {53var detailsDiv = document.createElement("div");5455new WinJS.Utilities.QueryCollection(detailsDiv)56.addClass("details")57.setAttribute("role", "region")58.setAttribute("aria-labelledby", "descLabel")59.setAttribute("aria-live", "assertive");6061var label = document.createElement("h3");62label.textContent = "Description";63label.id = "descLabel";6465detailsDiv.appendChild(label);66return detailsDiv;67},68}69);7071// The ScenarioOutput control inserts the appropriate markup to get labels & controls72// hooked into the output section of a scenario page so that it's not repeated in73// every one.7475var ScenarioOutput = WinJS.Class.define(76function (element, options) {77element.winControl = this;78this.element = element;79new WinJS.Utilities.QueryCollection(element)80.setAttribute("role", "region")81.setAttribute("aria-labelledby", "outputLabel")82.setAttribute("aria-live", "assertive");83element.id = "output";8485this._addOutputLabel(element);86this._addStatusOutput(element);87}, {88_addOutputLabel: function (element) {89var label = document.createElement("h2");90label.id = "outputLabel";91label.textContent = "Output";92element.parentNode.insertBefore(label, element);93},94_addStatusOutput: function (element) {95var statusDiv = document.createElement("div");96statusDiv.id = "statusMessage";97statusDiv.setAttribute("role", "textbox");98element.insertBefore(statusDiv, element.childNodes[0]);99}100}101);102103104// Sample infrastructure internals105106var currentScenarioUrl = null;107108WinJS.Navigation.addEventListener("navigating", function (evt) {109currentScenarioUrl = evt.detail.location;110});111112WinJS.log = function (message, tag, type) {113var isError = (type === "error");114var isStatus = (type === "status");115116if (isError || isStatus) {117var statusDiv = /* @type(HTMLElement) */ document.getElementById("statusMessage");118if (statusDiv) {119statusDiv.innerText = message;120if (isError) {121lastError = message;122statusDiv.style.color = "blue";123} else if (isStatus) {124lastStatus = message;125statusDiv.style.color = "green";126}127}128}129};130131// Control that populates and runs the scenario selector132133var ScenarioSelect = WinJS.UI.Pages.define("/sample-utils/scenario-select.html", {134ready: function (element, options) {135var that = this;136var selectElement = WinJS.Utilities.query("#scenarioSelect", element);137this._selectElement = selectElement[0];138139SdkSample.scenarios.forEach(function (s, index) {140that._addScenario(index, s);141});142143selectElement.listen("change", function (evt) {144var select = evt.target;145if (select.selectedIndex >= 0) {146var newUrl = select.options[select.selectedIndex].value;147WinJS.Navigation.navigate(newUrl);148}149});150selectElement[0].size = (SdkSample.scenarios.length > 5 ? 5 : SdkSample.scenarios.length);151if (SdkSample.scenarios.length === 1) {152// Avoid showing down arrow when there is only one scenario153selectElement[0].setAttribute("multiple", "multiple");154}155156// Use setImmediate to ensure that the select element is set as active only after157// the scenario page has been constructed.158setImmediate(function () {159that._selectElement.setActive();160});161},162163_addScenario: function (index, info) {164var option = document.createElement("option");165if (info.url === currentScenarioUrl) {166option.selected = "selected";167}168option.text = (index + 1) + ") " + info.title;169option.value = info.url;170this._selectElement.appendChild(option);171}172});173174function activated(e) {175WinJS.Utilities.query("#featureLabel")[0].textContent = SdkSample.sampleTitle;176}177178WinJS.Application.addEventListener("activated", activated, false);179180// Export public methods & controls181WinJS.Namespace.define("SdkSample", {182ScenarioInput: ScenarioInput,183ScenarioOutput: ScenarioOutput184});185186// SDK Sample Test helper187document.TestSdkSample = {188getLastError: function () {189return lastError;190},191192getLastStatus: function () {193return lastStatus;194},195196selectScenario: function (scenarioID) {197scenarioID = scenarioID >> 0;198var select = document.getElementById("scenarioSelect");199var newUrl = select.options[scenarioID - 1].value;200WinJS.Navigation.navigate(newUrl);201}202};203})();204205206