-
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
17 changed files
with
866 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,23 @@ | ||
/*can-globals@0.2.2#can-globals-instance*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-namespace', | ||
'./can-globals-proto' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
var Globals = require('./can-globals-proto'); | ||
var globals = new 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; | ||
} | ||
module.exports = 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,131 @@ | ||
/*can-globals@0.2.2#can-globals-proto*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-reflect' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var canReflect = require('can-reflect'); | ||
function dispatch(key, value) { | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var handlersCopy = handlers.slice(); | ||
for (var i = 0; i < handlersCopy.length; i++) { | ||
handlersCopy[i](value); | ||
} | ||
} | ||
} | ||
function Globals() { | ||
this.eventHandlers = {}; | ||
this.properties = {}; | ||
} | ||
Globals.prototype.define = function (key, value, enableCache) { | ||
if (enableCache === undefined) { | ||
enableCache = true; | ||
} | ||
if (!this.properties[key]) { | ||
this.properties[key] = { | ||
default: value, | ||
value: value, | ||
enableCache: enableCache | ||
}; | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.getKeyValue = function (key) { | ||
var property = this.properties[key]; | ||
if (property) { | ||
if (typeof property.value === 'function') { | ||
if (property.cachedValue) { | ||
return property.cachedValue; | ||
} | ||
if (property.enableCache) { | ||
property.cachedValue = property.value(); | ||
return property.cachedValue; | ||
} else { | ||
return property.value(); | ||
} | ||
} | ||
return property.value; | ||
} | ||
}; | ||
Globals.prototype.makeExport = function (key) { | ||
return function (value) { | ||
if (arguments.length === 0) { | ||
return this.getKeyValue(key); | ||
} | ||
if (typeof value === 'undefined' || value === null) { | ||
this.deleteKeyValue(key); | ||
} else { | ||
if (typeof value === 'function') { | ||
this.setKeyValue(key, function () { | ||
return value; | ||
}); | ||
} else { | ||
this.setKeyValue(key, value); | ||
} | ||
return value; | ||
} | ||
}.bind(this); | ||
}; | ||
Globals.prototype.offKeyValue = function (key, handler) { | ||
if (this.properties[key]) { | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var i = handlers.indexOf(handler); | ||
handlers.splice(i, 1); | ||
} | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.onKeyValue = function (key, handler) { | ||
if (this.properties[key]) { | ||
if (!this.eventHandlers[key]) { | ||
this.eventHandlers[key] = []; | ||
} | ||
this.eventHandlers[key].push(handler); | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.deleteKeyValue = function (key) { | ||
var property = this.properties[key]; | ||
if (property !== undefined) { | ||
property.value = property.default; | ||
property.cachedValue = undefined; | ||
dispatch.call(this, key, property.value); | ||
} | ||
return this; | ||
}; | ||
Globals.prototype.setKeyValue = function (key, value) { | ||
if (!this.properties[key]) { | ||
return this.define(key, value); | ||
} | ||
var property = this.properties[key]; | ||
property.value = value; | ||
property.cachedValue = undefined; | ||
dispatch.call(this, key, value); | ||
return this; | ||
}; | ||
Globals.prototype.reset = function () { | ||
for (var key in this.properties) { | ||
if (this.properties.hasOwnProperty(key)) { | ||
this.properties[key].cachedValue = undefined; | ||
dispatch.call(this, key, this.getKeyValue(key)); | ||
} | ||
} | ||
return this; | ||
}; | ||
canReflect.assignSymbols(Globals.prototype, { | ||
'can.getKeyValue': Globals.prototype.getKeyValue, | ||
'can.setKeyValue': Globals.prototype.setKeyValue, | ||
'can.deleteKeyValue': Globals.prototype.deleteKeyValue, | ||
'can.onKeyValue': Globals.prototype.onKeyValue, | ||
'can.offKeyValue': Globals.prototype.offKeyValue | ||
}); | ||
module.exports = 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.2.2#can-globals*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./can-globals-instance', | ||
'./global/global', | ||
'./document/document', | ||
'./location/location', | ||
'./mutation-observer/mutation-observer', | ||
'./is-browser-window/is-browser-window' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var globals = require('./can-globals-instance'); | ||
require('./global/global'); | ||
require('./document/document'); | ||
require('./location/location'); | ||
require('./mutation-observer/mutation-observer'); | ||
require('./is-browser-window/is-browser-window'); | ||
module.exports = 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,20 @@ | ||
/*can-globals@0.2.2#document/document*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'../global/global', | ||
'../can-globals-instance' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
require('../global/global'); | ||
var globals = require('../can-globals-instance'); | ||
globals.define('document', function () { | ||
return globals.getKeyValue('global').document; | ||
}); | ||
module.exports = globals.makeExport('document'); | ||
}(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,18 @@ | ||
/*can-globals@0.2.2#global/global*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'../can-globals-instance' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var globals = require('../can-globals-instance'); | ||
globals.define('global', function () { | ||
return typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope ? self : typeof process === 'object' && {}.toString.call(process) === '[object process]' ? global : window; | ||
}); | ||
module.exports = globals.makeExport('global'); | ||
}(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,18 @@ | ||
/*can-globals@0.2.2#is-browser-window/is-browser-window*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'../can-globals-instance' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
var globals = require('../can-globals-instance'); | ||
globals.define('isBrowserWindow', function () { | ||
return typeof window !== 'undefined' && typeof document !== 'undefined' && typeof SimpleDOM === 'undefined'; | ||
}); | ||
module.exports = globals.makeExport('isBrowserWindow'); | ||
}(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,20 @@ | ||
/*can-globals@0.2.2#location/location*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'../global/global', | ||
'../can-globals-instance' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
require('../global/global'); | ||
var globals = require('../can-globals-instance'); | ||
globals.define('location', function () { | ||
return globals.getKeyValue('global').location; | ||
}); | ||
module.exports = globals.makeExport('location'); | ||
}(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,21 @@ | ||
/*can-globals@0.2.2#mutation-observer/mutation-observer*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'../global/global', | ||
'../can-globals-instance' | ||
], function (require, exports, module) { | ||
(function (global) { | ||
'use strict'; | ||
require('../global/global'); | ||
var globals = require('../can-globals-instance'); | ||
globals.define('MutationObserver', function () { | ||
var GLOBAL = globals.getKeyValue('global'); | ||
return GLOBAL.MutationObserver || GLOBAL.WebKitMutationObserver || GLOBAL.MozMutationObserver; | ||
}); | ||
module.exports = globals.makeExport('MutationObserver'); | ||
}(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.2.2#can-globals-instance*/ | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
var Globals = require('./can-globals-proto.js'); | ||
var globals = new 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; | ||
} | ||
module.exports = globals; |
Oops, something went wrong.