APIs

Show:
  1. /**
  2. The Channel class is created under, and managed by, a timeline instance.
  3. So if for example a timeline has three channels, each channel would have a corresponding channel instance referenced within the timeline instance.
  4. Also, a channel creates, and holds a reference to, all the blocks that are contained within that channel, via the block_id.
  5. @class Channel
  6. @constructor
  7. @param {string} i_campaign_timeline_chanel_id
  8. @return {object} Channel instantiated
  9. **/
  10. define(['jquery', 'backbone', 'X2JS', 'BlockImage', 'BlockSVG', 'BlockVideo', 'BlockScene'], function ($, Backbone, X2JS, BlockImage, BlockSVG, BlockVideo, BlockScene) {
  11.  
  12. /**
  13. Event fires when a channel is selected on a timeline. The event includes the channel id that was selected.
  14. @event CAMPAIGN_TIMELINE_CHANNEL_SELECTED
  15. @param {This} caller
  16. @param {Event} campaign_timeline_channel_id
  17. @static
  18. @final
  19. **/
  20. BB.EVENTS.CAMPAIGN_TIMELINE_CHANNEL_SELECTED = 'CAMPAIGN_TIMELINE_CHANNEL_SELECTED';
  21.  
  22. var Channel = BB.Controller.extend({
  23.  
  24. /**
  25. Constructor
  26. @method initialize
  27. **/
  28. initialize: function () {
  29. var self = this;
  30. self.m_campaign_timeline_chanel_id = this.options.campaignTimelineChanelID;
  31. self.m_selected = false;
  32. self.m_blocks = {}; // hold references to all created player instances
  33. self.m_property = BB.comBroker.getService(BB.SERVICES['PROPERTIES_VIEW']);
  34. self.m_blockFactory = BB.comBroker.getService(BB.SERVICES['BLOCK_FACTORY']);
  35. self._listenReset();
  36. if (self.m_blockFactory.blocksLoaded()) {
  37. self._onBlocksLoaded();
  38. } else {
  39. BB.comBroker.listenOnce(BB.EVENTS['BLOCKS_LOADED'], $.proxy(self._onBlocksLoaded, self));
  40. self.m_blockFactory.loadBlockModules();
  41. }
  42. },
  43.  
  44. /**
  45. When all block modules have loaded, begin creating blocks
  46. @method _onBlocksLoaded
  47. **/
  48. _onBlocksLoaded: function () {
  49. var self = this;
  50. self._createChannelBlocks();
  51. self._postInit();
  52. $(Elements.SELECTED_TIMELINE).fadeIn();
  53. },
  54.  
  55. /**
  56. After blocks loaded, continue initiliazation
  57. @method _postInit
  58. **/
  59. _postInit: function () {
  60. var self = this;
  61. self._onTimelineChannelSelected();
  62. self._listenRandomPlayback();
  63. self._listenRepeatToFit();
  64. self._propLoadChannel();
  65. self._listenResourceRemoving();
  66. self._listenSceneRemoving();
  67. //self._listenViewerRemoved();
  68. },
  69.  
  70. /**
  71. Listen to reset of when switching to different campaign so we forget current state
  72. @method _listenReset
  73. **/
  74. _listenReset: function () {
  75. var self = this;
  76. BB.comBroker.listenWithNamespace(BB.EVENTS.CAMPAIGN_RESET, self, function () {
  77. for (var blockID in self.m_blocks) {
  78. self.deleteBlock(blockID, true);
  79. }
  80. $(self.m_thumbsContainer).empty();
  81. self._reset();
  82. });
  83. },
  84.  
  85. /**
  86. Reset current state
  87. @method _reset
  88. **/
  89. _reset: function () {
  90. var self = this;
  91. $(Elements.RANDOM_PLAYBACK).off('change', self.m_randomPlaybackHandler);
  92. $(Elements.REPEAT_TO_FIT).off('change', self.m_repeatToFitPlaybackHandler);
  93. BB.comBroker.stopListenWithNamespace(BB.EVENTS.CAMPAIGN_RESET, self);
  94. BB.comBroker.stopListenWithNamespace(BB.EVENTS.REMOVING_RESOURCE, self);
  95. BB.comBroker.stopListenWithNamespace(BB.EVENTS.REMOVING_SCENE, self);
  96. BB.comBroker.stopListenWithNamespace(BB.EVENTS.CAMPAIGN_TIMELINE_CHANNEL_SELECTED, self);
  97. $.each(self, function (k) {
  98. self[k] = undefined;
  99. });
  100. },
  101.  
  102. /**
  103. Wire UI and listen to change in related UI (random playback on channel)
  104. @method _listenRandomPlayback
  105. @return none
  106. **/
  107. _listenRandomPlayback: function () {
  108. var self = this;
  109. self.m_randomPlaybackHandler = $(Elements.RANDOM_PLAYBACK).on('change', function (e) {
  110. if (!self.m_selected)
  111. return;
  112. self._onChangeRandomPlayback(e);
  113. });
  114. },
  115.  
  116. /**
  117. Wire UI and listen to change in related UI (random playback on channel)
  118. @method _listenRandomPlayback
  119. @return none
  120. **/
  121. _listenRepeatToFit: function () {
  122. var self = this;
  123. self.m_repeatToFitPlaybackHandler = $(Elements.REPEAT_TO_FIT).on('change', function (e) {
  124. if (!self.m_selected)
  125. return;
  126. self._onChangeRepeatToFit(e);
  127. });
  128. },
  129.  
  130.  
  131. /**
  132. On change in random playback value update msdb with new value.
  133. @method _onChangeRandomPlayback
  134. @param {Event} e
  135. @return none
  136. **/
  137. _onChangeRandomPlayback: function (e) {
  138. var self = this;
  139. var state = $(Elements.RANDOM_PLAYBACK + ' option:selected').val() == "on" ? 'True' : 'False';
  140. pepper.setCampaignTimelineChannelRecord(self.m_campaign_timeline_chanel_id, 'random_order', state)
  141. },
  142.  
  143. /**
  144. On change in repeat to fit value update msdb with new value.
  145. @method _onChangeRepeatToFit
  146. @param {Event} e
  147. @return none
  148. **/
  149. _onChangeRepeatToFit: function (e) {
  150. var self = this;
  151. var state = $(Elements.REPEAT_TO_FIT + ' option:selected').val() == "on" ? 'True' : 'False';
  152. pepper.setCampaignTimelineChannelRecord(self.m_campaign_timeline_chanel_id, 'repeat_to_fit', state)
  153. },
  154.  
  155. /**
  156. Listen to when a resource removing from resources so we can remove corresponding blocks
  157. @method _listenResourceRemoving
  158. @return none
  159. **/
  160. _listenResourceRemoving: function () {
  161. var self = this;
  162. BB.comBroker.listenWithNamespace(BB.EVENTS.REMOVING_RESOURCE, self, function (e) {
  163. var removingResourceID = e.edata;
  164. for (var blockID in self.m_blocks) {
  165. if (self.m_blocks[blockID] instanceof BlockImage || self.m_blocks[blockID] instanceof BlockSVG || self.m_blocks[blockID] instanceof BlockVideo) {
  166. if (removingResourceID == self.m_blocks[blockID].getResourceID()) {
  167. self.deleteBlock(blockID);
  168. }
  169. }
  170. }
  171. });
  172. },
  173.  
  174. /**
  175. Listen to when a scene removing from scenes so we can remove corresponding blocks
  176. @method _listenSceneRemoving
  177. @return none
  178. **/
  179. _listenSceneRemoving: function () {
  180. var self = this;
  181. BB.comBroker.listenWithNamespace(BB.EVENTS.REMOVING_SCENE, self, function (e) {
  182. var removingSceneID = e.edata;
  183. for (var blockID in self.m_blocks) {
  184. if (self.m_blocks[blockID] instanceof BlockScene) {
  185. var sceneID = self.m_blocks[blockID].getChannelBlockSceneID();
  186. if (removingSceneID == sceneID) {
  187. self.deleteBlock(blockID);
  188. }
  189. }
  190. }
  191. });
  192. },
  193.  
  194. /**
  195. Update the properties panel with the state of random playback.
  196. @method _propLoadChannel
  197. @return none
  198. **/
  199. _propLoadChannel: function () {
  200. var self = this;
  201.  
  202. var recChannel = pepper.getCampaignTimelineChannelRecord(self.m_campaign_timeline_chanel_id);
  203. var stateRandom = recChannel['random_order'] == 'True' ? 'on' : 'off';
  204. var stateRepeat = recChannel['repeat_to_fit'] == 'True' ? 'on' : 'off';
  205. //$(Elements.RANDOM_PLAYBACK + ' option[value=' + stateRandom + ']').attr("selected", "selected");
  206. //$(Elements.REPEAT_TO_FIT + ' option[value=' + stateRepeat + ']').attr("selected", "selected");
  207. $(Elements.RANDOM_PLAYBACK).selectpicker('val',stateRandom);
  208. $(Elements.REPEAT_TO_FIT).selectpicker('val',stateRepeat);
  209. self.m_property.selectView(Elements.CHANNEL_PROPERTIES);
  210. },
  211.  
  212. /**
  213. Listen to even when timeline is selected and it it's this instance's channel_id, populate properties panel.
  214. @method _onTimelineChannelSelected
  215. @return none
  216. **/
  217. _onTimelineChannelSelected: function () {
  218. var self = this;
  219. BB.comBroker.listenWithNamespace(BB.EVENTS.CAMPAIGN_TIMELINE_CHANNEL_SELECTED, self, function (e) {
  220. var channelID = e.edata;
  221. if (self.m_campaign_timeline_chanel_id != channelID) {
  222. self.m_selected = false;
  223. return;
  224. }
  225. self.m_selected = true;
  226. // log('channel selected ' + self.m_campaign_timeline_chanel_id);
  227. self._propLoadChannel();
  228. });
  229. },
  230.  
  231. /**
  232. Create blocks instances for all the channel's blocs (i.e.: players / resources).
  233. @method _createChannelBlocks
  234. @return none
  235. **/
  236. _createChannelBlocks: function () {
  237. var self = this;
  238. var blockIDs = pepper.getChannelBlocks(self.m_campaign_timeline_chanel_id);
  239. for (var i = 0; i < blockIDs.length; i++) {
  240. var blockID = blockIDs[i];
  241. var recBlock = pepper.getBlockRecord(blockID);
  242. self.createChannelBlock(blockID, recBlock['player_data'])
  243. }
  244. BB.comBroker.fire(BB.EVENTS.CAMPAIGN_TIMELINE_CHANGED, self);
  245. },
  246.  
  247. /**
  248. This method produces block instances which will reside on the timeline and referenced within this
  249. channel instance.
  250. @method createChannelBlock
  251. @param {Number} i_campaign_timeline_chanel_player_id
  252. @param {XML} i_playerData
  253. @return {Object} reference to the block instance
  254. **/
  255. createChannelBlock: function (i_campaign_timeline_chanel_player_id, i_player_data) {
  256. var self = this;
  257. var blockFactory = BB.comBroker.getService(BB.SERVICES.BLOCK_FACTORY);
  258. var blockID = parseInt(i_campaign_timeline_chanel_player_id);
  259. self.m_blocks[blockID] = blockFactory.createBlock(blockID, i_player_data, BB.CONSTS.PLACEMENT_CHANNEL);
  260. return self.m_blocks[blockID];
  261. },
  262.  
  263. /**
  264. Get all blocks that belong to this channel instance but push them into an array so they are properly sorted by player offset time.
  265. @method getBlocks
  266. @return {Object} blocksSorted
  267. **/
  268. getBlocks: function () {
  269. var self = this;
  270. var blocksSorted = [];
  271. for (var block_id in self.m_blocks) {
  272. var recBlock = pepper.getBlockRecord(block_id);
  273. var player_data = pepper.getBlockRecord(block_id)['player_data'];
  274. var domPlayerData = $.parseXML(player_data);
  275. var sceneHandle = $(domPlayerData).find('Player').attr('player');
  276. // workaround to remove scenes listed inside table campaign_timeline_chanel_players
  277. if (sceneHandle == '3510')
  278. continue;
  279. var offsetTime = parseInt(recBlock['player_offset_time']);
  280. blocksSorted[offsetTime] = self.m_blocks[block_id];
  281. }
  282. return blocksSorted;
  283. },
  284.  
  285. /**
  286. Return a block instance for the selected i_campaign_timeline_chanel_player_id (i.e.: block_id).
  287. @method getBlockInstance
  288. @param {Number} i_campaign_timeline_chanel_player_id
  289. @return {Object} reference to the block instance
  290. **/
  291. getBlockInstance: function (i_campaign_timeline_chanel_player_id) {
  292. var self = this;
  293. return self.m_blocks[i_campaign_timeline_chanel_player_id];
  294. },
  295.  
  296. /**
  297. Delete this channel and all of it's related blocks
  298. @method deleteChannel
  299. @return none
  300. **/
  301. deleteChannel: function () {
  302. var self = this;
  303. pepper.removeChannelFromTimeline(self.m_campaign_timeline_chanel_id);
  304. for (var blockID in self.m_blocks) {
  305. self.deleteBlock(blockID);
  306. }
  307. self._reset();
  308. },
  309.  
  310. /**
  311. Delete a block from the channel
  312. @method deleteBlock
  313. @param {Number} i_block_id
  314. @params {Boolean} i_memoryOnly if true only remove from existance but not from msdb
  315. @return none
  316. **/
  317. deleteBlock: function (i_block_id, i_memoryOnly) {
  318. var self = this;
  319. self.m_blocks[i_block_id].deleteBlock(i_memoryOnly);
  320. delete self.m_blocks[i_block_id];
  321. }
  322. });
  323.  
  324. return Channel;
  325. });