Skip to content

Commit

Permalink
0.0.0-pre.1
Browse files Browse the repository at this point in the history
  • Loading branch information
imaustink committed Aug 4, 2017
1 parent a2979e9 commit fadf18e
Show file tree
Hide file tree
Showing 13 changed files with 775 additions and 0 deletions.
124 changes: 124 additions & 0 deletions dist/amd/can-globals-proto.js
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;
});
24 changes: 24 additions & 0 deletions dist/amd/can-globals.js
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;
}()));
});
25 changes: 25 additions & 0 deletions dist/amd/custom-event-pollyfill.js
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);
});
19 changes: 19 additions & 0 deletions dist/amd/js/global/global.js
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;
}()));
});
11 changes: 11 additions & 0 deletions dist/amd/js/is-browser-window/is-browser-window.js
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;
}()));
});
30 changes: 30 additions & 0 deletions dist/amd/js/js.js
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;
}()));
});
117 changes: 117 additions & 0 deletions dist/cjs/can-globals-proto.js
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;
11 changes: 11 additions & 0 deletions dist/cjs/can-globals.js
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;
}
20 changes: 20 additions & 0 deletions dist/cjs/custom-event-pollyfill.js
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);
Loading

0 comments on commit fadf18e

Please sign in to comment.