////////////////////////////////////////////////////////////
//// © Microsoft. All rights reserved. ////
////////////////////////////////////////////////////////////
///
///
(function (BaseNS) {
var AppNS = WinJS.Namespace.defineWithParent(BaseNS, "Paint", {
ViewManager: WinJS.Class.define(function (host, views) {
this._host = host;
this._loadedViews = [];
if (typeof views === "object") {
this._views = views;
} else {
throw new Error("views parameter must be object");
}
},
{
// Public members
load: function(view, state) {
///
/// Load a view into the main section
///
///
/// String key for the view to be loaded
///
///
/// State object to be passed to the view
///
if (this._views[view]) {
var that = this;
if (this._views[view].uri) {
WinJS.UI.Fragments.clone(this._views[view].uri, state)
.then(function (frag) {
that._host.appendChild(frag);
if(that._views[view].setupFunction) {
var setup = WinJS.Utilities.getMember(that._views[view].setupFunction, null);
var persistedData = AppNS.StateManager.getValueAsObject(view);
setup(that._host, persistedData);
that._loadedViews.push(view);
}
document.body.focus();
});
} else {
throw new Error("View does not have a uri property");
}
} else {
throw new Error("View is not defined in the view list");
}
},
persistViewStates: function () {
///
/// Persists the loaded view states
///
var persistedData = {};
for (var viewIndex in this._loadedViews) {
var view = this._loadedViews[viewIndex];
var viewNamespace = WinJS.Utilities.getMember(this._views[view].namespace);
if (viewNamespace) {
var getViewData = viewNamespace.getViewData;
var getViewBlobs = viewNamespace.getViewBlobs;
if (getViewData && typeof getViewData === "function") {
var viewBlobs = getViewBlobs();
var viewData = getViewData();
// Write blobs to local storage
for (var blob in viewBlobs) {
AppNS.Utils.writeBlobToLocalFolderAsync(viewBlobs[blob], blob)
.then(function () { }, function () { });
}
// Persist data
AppNS.StateManager.storeValue(view, viewData);
} else {
throw new Error("View does not implement getViewData function");
}
} else {
throw new Error("Invalid namespace for view");
}
}
},
changeViewsLayout: function (newLayout) {
///
/// Changes the layout that the views are displaying
///
for (var viewIndex in this._loadedViews) {
var view = this._loadedViews[viewIndex];
var viewNamespace = WinJS.Utilities.getMember(this._views[view].namespace);
if (viewNamespace) {
var changeLayout = viewNamespace.changeLayout;
if (changeLayout && typeof changeLayout === "function") {
changeLayout(newLayout);
} else {
throw new Error("View does not implement changeLayout function");
}
} else {
throw new Error("Invalid namespace for view");
}
}
}
}
)});
})(Microsoft);