1 2/** 3 * BehaviourEnvironment 4 * 5 * @class BehaviourEnvironment 6 */ 7(function(ns) { 8 9 // check if media query supported, assume browser with media query support also support :root 10 var _mqSupport, 11 _desktopSize = 1024; 12 try { 13 _mqSupport = !!(document.querySelectorAll(':root')); 14 } 15 catch (e) { 16 _mqSupport = false; 17 } 18 19 /** 20 * Constructs BehaviourEnvironment objects. 21 * Should only be called from create method. 22 * 23 * data-environment JSON specification 24 * { 25 * 'window':{ 26 * 'minWidth':<Number> 27 * 'maxWidth':<Number> 28 * } 29 * } 30 * 31 * @class BehaviourEnvironment 32 * @constructor 33 * @param {Object} element DOM Element 34 */ 35 var BehaviourEnvironment = function(element,properties) { 36 37 // is the environment suitable, by default it is 38 this._suitable = true; 39 40 // set properties object 41 this._properties = properties; 42 43 // listen for environment changes 44 this._listen(); 45 46 // test the environment 47 this._test(); 48 49 }; 50 51 /** 52 * Static method construct creates behaviour environment objects 53 * 54 * @method construct 55 * @class BehaviourEnvironment 56 */ 57 BehaviourEnvironment.construct = function(element) { 58 59 // check if has specifications 60 var specifications = element.getAttribute('data-environment'); 61 if (!specifications) { 62 return null; 63 } 64 65 var properties = null; 66 try { 67 properties = JSON.parse(specifications); 68 } 69 catch(e) { 70 console.warn("BehaviourEnvironment: data-environment attribute should have format data-environment='{\"foo\":\"bar\"}'"); 71 return null; 72 } 73 74 return new mira.core.BehaviourEnvironment(element,properties) 75 76 }; 77 78 79 // prototype shortcut 80 var p = BehaviourEnvironment.prototype; 81 82 83 /** 84 * Adds listeners to the environment to act when it changes 85 * 86 * @method _listen 87 * @class BehaviourEnvironment 88 */ 89 p._listen = function() { 90 91 if (this._properties.window) { 92 var responsiveWindow = mira.utils.ResponsiveWindow.getInstance(); 93 Observer.subscribe(responsiveWindow, 'resize', this._test.bind(this)); 94 } 95 96 }; 97 98 /** 99 * Checks if the current environment has the requested properties 100 * 101 * @method _test 102 * @class BehaviourEnvironment 103 */ 104 p._test = function() { 105 106 // test environment properties 107 var win,width,state,suitable = true; 108 109 // test window sizes 110 if (win = this._properties.window) { 111 112 // get window size 113 width = _mqSupport ? mira.window.getWidth() : _desktopSize; 114 115 // check max width 116 if (win.maxWidth && width > win.maxWidth) { 117 suitable = false; 118 } 119 120 // check min width 121 if (win.minWidth && width < win.minWidth) { 122 suitable = false; 123 } 124 125 } 126 127 // fire changed event if environment suitability changed 128 if (suitable != this._suitable) { 129 this._suitable = suitable; 130 Observer.publish(this, 'change'); 131 } 132 }; 133 134 /** 135 * Returns the suitability of the current environment 136 * 137 * @method isSuitable 138 * @class BehaviourEnvironment 139 * @return {Boolean} The suitability of the environment 140 */ 141 p.isSuitable = function() { 142 return this._suitable; 143 }; 144 145 // Register class 146 Namespace.register(ns).BehaviourEnvironment = BehaviourEnvironment; 147 148}('mira.core'));
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.