1 var SNT_DEBUG = SNT_DEBUG || false; 2 3 4 /** 5 * @description helper singelton 6 */ 7 var Sonettic = Sonettic || {}; 8 Sonettic.Helper = (function(){ 9 /** 10 * @property 11 * @private 12 * @desc list of all private static constants 13 */ 14 var constants = { 15 EVENT_ADD : 'ADD_EVENT_LISTENER', 16 EVENT_RM : 'REMOVE_EVENT_LISTENER', 17 EVENT_INIT : 'INIT_STATE', 18 EVENT_REGISTER : 'PLAYER_REGISTER' 19 }; 20 21 var debugObject = null; 22 var debugTextObject = null; 23 24 return { 25 /** 26 * @description This method return constant value or null 27 * @param {String} name Name of constant 28 * @return Value of constant or null 29 */ 30 getConstant : function(name){ 31 return constants[name] || null; 32 }, 33 /** 34 * @description Get flash object 35 * @param id ID of the Player's flash object 36 * @return Object 37 */ 38 getFlashObject : function(id){ 39 Sonettic.Helper.debug('Getting object with id [' + id + ']'); 40 var isIE = navigator.appName.indexOf("Microsoft") != -1; 41 var obj = (isIE) ? window[id] : document[id]; 42 return obj; 43 }, 44 /** 45 * @description Debug message 46 * @param msg Message to debug 47 */ 48 debug : function(msg){ 49 if(SNT_DEBUG == false || SNT_DEBUG == null)return; 50 if(window.console && window.console.log){ 51 window.console.log(msg); 52 }else{ 53 // create element 54 debugObject = debugObject || document.getElementById('sonettic-debug'); 55 if(debugObject == null){ 56 var body = document.getElementsByTagName('body')[0]; 57 var debugWindow = document.createElement('div'); 58 59 var holderElement = '<form action=""><textarea id="sonettic-debug" name="snt-debug" ' + 60 'style="border:1px dashed #000; ' + 61 'display:block; position:absolute; bottom: 10px; ' + 62 'right: 10px; width: 500px; height:100px;' + 63 'background-color:#f8f8f8; border-top:15px solid #c8c8c8;' + 64 'overflow:auto; font-size:11px; font-family:sans-serif;">'; 65 debugWindow.innerHTML = holderElement + '</textarea></form>'; 66 body.appendChild(debugWindow); 67 } 68 69 // write to output 70 if(debugObject != null){ 71 debugObject.value = debugObject.value + '>> ' + msg + '\n'; 72 var doLen = debugObject.length; 73 debugObject.scrollTop = debugObject.scrollHeight; 74 } 75 } 76 } 77 } 78 })(); 79 80 /** 81 * 82 * @description Creates a new Sonettic Cinema Holder. 83 * @constructor 84 * @class 85 * @protected 86 * @version 0.1a 87 */ 88 var SntEventsHolder = (function(){ 89 90 /** 91 * @property 92 * @private 93 * @description a set with list of all registered players 94 */ 95 var players = {}; 96 97 /** 98 * @property 99 * @private 100 * @description a set with list of all registered FLASH objects 101 */ 102 var objs = {}; 103 104 /** 105 * @property 106 * @private 107 * @description a set with list of statuses of all players (boolean true or false) 108 * which registered from the flash object 109 */ 110 var status = {}; 111 112 /** 113 * @property 114 * @private 115 * @description a set with list of all unregistered players which 116 * initialized by Flash object but not initialized by JavaScript 117 */ 118 var unregistered = {}; 119 120 121 /** 122 * @function 123 * @private 124 * @description This is static private method to get an item from the array by its name 125 * @param {Array} a Source set 126 * @param {String} n Name of the item 127 * @return value of the a[n] or null 128 */ 129 function getSetItemByName(a, n){ 130 return !a[n] || a[n] == null ? null : a[n]; 131 } 132 133 return function(){ 134 /** 135 * @function 136 * @private 137 * @description this is helper to get constant by its name 138 * @param name name of constant 139 * @return constant value or null if it's not exist 140 */ 141 this.getConstant = function(name){ 142 //constants[name] ? constants[name] : null; 143 return Sonettic.Helper.getConstant(name); 144 } 145 146 /** 147 * @function 148 * @private 149 * @description This is a player mutator 150 * @param name Name of the player 151 * @param value player object 152 */ 153 this.setPlayer = function(name, value){ 154 players[name] = value || null; 155 } 156 157 /** 158 * @function 159 * @private 160 * @description This is a player accessor 161 * @param {String} name Name of the player 162 * @return SntPlayer player object 163 */ 164 this.getPlayer = function(name){ 165 return getSetItemByName(players, name); 166 } 167 168 169 /** 170 * @function 171 * @private 172 * @description This is an object mutator 173 * @param name Name of the player 174 * @param value Flash object 175 */ 176 this.setObj = function(name, value){ 177 objs[name] = value || null; 178 } 179 180 /** 181 * @function 182 * @private 183 * @description This is an object accessor 184 * @param name Name of the player 185 * @return Flash object 186 */ 187 this.getObj = function(name){ 188 return getSetItemByName(objs, name); 189 } 190 191 /** 192 * @function 193 * @private 194 * @description This is an object status mutator 195 * @param name Name of the player 196 * @param value status 197 */ 198 this.setStatus = function(name, value){ 199 status[name] = value || false; 200 } 201 202 /** 203 * @function 204 * @private 205 * @description This is a status of the player accessor 206 * @param name Name of the player 207 * @return boolean Status of the player 208 */ 209 this.getStatus = function(name){ 210 return getSetItemByName(status, name); 211 } 212 213 /** 214 * @function 215 * @private 216 * @description This is an unregistered object mutator 217 * @param name Name of the player 218 * @param value Unregistered object 219 */ 220 this.setUnregistered = function(name, value){ 221 unregistered[name] = value || null; 222 } 223 224 /** 225 * @function 226 * @private 227 * @description This is an unregistered object accessor 228 * @param name Name of the player 229 * @return SntPlayer object or null 230 */ 231 this.getUnregistered = function(name){ 232 return getSetItemByName(unregistered, name); 233 } 234 } 235 })(); 236 237 238 /** 239 * @function 240 * @description logging to the console between Player instance and this API. 241 * @param msg Log message 242 * @return void 243 */ 244 SntEventsHolder.prototype.log = function(msg){ 245 Sonettic.Helper.debug(msg); 246 247 } 248 249 /** 250 * @function 251 * @description Register player in SntEventsHolder 252 * @param id ID 253 * @param player Instance (flash object) 254 * @param obj object SntPlayer 255 */ 256 SntEventsHolder.prototype.registerPlayer = function(id, obj){ 257 if(id == null || obj == null)return false; 258 259 this.setStatus(id, false); 260 this.setPlayer(id, obj); 261 262 if(this.getUnregistered(id) != null){ 263 this.setUnregistered(id, null); 264 265 // set player as registered 266 this.setStatus(id, true); 267 this.getPlayer(id).register(true); 268 } 269 270 this.log('Player [' + id + '] registered sucessfull'); 271 272 return true; 273 } 274 275 /** 276 * @function 277 * @description Method for receiving messages from Player engine. 278 * @param command It's ID from which command we receive message 279 * @param player Player ID 280 * @param args Array with reply 281 */ 282 SntEventsHolder.prototype.receiveMessage = function(command, player, args){ 283 284 if(this.getUnregistered(player) != null){ 285 this.log('Skipped commad [' + command + '] for UNREGISTERED player [' + player + ']'); 286 // just skip message if player isn't registered 287 return; 288 } 289 290 // register player as uncreated 291 if(this.getPlayer(player) == null){ 292 this.log('Created new player instance [' + player + '] and received command [' + command + ']'); 293 this.setUnregistered(player, new SntPlayer(player)); 294 return; 295 } 296 297 if(this.getStatus(player) == false && command == this.getConstant('EVENT_REGISTER')){ 298 this.setStatus(player, true); 299 } 300 301 if(this.getObj(player) == null){ 302 //try to get this player ID 303 var playerObj = Sonettic.Helper.getFlashObject(player); 304 if(playerObj == null){ 305 throw new Error('Player with id [' + player + '] not exist'); 306 return; 307 } 308 this.setObj(player, playerObj); 309 } 310 311 this.getPlayer(player).register(true); 312 313 this.log('Command reply [' + command + '] for player [' + player + '] received with data:' ); 314 this.log(args); 315 this.getPlayer(player).receiveMessage(command, args); 316 } 317 318 /** 319 * @function 320 * @description Method for sending messages to Player engine. 321 * @param command It's ID from which command we receive message 322 * @param player Player ID 323 * @param args Array with data 324 */ 325 SntEventsHolder.prototype.sendMessage = function(command, player, args){ 326 if(this.getStatus(player) == false || this.getObj(player) == null)return; 327 this.log('Command [' + command + '] sent from [' + player + '] with args:'); 328 this.log(args); 329 330 var rawObj = this.getObj(player); 331 var rawFunc = rawObj.receiveMessage; 332 var there = this; 333 // send message to the player with timeout 30 miliseconds 334 // it's only for run call to the player simultaneously to main JavaScript process 335 setTimeout(function(){ 336 if(rawObj == null){ 337 there.log('Copy of the SntPlayer Object doesn\'t exist anymore. The Conception of your code went wrong.'); 338 return; 339 } 340 341 //send message to player engine 342 rawObj.receiveMessage(command, player, args); 343 344 //set focus to player 345 rawObj.focus(); 346 }, 30); 347 } 348 349 /** 350 * @function 351 * @description Method for adding event listener to Player engine 352 * @param command it's Event ID from list of players engine events 353 * @param player Player ID 354 */ 355 SntEventsHolder.prototype.addEventListener = function(event, player){ 356 if(this.getStatus(player) == false || this.getObj(player) == null)return; 357 this.log('Event listener added for event [' + event + '] for player [' + player + ']'); 358 this.sendMessage(this.getConstant('EVENT_ADD'), player, [event]); 359 360 } 361 362 /** 363 * @function 364 * @description Method for removing event listener from Player engine 365 * @param command It's Event ID from list of players engine events 366 * @param player Player ID 367 */ 368 SntEventsHolder.prototype.removeEventListener = function(event, player){ 369 if(this.getStatus(player) == false || this.getObj(player) == null)return; 370 this.log('Event listener removed, event [' + event + '] for player [' + player + ']'); 371 this.sendMessage(this.getConstant('EVENT_RM'), player, [event]); 372 } 373 374 /** 375 * @function 376 * @description Method to getting Flash object by id or name 377 * @param id Name or ID of the Flash object 378 * @return reference to Flash object 379 */ 380 SntEventsHolder.prototype.getFlashObject = function(id){ 381 return Sonettig.Helper.getFashObject(id) 382 } 383 384