radio/libjs/wController/imagePane.js

140 lines
4.1 KiB
JavaScript

"use strict";
var Address = require("../wType/address");
var Vocabulary = require("./vocabulary");
var File = require("./file/file");
var LM = require("./localModel");
var ImagePane = Vocabulary.inherit({
"className": "ImagePane",
"constructor": function(addr) {
Vocabulary.fn.constructor.call(this, addr);
this._actions = [];
this._hasPageLink = false;
this._hasImage = false;
this.image = null;
},
"_actionActivate": function(name) {
var actObj = standardActions[name];
actObj.handler(this);
},
"addAction": function(action) {
var type = action.at("type").valueOf();
var obj = Object.create(null);
var supported = true;
obj.type = type;
switch (type) {
case 0:
var act = action.at("action").toString();
var actObj = standardActions[act];
if (actObj === undefined) {
this.trigger("seviceMessage", "Action " + act + " is not supported in ImagePanel, skipping");
supported = false;
}
var model = new LM(actionProps, "");
model.enabled = true;
model.hasLabel = true;
model.label = new LM(actionLabelProps, actObj.name);
model.addController(model.label);
model.activate = this._actionActivate.bind(this, act);
obj.model = model;
obj.action = act;
break;
}
if (supported) {
this._actions.push(obj);
this.trigger("addAction", obj.model);
}
},
"addElement": function(key, element) {
switch (key) {
case "image":
if (!this._hasImage) {
this._hasImage = true;
this.image = new File(new Address(["images", element.toString()]));
this.addForeignController("Corax", this.image);
}
break;
case "hasPageLink":
this._hasPageLink = element.valueOf();
break;
case "actions":
var size = element.length();
for (var i = 0; i < size; ++i) {
this.addAction(element.at(i));
}
}
Vocabulary.fn.addElement.call(this, key, element);
},
"getActions": function() {
var models = [];
for (var i = 0; i < this._actions.length; ++i) {
models.push(this._actions[i].model);
}
return models;
},
"getPageLink": function() {
if (this._hasPageLink) {
return this.data.at("pageLink").clone();
} else {
throw new Error("An attempt to request a page link from pane which doesn't have it");
}
},
"hasImage": function() {
return this._hasImage;
},
"hasPageLink": function() {
return this._hasPageLink;
},
"removeElement": function(key) {
Vocabulary.fn.removeElement.call(this, key);
if (key === "image" && this._hasImage) {
this.removeForeignController(this.image);
this._hasImage = false;
this.image.destructor();
this.image = null;
}
}
});
var standardActions = {
"play": {
handler: function (obj) {
var id = obj._pairAddress.back(); //todo it's a kind of crutch, need to do something about it in the future
window.play(id);
id.destructor();
},
name: "Play"
},
"scheduledToPlay": {
handler: function(obj) {
var id = obj._pairAddress.back(); //todo it's a kind of crutch, need to do something about it in the future
window.scheduleToPlay(id);
id.destructor();
},
name: "Schedule"
}
};
var actionProps = {
"backgroundColor": "primaryColor"
};
var actionLabelProps = {
"fontFamily": "casualFont",
"fontSize": "casualFontSize",
"color": "primaryFontColor"
}
module.exports = ImagePane;