github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/semantic/dist/components/site.js (about) 1 /*! 2 * # Semantic UI x.x - Site 3 * http://github.com/semantic-org/semantic-ui/ 4 * 5 * 6 * Copyright 2014 Contributors 7 * Released under the MIT license 8 * http://opensource.org/licenses/MIT 9 * 10 */ 11 ;(function ( $, window, document, undefined ) { 12 13 $.site = $.fn.site = function(parameters) { 14 var 15 time = new Date().getTime(), 16 performance = [], 17 18 query = arguments[0], 19 methodInvoked = (typeof query == 'string'), 20 queryArguments = [].slice.call(arguments, 1), 21 22 settings = ( $.isPlainObject(parameters) ) 23 ? $.extend(true, {}, $.site.settings, parameters) 24 : $.extend({}, $.site.settings), 25 26 namespace = settings.namespace, 27 error = settings.error, 28 29 eventNamespace = '.' + namespace, 30 moduleNamespace = 'module-' + namespace, 31 32 $document = $(document), 33 $module = $document, 34 element = this, 35 instance = $module.data(moduleNamespace), 36 37 module, 38 returnedValue 39 ; 40 module = { 41 42 initialize: function() { 43 module.instantiate(); 44 }, 45 46 instantiate: function() { 47 module.verbose('Storing instance of site', module); 48 instance = module; 49 $module 50 .data(moduleNamespace, module) 51 ; 52 }, 53 54 normalize: function() { 55 module.fix.console(); 56 module.fix.requestAnimationFrame(); 57 }, 58 59 fix: { 60 console: function() { 61 module.debug('Normalizing window.console'); 62 if (console === undefined || console.log === undefined) { 63 module.verbose('Console not available, normalizing events'); 64 module.disable.console(); 65 } 66 if (typeof console.group == 'undefined' || typeof console.groupEnd == 'undefined' || typeof console.groupCollapsed == 'undefined') { 67 module.verbose('Console group not available, normalizing events'); 68 window.console.group = function() {}; 69 window.console.groupEnd = function() {}; 70 window.console.groupCollapsed = function() {}; 71 } 72 if (typeof console.markTimeline == 'undefined') { 73 module.verbose('Mark timeline not available, normalizing events'); 74 window.console.markTimeline = function() {}; 75 } 76 }, 77 consoleClear: function() { 78 module.debug('Disabling programmatic console clearing'); 79 window.console.clear = function() {}; 80 }, 81 requestAnimationFrame: function() { 82 module.debug('Normalizing requestAnimationFrame'); 83 if(window.requestAnimationFrame === undefined) { 84 module.debug('RequestAnimationFrame not available, normailizing event'); 85 window.requestAnimationFrame = window.requestAnimationFrame 86 || window.mozRequestAnimationFrame 87 || window.webkitRequestAnimationFrame 88 || window.msRequestAnimationFrame 89 || function(callback) { setTimeout(callback, 0); } 90 ; 91 } 92 } 93 }, 94 95 moduleExists: function(name) { 96 return ($.fn[name] !== undefined && $.fn[name].settings !== undefined); 97 }, 98 99 enabled: { 100 modules: function(modules) { 101 var 102 enabledModules = [] 103 ; 104 modules = modules || settings.modules; 105 $.each(modules, function(index, name) { 106 if(module.moduleExists(name)) { 107 enabledModules.push(name); 108 } 109 }); 110 return enabledModules; 111 } 112 }, 113 114 disabled: { 115 modules: function(modules) { 116 var 117 disabledModules = [] 118 ; 119 modules = modules || settings.modules; 120 $.each(modules, function(index, name) { 121 if(!module.moduleExists(name)) { 122 disabledModules.push(name); 123 } 124 }); 125 return disabledModules; 126 } 127 }, 128 129 change: { 130 setting: function(setting, value, modules, modifyExisting) { 131 modules = (typeof modules === 'string') 132 ? (modules === 'all') 133 ? settings.modules 134 : [modules] 135 : modules || settings.modules 136 ; 137 modifyExisting = (modifyExisting !== undefined) 138 ? modifyExisting 139 : true 140 ; 141 $.each(modules, function(index, name) { 142 var 143 namespace = (module.moduleExists(name)) 144 ? $.fn[name].settings.namespace || false 145 : true, 146 $existingModules 147 ; 148 if(module.moduleExists(name)) { 149 module.verbose('Changing default setting', setting, value, name); 150 $.fn[name].settings[setting] = value; 151 if(modifyExisting && namespace) { 152 $existingModules = $(':data(module-' + namespace + ')'); 153 if($existingModules.length > 0) { 154 module.verbose('Modifying existing settings', $existingModules); 155 $existingModules[name]('setting', setting, value); 156 } 157 } 158 } 159 }); 160 }, 161 settings: function(newSettings, modules, modifyExisting) { 162 modules = (typeof modules === 'string') 163 ? [modules] 164 : modules || settings.modules 165 ; 166 modifyExisting = (modifyExisting !== undefined) 167 ? modifyExisting 168 : true 169 ; 170 $.each(modules, function(index, name) { 171 var 172 $existingModules 173 ; 174 if(module.moduleExists(name)) { 175 module.verbose('Changing default setting', newSettings, name); 176 $.extend(true, $.fn[name].settings, newSettings); 177 if(modifyExisting && namespace) { 178 $existingModules = $(':data(module-' + namespace + ')'); 179 if($existingModules.length > 0) { 180 module.verbose('Modifying existing settings', $existingModules); 181 $existingModules[name]('setting', newSettings); 182 } 183 } 184 } 185 }); 186 } 187 }, 188 189 enable: { 190 console: function() { 191 module.console(true); 192 }, 193 debug: function(modules, modifyExisting) { 194 modules = modules || settings.modules; 195 module.debug('Enabling debug for modules', modules); 196 module.change.setting('debug', true, modules, modifyExisting); 197 }, 198 verbose: function(modules, modifyExisting) { 199 modules = modules || settings.modules; 200 module.debug('Enabling verbose debug for modules', modules); 201 module.change.setting('verbose', true, modules, modifyExisting); 202 } 203 }, 204 disable: { 205 console: function() { 206 module.console(false); 207 }, 208 debug: function(modules, modifyExisting) { 209 modules = modules || settings.modules; 210 module.debug('Disabling debug for modules', modules); 211 module.change.setting('debug', false, modules, modifyExisting); 212 }, 213 verbose: function(modules, modifyExisting) { 214 modules = modules || settings.modules; 215 module.debug('Disabling verbose debug for modules', modules); 216 module.change.setting('verbose', false, modules, modifyExisting); 217 } 218 }, 219 220 console: function(enable) { 221 if(enable) { 222 if(instance.cache.console === undefined) { 223 module.error(error.console); 224 return; 225 } 226 module.debug('Restoring console function'); 227 window.console = instance.cache.console; 228 } 229 else { 230 module.debug('Disabling console function'); 231 instance.cache.console = window.console; 232 window.console = { 233 clear : function(){}, 234 error : function(){}, 235 group : function(){}, 236 groupCollapsed : function(){}, 237 groupEnd : function(){}, 238 info : function(){}, 239 log : function(){}, 240 markTimeline : function(){}, 241 warn : function(){} 242 }; 243 } 244 }, 245 246 destroy: function() { 247 module.verbose('Destroying previous site for', $module); 248 $module 249 .removeData(moduleNamespace) 250 ; 251 }, 252 253 cache: {}, 254 255 setting: function(name, value) { 256 if( $.isPlainObject(name) ) { 257 $.extend(true, settings, name); 258 } 259 else if(value !== undefined) { 260 settings[name] = value; 261 } 262 else { 263 return settings[name]; 264 } 265 }, 266 internal: function(name, value) { 267 if( $.isPlainObject(name) ) { 268 $.extend(true, module, name); 269 } 270 else if(value !== undefined) { 271 module[name] = value; 272 } 273 else { 274 return module[name]; 275 } 276 }, 277 debug: function() { 278 if(settings.debug) { 279 if(settings.performance) { 280 module.performance.log(arguments); 281 } 282 else { 283 module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':'); 284 module.debug.apply(console, arguments); 285 } 286 } 287 }, 288 verbose: function() { 289 if(settings.verbose && settings.debug) { 290 if(settings.performance) { 291 module.performance.log(arguments); 292 } 293 else { 294 module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':'); 295 module.verbose.apply(console, arguments); 296 } 297 } 298 }, 299 error: function() { 300 module.error = Function.prototype.bind.call(console.error, console, settings.name + ':'); 301 module.error.apply(console, arguments); 302 }, 303 performance: { 304 log: function(message) { 305 var 306 currentTime, 307 executionTime, 308 previousTime 309 ; 310 if(settings.performance) { 311 currentTime = new Date().getTime(); 312 previousTime = time || currentTime; 313 executionTime = currentTime - previousTime; 314 time = currentTime; 315 performance.push({ 316 'Element' : element, 317 'Name' : message[0], 318 'Arguments' : [].slice.call(message, 1) || '', 319 'Execution Time' : executionTime 320 }); 321 } 322 clearTimeout(module.performance.timer); 323 module.performance.timer = setTimeout(module.performance.display, 100); 324 }, 325 display: function() { 326 var 327 title = settings.name + ':', 328 totalTime = 0 329 ; 330 time = false; 331 clearTimeout(module.performance.timer); 332 $.each(performance, function(index, data) { 333 totalTime += data['Execution Time']; 334 }); 335 title += ' ' + totalTime + 'ms'; 336 if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) { 337 console.groupCollapsed(title); 338 if(console.table) { 339 console.table(performance); 340 } 341 else { 342 $.each(performance, function(index, data) { 343 console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); 344 }); 345 } 346 console.groupEnd(); 347 } 348 performance = []; 349 } 350 }, 351 invoke: function(query, passedArguments, context) { 352 var 353 object = instance, 354 maxDepth, 355 found, 356 response 357 ; 358 passedArguments = passedArguments || queryArguments; 359 context = element || context; 360 if(typeof query == 'string' && object !== undefined) { 361 query = query.split(/[\. ]/); 362 maxDepth = query.length - 1; 363 $.each(query, function(depth, value) { 364 var camelCaseValue = (depth != maxDepth) 365 ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1) 366 : query 367 ; 368 if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) { 369 object = object[camelCaseValue]; 370 } 371 else if( object[camelCaseValue] !== undefined ) { 372 found = object[camelCaseValue]; 373 return false; 374 } 375 else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) { 376 object = object[value]; 377 } 378 else if( object[value] !== undefined ) { 379 found = object[value]; 380 return false; 381 } 382 else { 383 module.error(error.method, query); 384 return false; 385 } 386 }); 387 } 388 if ( $.isFunction( found ) ) { 389 response = found.apply(context, passedArguments); 390 } 391 else if(found !== undefined) { 392 response = found; 393 } 394 if($.isArray(returnedValue)) { 395 returnedValue.push(response); 396 } 397 else if(returnedValue !== undefined) { 398 returnedValue = [returnedValue, response]; 399 } 400 else if(response !== undefined) { 401 returnedValue = response; 402 } 403 return found; 404 } 405 }; 406 407 if(methodInvoked) { 408 if(instance === undefined) { 409 module.initialize(); 410 } 411 module.invoke(query); 412 } 413 else { 414 if(instance !== undefined) { 415 module.destroy(); 416 } 417 module.initialize(); 418 } 419 return (returnedValue !== undefined) 420 ? returnedValue 421 : this 422 ; 423 }; 424 425 $.site.settings = { 426 427 name : 'Site', 428 namespace : 'site', 429 430 error : { 431 console : 'Console cannot be restored, most likely it was overwritten outside of module', 432 method : 'The method you called is not defined.' 433 }, 434 435 debug : false, 436 verbose : true, 437 performance : true, 438 439 modules: [ 440 'accordion', 441 'api', 442 'checkbox', 443 'dimmer', 444 'dropdown', 445 'form', 446 'modal', 447 'nag', 448 'popup', 449 'rating', 450 'shape', 451 'sidebar', 452 'state', 453 'sticky', 454 'tab', 455 'transition', 456 'video', 457 'visit', 458 'visibility' 459 ], 460 461 siteNamespace : 'site', 462 namespaceStub : { 463 cache : {}, 464 config : {}, 465 sections : {}, 466 section : {}, 467 utilities : {} 468 } 469 470 }; 471 472 // allows for selection of elements with data attributes 473 $.extend($.expr[ ":" ], { 474 data: ($.expr.createPseudo) 475 ? $.expr.createPseudo(function(dataName) { 476 return function(elem) { 477 return !!$.data(elem, dataName); 478 }; 479 }) 480 : function(elem, i, match) { 481 // support: jQuery < 1.8 482 return !!$.data(elem, match[ 3 ]); 483 } 484 }); 485 486 487 })( jQuery, window , document );