-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
775 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/*can-globals@0.0.0-pre.0#can-globals-proto*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./custom-event-pollyfill' | ||
], function (require, exports, module) { | ||
'use strict'; | ||
require('./custom-event-pollyfill'); | ||
function errorMessage(key) { | ||
return key + ' is not defined!'; | ||
} | ||
function Globals() { | ||
this.eventHandlers = {}; | ||
this.properties = {}; | ||
} | ||
Globals.prototype.define = function (key, value, enableCache) { | ||
if (!this.properties[key]) { | ||
this.properties[key] = { | ||
default: value, | ||
value: value, | ||
enableCache: enableCache | ||
}; | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.dispatch = function (name, key, value) { | ||
var event = new CustomEvent(name, { | ||
detail: { | ||
key: key, | ||
value: value | ||
} | ||
}); | ||
this.callHandlers(this.eventHandlers[key], event); | ||
this.callHandlers(this.eventHandlers[''], event); | ||
}; | ||
Globals.prototype.callHandlers = function (handlers, event) { | ||
if (handlers) { | ||
for (var i = 0; i < handlers.length; i++) { | ||
handlers[i](event); | ||
} | ||
} | ||
}; | ||
Globals.prototype.get = function (key) { | ||
var property = this.properties[key]; | ||
if (property) { | ||
if (typeof property.value === 'function') { | ||
if (property.enableCache) { | ||
property.value = property.value(); | ||
return property.value; | ||
} else { | ||
return property.value(); | ||
} | ||
} | ||
return property.value; | ||
} | ||
throw errorMessage(key); | ||
}; | ||
Globals.prototype.makeExport = function (key) { | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
return function (value) { | ||
if (arguments.length === 0) { | ||
return this.get(key); | ||
} | ||
if (typeof value === 'undefined') { | ||
this.reset(key); | ||
} else { | ||
this.set(key, value); | ||
} | ||
}.bind(this); | ||
}; | ||
Globals.prototype.off = function (key, handler) { | ||
if (arguments.length === 1) { | ||
handler = key; | ||
key = ''; | ||
} | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var i = handlers.indexOf(handler); | ||
handlers.splice(i, 1); | ||
return this; | ||
} | ||
}; | ||
Globals.prototype.on = function (key, handler) { | ||
if (arguments.length === 1) { | ||
handler = key; | ||
key = ''; | ||
} | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
if (!this.eventHandlers[key]) { | ||
this.eventHandlers[key] = []; | ||
} | ||
this.eventHandlers[key].push(handler); | ||
return this; | ||
}; | ||
Globals.prototype.reset = function (key) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var property = this.properties[key]; | ||
if (property !== undefined) { | ||
property.value = property.default; | ||
this.dispatch('reset', key, property.value); | ||
return this; | ||
} | ||
}; | ||
Globals.prototype.set = function (key, value) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var property = this.properties[key]; | ||
property.value = value; | ||
this.dispatch('set', key, value); | ||
return this; | ||
}; | ||
module.exports = Globals; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*can-globals@0.0.0-pre.0#can-globals*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-namespace', | ||
'./can-globals-proto', | ||
'./js/js' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
var Globals = require('./can-globals-proto'); | ||
var globals = new Globals(); | ||
require('./js/js')(globals); | ||
if (namespace.globals) { | ||
throw new Error('You can\'t have two versions of can-globals, check your dependencies'); | ||
} else { | ||
module.exports = namespace.globals = globals; | ||
} | ||
}(function () { | ||
return this; | ||
}())); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/*can-globals@0.0.0-pre.0#custom-event-pollyfill*/ | ||
define('can-globals@0.0.0-pre.0#custom-event-pollyfill', [ | ||
'module', | ||
'@loader', | ||
'require' | ||
], function (module, loader, require) { | ||
loader.get('@@global-helpers').prepareGlobal({ | ||
require: require, | ||
name: module.id, | ||
deps: [] | ||
}); | ||
var define = loader.global.define; | ||
var require = loader.global.require; | ||
var source = '/* global window, document */\n\'use strict\';\n// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill\n(function () {\n\n if ( typeof window.CustomEvent === "function" ) return false;\n\n function CustomEvent ( event, params ) {\n params = params || { bubbles: false, cancelable: false, detail: undefined };\n var evt = document.createEvent( \'CustomEvent\' );\n evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );\n return evt;\n }\n\n CustomEvent.prototype = window.Event.prototype;\n\n window.CustomEvent = CustomEvent;\n})();\n'; | ||
loader.global.define = undefined; | ||
loader.global.module = undefined; | ||
loader.global.exports = undefined; | ||
loader.__exec({ | ||
'source': source, | ||
'address': module.uri | ||
}); | ||
loader.global.require = require; | ||
loader.global.define = define; | ||
return loader.get('@@global-helpers').retrieveGlobal(module.id, undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*can-globals@0.0.0-pre.0#js/global/global*/ | ||
define(function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var GLOBAL; | ||
module.exports = function (setGlobal) { | ||
if (setGlobal !== undefined) { | ||
GLOBAL = setGlobal; | ||
} | ||
if (GLOBAL) { | ||
return GLOBAL; | ||
} else { | ||
return GLOBAL = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope ? self : typeof process === 'object' && {}.toString.call(process) === '[object process]' ? global : window; | ||
} | ||
}; | ||
}(function () { | ||
return this; | ||
}())); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*can-globals@0.0.0-pre.0#js/is-browser-window/is-browser-window*/ | ||
define(function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
module.exports = function () { | ||
return typeof window !== 'undefined' && typeof document !== 'undefined' && typeof SimpleDOM === 'undefined'; | ||
}; | ||
}(function () { | ||
return this; | ||
}())); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*can-globals@0.0.0-pre.0#js/js*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./global/global', | ||
'./is-browser-window/is-browser-window' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var importMap = { | ||
global: require('./global/global'), | ||
isBrowserWindow: require('./is-browser-window/is-browser-window') | ||
}; | ||
function defineGlobal(g, name) { | ||
g.define(name, function () { | ||
return importMap[name]; | ||
}); | ||
} | ||
module.exports = function (g) { | ||
for (var name in importMap) { | ||
if (importMap.hasOwnProperty(name)) { | ||
defineGlobal(g, name); | ||
} | ||
} | ||
}; | ||
}(function () { | ||
return this; | ||
}())); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*can-globals@0.0.0-pre.0#can-globals-proto*/ | ||
'use strict'; | ||
require('./custom-event-pollyfill.js'); | ||
function errorMessage(key) { | ||
return key + ' is not defined!'; | ||
} | ||
function Globals() { | ||
this.eventHandlers = {}; | ||
this.properties = {}; | ||
} | ||
Globals.prototype.define = function (key, value, enableCache) { | ||
if (!this.properties[key]) { | ||
this.properties[key] = { | ||
default: value, | ||
value: value, | ||
enableCache: enableCache | ||
}; | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.dispatch = function (name, key, value) { | ||
var event = new CustomEvent(name, { | ||
detail: { | ||
key: key, | ||
value: value | ||
} | ||
}); | ||
this.callHandlers(this.eventHandlers[key], event); | ||
this.callHandlers(this.eventHandlers[''], event); | ||
}; | ||
Globals.prototype.callHandlers = function (handlers, event) { | ||
if (handlers) { | ||
for (var i = 0; i < handlers.length; i++) { | ||
handlers[i](event); | ||
} | ||
} | ||
}; | ||
Globals.prototype.get = function (key) { | ||
var property = this.properties[key]; | ||
if (property) { | ||
if (typeof property.value === 'function') { | ||
if (property.enableCache) { | ||
property.value = property.value(); | ||
return property.value; | ||
} else { | ||
return property.value(); | ||
} | ||
} | ||
return property.value; | ||
} | ||
throw errorMessage(key); | ||
}; | ||
Globals.prototype.makeExport = function (key) { | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
return function (value) { | ||
if (arguments.length === 0) { | ||
return this.get(key); | ||
} | ||
if (typeof value === 'undefined') { | ||
this.reset(key); | ||
} else { | ||
this.set(key, value); | ||
} | ||
}.bind(this); | ||
}; | ||
Globals.prototype.off = function (key, handler) { | ||
if (arguments.length === 1) { | ||
handler = key; | ||
key = ''; | ||
} | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var i = handlers.indexOf(handler); | ||
handlers.splice(i, 1); | ||
return this; | ||
} | ||
}; | ||
Globals.prototype.on = function (key, handler) { | ||
if (arguments.length === 1) { | ||
handler = key; | ||
key = ''; | ||
} | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
if (!this.eventHandlers[key]) { | ||
this.eventHandlers[key] = []; | ||
} | ||
this.eventHandlers[key].push(handler); | ||
return this; | ||
}; | ||
Globals.prototype.reset = function (key) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var property = this.properties[key]; | ||
if (property !== undefined) { | ||
property.value = property.default; | ||
this.dispatch('reset', key, property.value); | ||
return this; | ||
} | ||
}; | ||
Globals.prototype.set = function (key, value) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var property = this.properties[key]; | ||
property.value = value; | ||
this.dispatch('set', key, value); | ||
return this; | ||
}; | ||
module.exports = Globals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*can-globals@0.0.0-pre.0#can-globals*/ | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
var Globals = require('./can-globals-proto.js'); | ||
var globals = new Globals(); | ||
require('./js/js.js')(globals); | ||
if (namespace.globals) { | ||
throw new Error('You can\'t have two versions of can-globals, check your dependencies'); | ||
} else { | ||
module.exports = namespace.globals = globals; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*can-globals@0.0.0-pre.0#custom-event-pollyfill*/ | ||
var loader = require('@loader'); | ||
loader.get('@@global-helpers').prepareGlobal({ | ||
require: require, | ||
name: module.id, | ||
deps: [] | ||
}); | ||
var define = loader.global.define; | ||
var require = loader.global.require; | ||
var source = '/* global window, document */\n\'use strict\';\n// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill\n(function () {\n\n if ( typeof window.CustomEvent === "function" ) return false;\n\n function CustomEvent ( event, params ) {\n params = params || { bubbles: false, cancelable: false, detail: undefined };\n var evt = document.createEvent( \'CustomEvent\' );\n evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );\n return evt;\n }\n\n CustomEvent.prototype = window.Event.prototype;\n\n window.CustomEvent = CustomEvent;\n})();\n'; | ||
loader.global.define = undefined; | ||
loader.global.module = undefined; | ||
loader.global.exports = undefined; | ||
loader.__exec({ | ||
'source': source, | ||
'address': module.uri | ||
}); | ||
loader.global.require = require; | ||
loader.global.define = define; | ||
module.exports = loader.get('@@global-helpers').retrieveGlobal(module.id, undefined); |
Oops, something went wrong.