1/**JybridExt initScript*/ 2if(typeof jybrid === 'undefined'){ 3 throw new Error('The jybrid JS Framework was not initialized in Browser'); 4} 5if(typeof jybridExt === 'undefined'){ 6 throw new Error('The jybrid JS Extension was not initialized in Browser. Make sure the jybrid ExtensionPack was load before this script'); 7} 8// initialise the global jybrid window object 9var jybridWindow = { 10 11}; 12 13 14 15 16/* JybridExt Window AddWindow Start 17* @see https://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript 18* 19* @param {jybridWindow} jW 20* @param {jybridExt} jE 21* @param jybrid jyb 22* */ 23(function(jW, jE, jyb){ 24 25 'use strict'; 26 /** 27 * Internal Registry which hold all windows and there configs 28 * 29 * @type {WindowsRegistryClass} privateWins 30 */ 31 var privateWins; 32 33 /*** 34 * 35 * JybGetSetHas is an Prototype class which is for extending Jybrid with simple Configuration checkers 36 * @todo move to an core-class because its need in jybrid core 37 * */ 38 var JybGetSetHas = function(data){ 39 this.data = 'undefined' !== typeof data ? data : {}; 40 }; 41 /** 42 * Create the Stack 43 * 44 * @param {Object|Array}stack 45 * @return {JybGetSetHas} 46 */ 47 JybGetSetHas.prototype.init = function(stack){ 48 if(typeof stack === 'undefined'){ 49 stack = {}; 50 } 51 this.data = stack; 52 return this; 53 }; 54 /** 55 * Datas 56 * @return {*} 57 */ 58 JybGetSetHas.prototype.getDatas = function(){ 59 return this.data; 60 }; 61 /*** 62 * Count elements of an Object (only the upper-level) 63 * 64 * @return {number} 65 */ 66 JybGetSetHas.prototype.countDatas = function(){ 67 var d = this.getDatas(); 68 return Object.keys(typeof d === 'object' ? d : {}).length; 69 70 }; 71 /*** 72 * Simple has 73 * @param {string|number}key 74 * @return {boolean} 75 */ 76 JybGetSetHas.prototype.has = function(key){ 77 return this.data.hasOwnProperty(key); 78 }; 79 /*** 80 * Simple getter with default or custom Return if not found. 81 * @example myObj.set('testVar', myObj.get('testVar',objIfNotSetBeforeSetNow)); 82 * 83 * @param {string|number} key 84 * @param {*} defaultReturn 85 * 86 * @return {null|*} returning an default value 87 */ 88 JybGetSetHas.prototype.get = function(key, defaultReturn){ 89 return this.has(key) ? this.data[key] : typeof defaultReturn === 'undefined' ? null : defaultReturn; 90 }; 91 /** 92 * Setter Method 93 * 94 * @param {string} key 95 * @param {*}value 96 * @return {JybGetSetHas} 97 */ 98 JybGetSetHas.prototype.set = function(key, value){ 99 this.data[key] = value; 100 return this; 101 }; 102 /*** 103 * Removes an Value (with his key) from data 104 * @param {string} key try to remove an part from object 105 */ 106 JybGetSetHas.prototype.remove = function(key){ 107 if(this.has(key)){ 108 try{ 109 delete this.data[key]; 110 }catch(e){ 111 // todo throw error 112 } 113 } 114 }; 115 116 /** 117 * Jybrid Window Registry to handle more than 1 Window. For complexer Tasks;-) 118 * 119 * @param {object} winDefaultConf On Create the Jybrid window during an regular pageLoad give a 120 * @param {object} overlayDefaultConfig On Create the Jybrid window during an regular pageLoad give a 121 */ 122 var WindowsRegistryClass = function(winDefaultConf, overlayDefaultConfig){ 123 124 this.winDefaultConf = Object.create(JybGetSetHas.prototype).init(typeof winDefaultConf === 'object' ? winDefaultConf : {}); 125 this.winInstances = Object.create(JybGetSetHas.prototype, {}).init({}); 126 // global overlay-config 127 this.overlayDefaultConfig = Object.create(JybGetSetHas.prototype).init(typeof overlayDefaultConfig === 'object' ? overlayDefaultConfig : {}); 128 }; 129 /** 130 * Close all windows and De-Register them from the internal Stack 131 */ 132 WindowsRegistryClass.prototype.closeAllWindowInstances = function(){ 133 var inss = this.winInstances.getDatas(); 134 for(var ins in inss){ 135 if(inss.hasOwnProperty(ins) && inss[ins] instanceof WindowClass){ 136 inss[ins].close(); 137 } 138 } 139 }; 140 /** 141 * WindowClass Blind Constructor that child-classes can better set there own init Method 142 * @return {WindowClass} 143 * @constructor 144 */ 145 var WindowClass = function(){ 146 147 // return the object for chaining 148 return this; 149 }; 150 /** 151 * Init-Method to keep the var "WindowClass" clear from Extra-Code 152 * 153 * @param _defaults 154 * @param _command 155 * @return {WindowClass} 156 * @return {WindowClass} 157 */ 158 WindowClass.prototype.init = function(_defaults, _command){ 159 160 // configuration of the jybrid instance 161 this.Config = Object.create(JybGetSetHas.prototype).init(jyb.extend(_defaults, _command));
162 // fine grained configuration with attributes as own setter getter 163 this.Attributes = Object.create(JybGetSetHas.prototype).init(jyb.extend(this.Config.get('attributes', {}))); 164 this.Config.remove('attributes'); 165 // fine grained configuration with hard-set positions as own setter getter 166 this.Positions = Object.create(JybGetSetHas.prototype).init(jyb.extend(this.Config.get('positions', {}))); 167 this.Config.remove('positions'); 168 // fine grained configuration which holds the wanted calculation-object of some position properties as own setter getter 169 this.calcPositions = Object.create(JybGetSetHas.prototype).init(jyb.extend(this.Config.get('calcPositions', {}))); 170 this.Config.remove('calcPositions'); 171 // fine grained configuration with css-style properties as own setter getter 172 this.Styles = Object.create(JybGetSetHas.prototype).init(jyb.extend(this.Config.get('styles', {}))); 173 this.Config.remove('styles'); 174 // holds the Overlay Config 175 this.Overlay = Object.create(JybGetSetHas.prototype).init(jyb.extend(this.Config.get('overlay', {}))); 176 this.Config.remove('overlay'); 177 return this; 178 }; 179 180 /** 181 * Getting the Window Id 182 * 183 * @param {string|number} wId window-id given from command 184 * @param {undefined|string} prefix During missing window-id jybrid.window creates an own window id with prefix. 185 * @return {string} 186 */ 187 WindowClass.prototype._createWindowId = function(wId, prefix){ 188 // todo test 189 var defPrefix = this.Config.get('prefix'); 190 var t = Math.random(); 191 var s = t.toString().replace('.', '_'); 192 return !wId ? (!prefix ? !defPrefix ? 'jybrid_' : defPrefix : prefix) + s : wId; 193 }; 194 /** 195 * Traversal method to get from inside an Window-Object the "global" Windows- registry 196 * 197 * @return {WindowsRegistryClass} 198 */ 199 WindowClass.prototype.getWindows = function(){ 200 return privateWins; 201 }; 202 /*** 203 * 204 * @return {JybGetSetHas} 205 */ 206 WindowClass.prototype.getWinInstances = function(){ 207 return this.getWindows().winInstances; 208 }; 209 /*** 210 * Find the Last Z-Index in windows they are already 211 * 212 * @return {number} 213 */ 214 WindowClass.prototype.getLastZIndex = function(startsFromConfig){ 215 var idxs = [0, startsFromConfig]; 216 var tmpZI; 217 var inss = this.getWinInstances().getDatas(); 218 for(var ins in inss){ 219 if(inss.hasOwnProperty(ins) && inss[ins] instanceof WindowClass){ 220 tmpZI = parseInt(inss[ins].Config.get('ZIndex', 0), 10); 221 if(0 < tmpZI){ 222 idxs.push(tmpZI); 223 } 224 } 225 } 226 // ecma5 227 return idxs.reduce(function(a, b){ 228 return Math.max(a, b); 229 }); 230 }; 231 /** 232 * PUBLIC Getting the Window Id with auto-create if need 233 * 234 * @return {string} 235 */ 236 WindowClass.prototype.getWindowId = function(){ 237 var wid; 238 // already created the window_id 239 if(null !== (wid = this.Config.get('window_id'))){ 240 return wid; 241 } 242 this.Config.set('window_id', this._createWindowId(this.Config.get('window_id'), this.Config.get('window_prefix', ''))); 243 return this.Config.get('window_id'); 244 }; 245 /** 246 * Attaching Styles into the Style-Attribute 247 * todo move to an upper class 248 * @param {Array|Object} styles merged styles array 249 * @return {string} the style properties string 250 */ 251 WindowClass.prototype.stringifyStyle = function(styles){ 252 var parts = []; 253 if(typeof styles !== 'object' || styles.length === 0){ 254 return ''; 255 } 256 for(var k in styles){ 257 if(styles.hasOwnProperty(k)){ 258 var v = styles[k]; 259 if(jyb.isAttribValue(v) && jyb.isAttribValue(k)){ 260 parts.push(k + ':' + v); 261 } 262 } 263 } 264 if(parts.length === 0){ 265 return ''; 266 } 267 return parts.join(';'); 268 }; 269 270 /** 271 * todo move to jybrid attrib methods with attribSet 272 * @param elem 273 * @param attribs 274 */ 275 WindowClass.prototype.addAttributes = function(elem, attribs){ 276 for(var k in attribs){ 277 if(attribs.hasOwnProperty(k)){ 278 jyb.attribAppend(elem, k, attribs[k]); 279 } 280 } 281 }; 282 /** 283 * Automatic handling of the close button 284 * @return {boolean} 285 */ 286 WindowClass.prototype.bindClose = function(){ 287 288 var xmW = this.Config.get('windowObject', null); 289 if(!xmW){ 290 return false; 291 } 292 var self = this; 293 var bindClose = this.Config.get('bindClose'); 294 if(bindClose && 0 < Object.keys(bindClose).length){ 295 // getting the subObject which contains the bind-close commands 296 var bC = new JybGetSetHas(bindClose); 297 var ele = bC.get('ele', null); 298 299 // make sure the close button will searched inside the window 300 var inWindow = bC.get('inWindow', false); 301 302 if(ele === 'self'){ 303 // simply click at the window 304 ele = xmW; 305 }else{ 306 ele = !(ele) ? inWindow ? xmW : null : ele; 307 if(typeof ele === 'string' && inWindow){ 308 ele = '#' + self.Config.get('window_id') + ' ' + ele; 309 } 310 } 311 if((ele = jyb.$(ele))){ 312 // removes self while clicking 313 this.attachClose(ele, bC.get('trigger', 'click')); 314 } 315 } 316 // for chaining 317 return this; 318 }; 319 /** 320 * Attaching the close button from internal-js-class-scope which contains the whole window-instance as 'self' 321 * @param ele 322 * @param trigger 323 */ 324 WindowClass.prototype.attachClose = function(ele, trigger){ 325 var self = this; 326 jyb.on(ele, trigger, function(){ 327 self.close(); 328 }); 329 330 }; 331 /** 332 * Mainstay to add an JybridWindow 333 * @return {boolean} 334 */ 335 WindowClass.prototype.add = function(){ 336 337 var pageOverFlow = 0; 338 339 // setup document and window 340 this.Config.set('objDoc', this.Config.get('objDoc', document)).set('objWin', this.Config.get('objWin', window)); 341 var objDoc = this.Config.get('objDoc', document); 342 var objWin = this.Config.get('objWin', window); 343 // style they will be autoge 344 var internalOverrideStyles = Object.create(JybGetSetHas.prototype).init({}); 345 /*create an window_id for calling directly from js onpage*/ 346 var windowId = this.getWindowId(); 347 var innerHTML = this.Config.get('data'); 348 349 // @todo is an empty window or only an TextNode without HTML so wrap TAG if want 350 if(!innerHTML && this.Config.get('createEmptyWindow')){ 351 // todo just an dummy 352 } 353
354 var objBody = objDoc.getElementsByTagName('body').item(0) ? objDoc.getElementsByTagName('body').item(0) : objDoc.getElementsByTagName('html').item(0); 355 356 var xmW = objDoc.createElement('div'); 357 this.Config.set('windowObject', xmW); 358 this.Attributes.set('id', windowId); 359 360 // calculating the overlay 361 var lOver = this.Overlay.countDatas(); 362 var displayOverlay = 0 < lOver; 363 var ZIndex; 364 var lZIndex = 0; 365 if((ZIndex = this.Config.get('ZIndex', null))){ 366 // sure, wie use the z-index and during the WindowClass creation it was not set with 'false' parameter 367 if(ZIndex !== false){ 368 lZIndex = this.getLastZIndex(ZIndex); 369 lZIndex += (displayOverlay) ? 2 : 1; 370 // configure this value for later calculation for other windows 371 this.Config.set('ZIndex', lZIndex); 372 } 373 }else if(displayOverlay){ 374 375 lZIndex = 2000; 376 } 377 // set the z-index to internalOverrideStyles because its possible that the 'useStyles' was false 378 internalOverrideStyles.set('z-index', lZIndex); 379 // we have an overlay 380 if(0 < displayOverlay){ 381 382 var zI = this.Overlay.get('ZIndex', this.Config.get('ZIndex', 2000)); 383 384 var jybWindowOverlayId = 'overlay_' + this.getWindowId(); 385 this.Overlay.set('window_id', jybWindowOverlayId); 386 this.Overlay.set('ZIndex', zI); 387 this.Config.set('overlayId', jybWindowOverlayId); 388 this.Config.set('overlayObject', jW.CreateOverlay(this.Overlay.getDatas()).add()); 389 } 390 391 // adding classNames because of calculated sizes; 392 // default className for jybrid Window if exists 393 jyb.addClass(xmW, this.Config.get('classNames')); 394 395 xmW.innerHTML = innerHTML; 396 objBody.appendChild(xmW); 397 398 var pageSize = jE.Size.getSize(objDoc, objWin); 399 400 //todo handle text-nodes or empty nodes : perhaps we have only an textNode!! 401 if(xmW.hasOwnProperty('firstChild')){ 402 403 /* intern storage for layer */ 404 xmW.sizes = jE.Size.getElementSize(xmW.firstChild); 405 if(xmW.sizes.height > pageSize.pH){ 406 pageSize.wH = xmW.sizes.height + pageOverFlow; 407 // todo, check perhaps with is need 408 pageSize.pH = xmW.sizes.height + pageOverFlow; 409 } 410 } 411 412 // Old Vars 413 var vSpace = 0; 414 var hSpace = 0; 415 // todo check parameter 416 var vFactor = 2; 417 var hFactor = 2; 418 419 xmW.sizes = jE.Size.getElementSize(xmW); 420 var calcVertical = this.calcPositions.get('vertical', false); 421 var fromScrollTop = this.calcPositions.get('scrollTop', false); 422 // 423 if(fromScrollTop){ 424 var sct = (Math.max((pageSize.sT), 0) + vSpace); 425 this.Positions.set('top', parseInt(sct, 0) + fromScrollTop); 426 }else if(calcVertical){ 427 // Calculate window into vertical center 428 // vertical Versatz of the window 429 var fnlV = (Math.max((pageSize.sT + ((pageSize.wH - xmW.sizes.height) / vFactor)), 0) + vSpace); 430 this.Positions.set('top', parseInt(fnlV, 0) + calcVertical); 431 } 432 433 // Override Styles the was generated 434 435 // patch the style datas 436 var generatedStyles = jyb.extend(this.Positions.getDatas(), internalOverrideStyles.getDatas()); 437 438 if(this.Config.get('useStyles', false)){ 439 var styles = this.Styles.getDatas(); 440 xmW.setAttribute('style', this.stringifyStyle(jyb.extend(styles, generatedStyles))); 441 }else{ 442 // set only the calculated positions 443 if(generatedStyles.length > 0){ 444 xmW.setAttribute('style', generatedStyles); 445 } 446 } 447 this.addAttributes(xmW, this.Attributes.getDatas()); 448 // determines the closings in an own method 449 this.bindClose(); 450 return this; 451 }; 452 453 /** 454 * Close the Jybrid Window with his Layer 455 */ 456 WindowClass.prototype.close = function(){ 457 458 var winId = this.Config.get('window_id'); 459 460 // remove the html 461 jyb.remove(winId); 462 463 // remove the Overlay if exists 464 var overlayObject = false; 465 if((overlayObject = this.Config.get('overlayObject', false)) instanceof WindowsOverlay){ 466 overlayObject.close(); 467 } 468 469 // remove from WinInstances 470 this.getWindows().winInstances.remove(winId); 471 472 }; 473 474 var WindowsOverlay = function(){ 475 476 }; 477 // ChildClass of WindowOverlay 478 WindowsOverlay.prototype = Object.create(WindowClass.prototype); 479 480 // handling some default vars 481 // todo make config default object as demo 482 var internalDefaultParams = { 483 // general attach the jybrid window nameSpace 484 classNames : 'JybridWindow', 485 // if we have an empty window body, should create the window or not 486 createEmptyWindow: true, 487 // how to handle if the window empty and the window should be create while @param createEmptyWindow is true 488 handleEmptyWindow: function(){ 489 490 }, 491 492 /** 493 * Main-Flag to tell jybrid-window that the window must calculate the window position 494 * OTHERWISE you have to configure styles and offsets via an CSS-Property/ClassName 495 * @param {boolean} calcPositions 496 * @default true 497 */ 498 calcPositions: { 499 vertical : 'px', 500 horizontal: 'px' 501 }, 502 /** 503 * Main-Flag to attach Styles or use the styles-property in jybrid-window 504 * OTHERWISE you have to configure styles and offsets via an CSS-Property/ClassName 505 * @param {boolean} useStyles 506 * @default true 507 */ 508 useStyles : true, 509 styles : {position: 'absolute'}, 510 /* 511 * Adding some Attributes 512 * */ 513 attributes : {rel: 'JybridWindow'}, 514 bindClose : {}, 515 bindMovable : {} 516 517 }; 518 /** 519 * Getting the Main Registry Object for all windows (windowsStack) 520 * 521 * @return {WindowsRegistryClass} 522 */ 523 jW.getWindows = function(jybridWindowDefaultParams, overlayDefaultParams){ 524 return (privateWins instanceof WindowsRegistryClass) ? privateWins : 525 privateWins = new WindowsRegistryClass(jyb.extend(internalDefaultParams, typeof jybridWindowDefaultParams === 'object' ? jybridWindowDefaultParams : {}), overlayDefaultParams); 526 }; 527 /** 528 * Constructor Method. Call it First 529 * 530 * @param {Object}
530 jybridWindowDefaultParams default params they are used for the window instances 531 * @param {Object} overlayDefaultParams default params they are used for the window instances 532 */ 533 jW.init = function(jybridWindowDefaultParams, overlayDefaultParams){ 534 this.getWindows(jybridWindowDefaultParams, overlayDefaultParams); 535 }; 536 /** 537 * Creates an Window Instance without display 538 * 539 * @return {WindowClass} 540 * **/ 541 jW.create = function(command){ 542 543 if(command instanceof WindowClass){ 544 return command; 545 } 546 547 var tmpW = new WindowClass().init(privateWins.winDefaultConf.getDatas(), command); 548 var insName = tmpW.getWindowId(); 549 550 // todo check the window id is already registered 551 privateWins.winInstances.set(insName, tmpW); 552 return privateWins.winInstances.get(insName); 553 }; 554 /*** 555 * Create an Jybrid Overlay for Window 556 * @param command 557 * @return {WindowsOverlay} 558 * @constructor 559 */ 560 jW.CreateOverlay = function(command){ 561 562 if(command instanceof WindowsOverlay){ 563 return command; 564 } 565 566 var tmpW = new WindowsOverlay().init(privateWins.overlayDefaultConfig.getDatas(), command); 567 var insName = tmpW.getWindowId(); 568 569 // todo check the window id is already registered 570 privateWins.winInstances.set(insName, tmpW); 571 return privateWins.winInstances.get(insName); 572 }; 573 /** 574 * add Window Command 575 */ 576 jW.add = function(command){ 577 jW.create(command).add(); 578 }; 579 580 /** 581 * Close all open windows 582 */ 583 jW.closeWindows = function(){ 584 jW.getWindows().closeAllWindowInstances(); 585 }; 586 587 jW.close = function(){ 588 var id = ''; 589 if('undefined' !== typeof arguments){ 590 if('undefined' !== typeof (id = arguments[0].id) && id !== ''){ 591 var wInstance = jW.getWindows().winInstances.get(id); 592 if(wInstance instanceof WindowClass){ 593 wInstance.close(); 594 } 595 } 596 } 597 }; 598}(jybridWindow, jybridExt, jybrid)); 599 600/* End Window AddWindow*/
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.