From 46560924cefedf8f1db2c5f13177f2e08b5f37a9 Mon Sep 17 00:00:00 2001 From: blue Date: Sun, 2 Sep 2018 22:22:28 +0300 Subject: [PATCH] A draft of player controller in libjs --- libjs/wController/CMakeLists.txt | 1 + libjs/wController/player.js | 118 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 libjs/wController/player.js diff --git a/libjs/wController/CMakeLists.txt b/libjs/wController/CMakeLists.txt index 96b4f71..7f30e50 100644 --- a/libjs/wController/CMakeLists.txt +++ b/libjs/wController/CMakeLists.txt @@ -17,3 +17,4 @@ configure_file(localModel.js localModel.js) configure_file(catalogue.js catalogue.js) configure_file(imagePane.js imagePane.js) configure_file(file/file.js file/file.js) +configure_file(player.js player.js) diff --git a/libjs/wController/player.js b/libjs/wController/player.js new file mode 100644 index 0000000..ccb015f --- /dev/null +++ b/libjs/wController/player.js @@ -0,0 +1,118 @@ +"use strict"; + +var Controller = require("./controller"); +var Button = require("./button"); +var Vocabulary = require("./vocabulary"); +var Enum = require("../utils/enum"); + +var Player = Controller.inherit({ + className: "Player", + constructor: function(addr) { + Controller.fn.constructor.call(this, addr); + + this.controls = Object.create(null); + this.views = Object.create(null); + + this.addHandler("get"); + this.addHandler("viewsChange"); + }, + _addControl: function(type, address) { + var t = type.valueOf(); + + if (this.controls[t] !== undefined) { + throw new Error("An attempt to add multiple instances of " + ItemType.reversed[t] + " into Player"); + } + + if (ItemType.reversed[t] !== undefined) { + switch (t) { + case ItemType.straight.playPause: + var btn = new Button(address.clone()); + btn.itemType = t; + this.controls[t] = btn; + this.addController(btn); + break; + default: + this.trigger("serviceMessage", "An attempt to add ItemType " + ItemType.reversed[t] + " to controls of the Player, but it's not qualified to be a control"); + } + } else { + this.trigger("serviceMessage", "An unrecgnized item ItemType in Player: " + t); + } + }, + _addView: function(type, address) { + var t = type.valueOf(); + + if (this.views[t] !== undefined) { + throw new Error("An attempt to add multiple instances of " + ItemType.reversed[t] + " into Player"); + } + + if (ItemType.reversed[t] !== undefined) { + switch (t) { + case ItemType.straight.queue: + this.trigger("serviceMessage", "Queue is not supported yet in Player"); + break; + case ItemType.straight.currentPlayback: + var cpb = new Vocabulary(address.clone()); + cpb.ItemType = t; + this.views[t] = cpb; + this.addController(cpb); + default: + this.trigger("serviceMessage", "An attempt to add ItemType " + ItemType.reversed[t] + " to views of the Player, but it's not qualified to be a view"); + } + } else { + this.trigger("serviceMessage", "An unrecgnized item ItemType in Player: " + t); + } + }, + _h_get: function(ev) { + var data = ev.getData(); + + var controls = data.at("controls"); + var views = data.at("views"); + var size, i, vc; + + size = controls.length(); + for (i = 0; i < size; ++i) { + vc = controls.at(i); + this._addControl(vc.at("type"), vc.at("address")); + } + + size = views.length(); + for (i = 0; i < size; ++i) { + vc = views.at(i); + this._addview(vc.at("type"), vc.at("address")); + } + }, + _h_viewsChange: function(ev) { + var data = ev.getData(); + + var add = data.at("add"); + var remove = data.at("remove"); + var size, i, vc; + + size = remove.length(); + for (i = 0; i < size; ++i) { + this._removeView(remove.at(i)); + } + + size = add.length(); + for (i = 0; i < size; ++i) { + vc = add.at(i); + this._addview(vc.at("type"), vc.at("address")); + } + }, + _removeControl: function(type) { + //TODO + }, + _removeView: function(type) { + //TODO + } + +}); + +var ItemType = new Enum("ItemType"); +ItemType.add("playPause"); +ItemType.add("currentPlayback"); +ItemType.add("queue"); + +Player.ItemType = ItemType; + +module.exports = Player;