|
22 | 22 | var
|
23 | 23 | ß = null,
|
24 | 24 | loadedJs = {},
|
| 25 | + scandioHtmlClass = 'scandio-js', |
| 26 | + $scandioEl = null, |
25 | 27 | // Previous version for `ß.noConflict`
|
26 | 28 | previousScandio = root.ß,
|
27 | 29 | // Breaker for loop iteration
|
|
92 | 94 | }
|
93 | 95 | },
|
94 | 96 |
|
| 97 | + _injectDom = function() { |
| 98 | + $(function() { |
| 99 | + if ( $(scandioHtmlClass).length === 0 ) { |
| 100 | + $scandioEl = $('<div/>', { |
| 101 | + class: scandioHtmlClass |
| 102 | + }).appendTo('body'); |
| 103 | + } |
| 104 | + }); |
| 105 | + }, |
| 106 | + |
95 | 107 | // Any call to subordinate initialization function goes here
|
96 | 108 | // *Note:* We're in pre-creation state
|
97 | 109 | _initialize = function() {
|
98 | 110 | // As the adove catching of console calls
|
99 | 111 | _catchConsole();
|
| 112 | + _injectDom(); |
100 | 113 | };
|
101 | 114 |
|
102 | 115 | // Intialize
|
|
107 | 120 |
|
108 | 121 | // Version of our library
|
109 | 122 | ß.VERSION = '0.0.1';
|
| 123 | +// DOM functionality |
| 124 | +// --------------- |
| 125 | + |
| 126 | +// Register dom namespace on scandiojs object |
| 127 | + |
| 128 | +ß.dom = {}; |
| 129 | + |
| 130 | +// Closes and secures a cache module with within its own scope |
| 131 | +// *Note:* This function being an IIFE leaves of parameters on outer function |
| 132 | +ß.dom.cache = (function($, ß){ |
| 133 | + // Sets up local cache store |
| 134 | + var |
| 135 | + cache = {}, |
| 136 | + |
| 137 | + // Handler of DOMNodeRemoved handling external node removals |
| 138 | + // Sorry, I didn't hack this |
| 139 | + nodeRemoved = function(event) { |
| 140 | + var label, coll, l, t = event.target; |
| 141 | + for (label in cache) { |
| 142 | + l = (coll = cache[label]).length; |
| 143 | + while (l--) { |
| 144 | + if (coll[l] === t || $.contains(t,coll[l])) { |
| 145 | + delete coll[l]; --coll.length; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }, |
| 150 | + |
| 151 | + // Updates complete cache or scoped to a label |
| 152 | + update = function(label) { |
| 153 | + // Passed in label is string, scoping to that label |
| 154 | + if (ß.isString(label)) { |
| 155 | + //Reset cache value at label |
| 156 | + if(cache[label] !== undefined) { |
| 157 | + cache[label] = $(cache[label].selector || ''); |
| 158 | + } |
| 159 | + } else { |
| 160 | + // For each value in cache refresh it |
| 161 | + ß.util.each(cache, function($cached, label) { |
| 162 | + cache[label] = $($cached.selector); |
| 163 | + }); |
| 164 | + } |
| 165 | + }, |
| 166 | + |
| 167 | + // Gets a value from cache or loads it from DOM |
| 168 | + get = function(label, selector) { |
| 169 | + // Both label and selector passed, cache/dom reading... |
| 170 | + if (ß.isString(selector) && ß.isString(label)) { |
| 171 | + // ...either from cache or DOM |
| 172 | + cache[label] = cache[label] || $(selector); |
| 173 | + } |
| 174 | + |
| 175 | + // What the callee gets: a jQuery object |
| 176 | + return cache[label]; |
| 177 | + }; |
| 178 | + |
| 179 | + // Bind to node removal in DOM |
| 180 | + $(document).on('DOMNodeRemoved', nodeRemoved); |
| 181 | + |
| 182 | + // Return public functions in object literal |
| 183 | + return { |
| 184 | + get: get, |
| 185 | + update: update |
| 186 | + }; |
| 187 | +}(jQuery, ß)); |
110 | 188 | // Debug/logging module
|
111 | 189 | // ---------------
|
112 | 190 |
|
113 | 191 | // Sets up logger object with level and log-history
|
114 | 192 | ß.logger = {
|
115 | 193 | level: 5,
|
116 |
| - logs: {} |
| 194 | + logs: {}, |
| 195 | + logDom: true |
117 | 196 | };
|
118 | 197 |
|
| 198 | +ß.debug = {}; |
| 199 | + |
119 | 200 | // `ß.debug` will get a set of methods (*see return-statement*)
|
120 | 201 | ß.debug = (function(){
|
121 | 202 | var
|
122 | 203 | console = window.console,
|
123 | 204 | length = logMethods.length,
|
124 | 205 | methods = {},
|
| 206 | + alertEls = { |
| 207 | + debug: 'info', |
| 208 | + error: 'danger', |
| 209 | + info: 'info', |
| 210 | + log: 'success', |
| 211 | + warn: 'warning' |
| 212 | + }, |
125 | 213 | // Closes the scope for `method and level`
|
126 | 214 | // *Note:* Due to js and its state-maintainance for closures
|
127 | 215 | // the last passed argument would otherwise win
|
128 | 216 | createLogger = function (method, level) {
|
| 217 | + var |
| 218 | + logElWrapperPath = 'scandio-log--' + method, |
| 219 | + logElInnerPath = 'alert alert-' + alertEls[method] || method; |
| 220 | + |
129 | 221 | // Sets up history for the log-method
|
130 | 222 | ß.logger.logs[method] = [];
|
131 | 223 |
|
| 224 | + if (ß.logger.logDom === true) { |
| 225 | + $(function() { |
| 226 | + $('<div/>', { |
| 227 | + class: logElWrapperPath |
| 228 | + }).appendTo($scandioEl).html( |
| 229 | + $('<div />', { |
| 230 | + class: logElInnerPath |
| 231 | + }) |
| 232 | + ); |
| 233 | + }); |
| 234 | + |
| 235 | + ß.dom[method] = function(msg) { |
| 236 | + var |
| 237 | + className = ".alert-" + alertEls[method] || method, |
| 238 | + $logEl = ß.dom.cache.get(logElInnerPath, className).length > 0 ? |
| 239 | + ß.dom.cache.get(logElInnerPath, className) : ß.dom.cache.update(logElInnerPath); |
| 240 | + |
| 241 | + if (ß.logger.logDom && $logEl && $logEl.length > 0) { $logEl.append(msg + '<hr />'); } |
| 242 | + }; |
| 243 | + } |
| 244 | + |
132 | 245 | // The return value's log-type gets a function
|
133 | 246 | methods[method] = function() {
|
134 | 247 | // Lets get some arguments
|
135 | 248 | var args = slice.call(arguments);
|
136 | 249 |
|
137 | 250 | // Only log to console if required by level
|
138 |
| - if(ß.logger.level > level) { |
| 251 | + if (ß.logger.level > level) { |
139 | 252 | console[method].apply(console, args);
|
| 253 | + ß.dom[method].apply(ß, args); |
140 | 254 | }
|
141 | 255 |
|
142 | 256 | // but always push it to history
|
|
149 | 263 |
|
150 | 264 | // Now the `ß.debug`-object gets its functions
|
151 | 265 | return methods;
|
152 |
| -})(); |
153 |
| -// Utility functions |
| 266 | +})();// Utility functions |
154 | 267 | // ---------------
|
155 | 268 |
|
156 | 269 | // Register util namespace on scandiojs object
|
|
810 | 923 | ß.responsive.breakpoint = function(name) {
|
811 | 924 | return $( ß.responsive.breakpointEl ).html() === name;
|
812 | 925 | };
|
813 |
| -// DOM functionality |
814 |
| -// --------------- |
815 |
| - |
816 |
| -// Register dom namespace on scandiojs object |
817 |
| - |
818 |
| -ß.dom = {}; |
819 |
| - |
820 |
| -// Closes and secures a cache module with within its own scope |
821 |
| -// *Note:* This function being an IIFE leaves of parameters on outer function |
822 |
| -ß.dom.cache = (function($, ß){ |
823 |
| - // Sets up local cache store |
824 |
| - var |
825 |
| - cache = {}, |
826 |
| - |
827 |
| - // Handler of DOMNodeRemoved handling external node removals |
828 |
| - // Sorry, I didn't hack this |
829 |
| - nodeRemoved = function(event) { |
830 |
| - var label, coll, l, t = event.target; |
831 |
| - for (label in cache) { |
832 |
| - l = (coll = cache[label]).length; |
833 |
| - while (l--) { |
834 |
| - if (coll[l] === t || $.contains(t,coll[l])) { |
835 |
| - delete coll[l]; --coll.length; |
836 |
| - } |
837 |
| - } |
838 |
| - } |
839 |
| - }, |
840 |
| - |
841 |
| - // Updates complete cache or scoped to a label |
842 |
| - update = function(label) { |
843 |
| - // Passed in label is string, scoping to that label |
844 |
| - if (ß.isString(label)) { |
845 |
| - //Reset cache value at label |
846 |
| - cache[label] = $(cache[label].selector || ''); |
847 |
| - } else { |
848 |
| - // For each value in cache refresh it |
849 |
| - ß.util.each(cache, function($cached, label) { |
850 |
| - cache[label] = $($cached.selector); |
851 |
| - }); |
852 |
| - } |
853 |
| - }, |
854 |
| - |
855 |
| - // Gets a value from cache or loads it from DOM |
856 |
| - get = function(label, selector) { |
857 |
| - // Both label and selector passed, cache/dom reading... |
858 |
| - if (ß.isString(selector) && ß.isString(label)) { |
859 |
| - // ...either from cache or DOM |
860 |
| - cache[label] = cache[label] || $(selector); |
861 |
| - } |
862 |
| - |
863 |
| - // What the callee gets: a jQuery object |
864 |
| - return cache[label]; |
865 |
| - }; |
866 |
| - |
867 |
| - // Bind to node removal in DOM |
868 |
| - $(document).on('DOMNodeRemoved', nodeRemoved); |
869 |
| - |
870 |
| - // Return public functions in object literal |
871 |
| - return { |
872 |
| - get: get, |
873 |
| - update: update |
874 |
| - }; |
875 |
| -}(jQuery, ß)); |
876 | 926 | // Outro, AMD and conflict resolution
|
877 | 927 | // ---------------
|
878 | 928 |
|
|
0 commit comments