manage prefix on command names to ovoid conflict in multisite context

This commit is contained in:
a-Sansara 2016-01-11 10:37:29 +01:00
parent a2382c0a5a
commit 34a76390a6
2 changed files with 64 additions and 32 deletions

2
src/bt-min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -127,33 +127,19 @@ var $j = (function alias() {
var $bt = { var $bt = {
VERSION : 0.8, VERSION : 0.8,
UID : null,
HASH : null,
TRACE : true && !$.isNone(console), TRACE : true && !$.isNone(console),
/*! @constant LS_TABS localStorage key for browsertabs list */ setConstant : function(value, name) {
LS_TABS : 'bt.list', if (!name) {
/*! @constant LS_CURTAB localStorage key for current browsertab */ if (this.UID == null) throw Error('UID is not define, you need to define $bt.UID or provide the "name" parameter');
LS_CURTAB : 'bt.current', else if (this.HASH == null) {
/*! @constant LS_CMD localStorage key command to interact with other tabs */ this.HASH = this._getHash(this.UID);
LS_CMD : 'bt.event', }
/*! @constant CMD_SYNC internal command to perform a browser tab synchro */ return this.HASH+'.'+value;
CMD_SYNC : 'bt.sync', }
/*! @constant CMD_VAR_SET internal command to perform a browser tab var set */ return _getHash(name)+'.'+value;
CMD_VAR_SET : 'bt.varset', },
/*! @constant CMD_VAR_SYNC internal command to perform a browser tab var sync */
CMD_VAR_SYNC : 'bt.varsync',
/*! @constant CMD_ATTR_SYNC internal command to perform a dom sync attribute */
CMD_ATTR_SYNC: 'bt.attr',
/*! @constant CMD_APPEND internal command to perform a dom append */
CMD_APPEND : 'bt.dom.append',
/*! @constant CMD_PREPEND internal command to perform a dom append */
CMD_PREPEND : 'bt.dom.prepend',
/*! @constant CMD_HTML internal command to perform a dom html */
CMD_HTML : 'bt.dom.rewrite',
/*! @constant CMD_RELOAD internal command to perform a browser tab reload */
CMD_RELOAD : 'bt.reload',
/*! @constant CMD_ZOMBKILL internal command to perform a browser tab zombies kill */
CMD_ZOMBKILL : 'bt.zombkill',
/*! @constant CMD_DONTKILL internal command to perform a dontkill browser tab (CMD_ZOMBKILL reply) */
CMD_DONTKILL : 'bt.dontkill',
/*! @var vars */ /*! @var vars */
vars : [], vars : [],
/*! @var callbacks */ /*! @var callbacks */
@ -170,7 +156,13 @@ var $bt = {
* @method init * @method init
* @param string fn a function to call on initializing on dom ready * @param string fn a function to call on initializing on dom ready
*/ */
init : function(fn) { init : function(uid, fn) {
if (!fn && $.isFunc(uid) || arguments.length==0) {
if (this.UID == null) throw Error('UID is not define, you need to define $bt.UID or provide the "uid" parameter');
if (arguments.length > 0) fn = uid;
uid = this.UID;
}
this._initConstant(uid);
this._init(fn); this._init(fn);
}, },
/*! /*!
@ -389,6 +381,46 @@ var $bt = {
if (i > -1) $bt.list.splice(i, 1); if (i > -1) $bt.list.splice(i, 1);
}, },
/*! @private */ /*! @private */
_getHash : function(name) {
name = window.location.host + '#' + name;
var uid = 0;
for (var i = 0, c = 0, lim = name.length; i < lim; i++) {
uid = ((uid<<5)-uid)+name.charCodeAt(i);
uid = uid & uid; // Convert to 32bit integer
}
return Math.abs(uid);
},
/*! @private */
_initConstant : function(name) {
var uid = this.HASH != null ? this.HASH : this._getHash(name);
/*! @constant LS_TABS localStorage key for browsertabs list */
this.LS_TABS = uid+'.bt.list',
/*! @constant LS_CURTAB localStorage key for current browsertab */
this.LS_CURTAB = uid+'.bt.current',
/*! @constant LS_CMD localStorage key command to interact with other tabs */
this.LS_CMD = uid+'.bt.event',
/*! @constant CMD_SYNC internal command to perform a browser tab synchro */
this.CMD_SYNC = uid+'.bt.sync',
/*! @constant CMD_VAR_SET internal command to perform a browser tab var set */
this.CMD_VAR_SET = uid+'.bt.varset',
/*! @constant CMD_VAR_SYNC internal command to perform a browser tab var sync */
this.CMD_VAR_SYNC = uid+'.bt.varsync',
/*! @constant CMD_ATTR_SYNC internal command to perform a dom sync attribute */
this.CMD_ATTR_SYNC = uid+'.bt.attr',
/*! @constant CMD_APPEND internal command to perform a dom append */
this.CMD_APPEND = uid+'.bt.dom.append',
/*! @constant CMD_PREPEND internal command to perform a dom append */
this.CMD_PREPEND = uid+'.bt.dom.prepend',
/*! @constant CMD_HTML internal command to perform a dom html */
this.CMD_HTML = uid+'.bt.dom.rewrite',
/*! @constant CMD_RELOAD internal command to perform a browser tab reload */
this.CMD_RELOAD = uid+'.bt.reload',
/*! @constant CMD_ZOMBKILL internal command to perform a browser tab zombies kill */
this.CMD_ZOMBKILL = uid+'.bt.zombkill',
/*! @constant CMD_DONTKILL internal command to perform a dontkill browser tab (CMD_ZOMBKILL reply) */
this.CMD_DONTKILL = uid+'.bt.dontkill'
},
/*! @private */
_init : function(fn) { _init : function(fn) {
$(window).on('beforeunload', $bt._unload); $(window).on('beforeunload', $bt._unload);
$(window).on('storage', $bt._cmd); $(window).on('storage', $bt._cmd);
@ -450,8 +482,8 @@ var $bt = {
var cmd = $j.obj(e.newValue); var cmd = $j.obj(e.newValue);
if (!cmd) return; if (!cmd) return;
if (cmd.to == "*" || cmd.to == $bt.id) { if (cmd.to == "*" || cmd.to == $bt.id) {
$bt.log('RECEIVING cmd '+cmd.name+' : '); console.log('RECEIVING cmd '+cmd.name+' : ');
$bt.log(cmd); console.log(cmd);
try { try {
if (!$.isNone(cmd.context) && cmd.context!=null && !$.isNone(window.parent.frames[cmd.context])) { if (!$.isNone(cmd.context) && cmd.context!=null && !$.isNone(window.parent.frames[cmd.context])) {
cmd.context = window.parent.frames[cmd.context].document; cmd.context = window.parent.frames[cmd.context].document;
@ -496,11 +528,11 @@ var $bt = {
break; break;
case $bt.CMD_VAR_SET : case $bt.CMD_VAR_SET :
$bt.vars[cmd.varName] = cmd.data; $bt.vars[cmd.varName] = $j.obj(cmd.data);
break; break;
case $bt.CMD_VAR_SYNC : case $bt.CMD_VAR_SYNC :
$bt.varset(cmd.varName, $bt.vars[cmd.varName]); if (!$.isNone($bt.vars[cmd.varName])) $bt.varset(cmd.varName, $bt.vars[cmd.varName]);
break; break;
case $bt.CMD_ATTR_SYNC : case $bt.CMD_ATTR_SYNC :