/*global Class Application Client MOCK_FAVES_CONFIG */
//= require core/mock_config.js
// Mock client interface
window.Mock = Class.extend({
initialize: function(client) {
this.client = client;
window.addEventListener("load", $.proxy(this._makeToolbar, this), true);
},
_makeToolbar: function(){
var toolbar = document.createElement("div");
toolbar.id = "mock-toolbar-container";
// CSS is set in `mock.css`
toolbar.innerHTML = "
";
document.body.insertBefore(toolbar, document.body.firstChild);
this.toolbar = toolbar;
}
});
var Mock = new Mock(Client);
Client.makeButtons = function(str) {
if (!str) return;
var specs = str.split(":");
if (specs.length !== 2) {
alert("There should be 2 segments to a buttons specification");
} else {
makeButtonsForSection(specs[0], "mock-left");
makeButtonsForSection(specs[1], "mock-right");
}
function makeButtonsForSection(str, id) {
$('#'+id).hide();
if (str !== "") {
$('#'+id).show();
if (str === "NudgeNav") {
// Support for nudge nav in mock toolbar.
$('#'+id).html("↑ ↓");
} else {
$('#'+id).html("" + str + "");
}
}
}
};
Client.notify = function(obj) {
if (typeof obj.buttons !== 'undefined') {
this.makeButtons(obj.buttons);
}
if (typeof obj.title !== 'undefined' && typeof obj.action !== 'undefined' && obj.action !== 'share') {
this.makeTitle(obj.title);
}
if (obj.action === "getFavesAndNotifs") {
Client.callback("favesAndNotifs", MOCK_FAVES_CONFIG);
}
// CFB2 only
if (obj.action === "alert") {
alert(obj.heading + "\n" + obj.text);
}
// Footer nav for CFB2 & NFL3 only
if (obj.action === "navigateTo") {
if (obj.location === 'home') {
document.location = 'zcanvas.html';
} else {
alert('Pretend you have navigated to: ' + obj.location + '.');
}
}
// Client.notify logs notifications to the console.
if (window.console && window.console.log) {
var arr = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
arr.push(prop + ": " + obj[prop]);
}
}
console.log("))) Client.notify({ " + arr.join(", ") + " })");
}
};
// Set the view title
Client.makeTitle = function(title) {
$("#mock-center").html(title);
};
setTimeout(function() {
$('#mock-toolbar').on('click', 'action', function(e) {
var name = $(e.target).attr('name');
var value = $(e.target).attr('value');
Application.onClientCallback(name, value);
});
}, 400);
Application.clientCallbackHandlers.push(function(name, value) {
if (value === 'Back') history.back();
if (value === 'Menu') history.back();
if (value === 'Close') Client.callback('close');
});