📄 sunrise_event.js
字号:
SunRise.CustomEvent = function(type, oScope, silent, signature) {
this.type = type;
this.scope = oScope || window;
this.silent = silent;
this.signature = signature || SunRise.CustomEvent.LIST;
this.subscribers = [];
if (!this.silent) {
}
this.lastError = null;
};
SunRise.CustomEvent.LIST = 0;
SunRise.CustomEvent.FLAT = 1;
SunRise.CustomEvent.prototype = {
subscribe: function(fn, obj, override) {
if (!fn) {
throw new Error("Invalid callback for subscriber to '" + this.type + "'");
}
if (this.subscribeEvent) {
this.subscribeEvent.fire(fn, obj, override);
}
this.subscribers.push( new SunRise.Subscriber(fn, obj, override) );
},
unsubscribe: function(fn, obj) {
if (!fn) {
return this.unsubscribeAll();
}
var found = false;
for (var i=0, len=this.subscribers.length; i<len; ++i) {
var s = this.subscribers[i];
if (s && s.contains(fn, obj)) {
this._delete(i);
found = true;
}
}
return found;
},
fire: function() {
this.lastError = null;
var errors = [],
len=this.subscribers.length;
if (!len && this.silent) {
return true;
}
var args=[].slice.call(arguments, 0), ret=true, i, rebuild=false;
if (!this.silent) {
}
var subs = this.subscribers.slice(), throwErrors = SunRise.Event.throwErrors;
for (i=0; i<len; ++i) {
var s = subs[i];
if (!s) {
rebuild=true;
} else {
if (!this.silent) {
}
var scope = s.getScope(this.scope);
if (this.signature == SunRise.CustomEvent.FLAT) {
var param = null;
if (args.length > 0) {
param = args[0];
}
try {
ret = s.fn.call(scope, param, s.obj);
} catch(e) {
this.lastError = e;
// errors.push(e);
if (throwErrors) {
throw e;
}
}
} else {
try {
ret = s.fn.call(scope, this.type, args, s.obj);
} catch(ex) {
this.lastError = ex;
if (throwErrors) {
throw ex;
}
}
}
if (false === ret) {
if (!this.silent) {
}
break;
// return false;
}
}
}
return (ret !== false);
},
unsubscribeAll: function() {
for (var i=this.subscribers.length-1; i>-1; i--) {
this._delete(i);
}
this.subscribers=[];
return i;
},
_delete: function(index) {
var s = this.subscribers[index];
if (s) {
delete s.fn;
delete s.obj;
}
this.subscribers.splice(index, 1);
}
};
if (!SunRise.Event) {
SunRise.Event = function() {
var loadComplete = false;
var listeners = [];
var unloadListeners = [];
var legacyEvents = [];
var legacyHandlers = [];
var retryCount = 0;
var onAvailStack = [];
var legacyMap = [];
var counter = 0;
var webkitKeymap = {
63232: 38, // up
63233: 40, // down
63234: 37, // left
63235: 39, // right
63276: 33, // page up
63277: 34, // page down
25: 9 // SHIFT-TAB (Safari provides a different key code in
// this case, even though the shiftKey modifier is set)
};
return {
POLL_RETRYS: 2000,
POLL_INTERVAL: 20,
EL: 0,
TYPE: 1,
FN: 2,
WFN: 3,
UNLOAD_OBJ: 3,
ADJ_SCOPE: 4,
OBJ: 5,
OVERRIDE: 6,
lastError: null,
isSafari: SunRise.ua.webkit,
webkit: SunRise.ua.webkit,
isIE: SunRise.ua.ie,
_interval: null,
_dri: null,
DOMReady: false,
throwErrors: false,
startInterval: function() {
if (!this._interval) {
var self = this;
var callback = function() { self._tryPreloadAttach(); };
this._interval = setInterval(callback, this.POLL_INTERVAL);
}
},
onAvailable: function(p_id, p_fn, p_obj, p_override, checkContent) {
var a = (SunRise.lang.isString(p_id)) ? [p_id] : p_id;
for (var i=0; i<a.length; i=i+1) {
onAvailStack.push({id: a[i],
fn: p_fn,
obj: p_obj,
override: p_override,
checkReady: checkContent });
}
retryCount = this.POLL_RETRYS;
this.startInterval();
},
onContentReady: function(p_id, p_fn, p_obj, p_override) {
this.onAvailable(p_id, p_fn, p_obj, p_override, true);
},
onDOMReady: function(p_fn, p_obj, p_override) {
if (this.DOMReady) {
setTimeout(function() {
var s = window;
if (p_override) {
if (p_override === true) {
s = p_obj;
} else {
s = p_override;
}
}
p_fn.call(s, "DOMReady", [], p_obj);
}, 0);
} else {
this.DOMReadyEvent.subscribe(p_fn, p_obj, p_override);
}
},
addListener: function(el, sType, fn, obj, override) {
if (!fn || !fn.call) {
return false;
}
// The el argument can be an array of elements or element ids.
if ( this._isValidCollection(el)) {
var ok = true;
for (var i=0,len=el.length; i<len; ++i) {
ok = this.on(el[i],
sType,
fn,
obj,
override) && ok;
}
return ok;
} else if (SunRise.lang.isString(el)) {
var oEl = this.getEl(el);
if (oEl) {
el = oEl;
} else {
// defer adding the event until the element is available
this.onAvailable(el, function() {
SunRise.Event.on(el, sType, fn, obj, override);
});
return true;
}
}
// Element should be an html element or an array if we get
// here.
if (!el) {
return false;
}
if ("unload" == sType && obj !== this) {
unloadListeners[unloadListeners.length] =
[el, sType, fn, obj, override];
return true;
}
var scope = el;
if (override) {
if (override === true) {
scope = obj;
} else {
scope = override;
}
}
var wrappedFn = function(e) {
return fn.call(scope, SunRise.Event.getEvent(e, el),
obj);
};
var li = [el, sType, fn, wrappedFn, scope, obj, override];
var index = listeners.length;
// cache the listener so we can try to automatically unload
listeners[index] = li;
if (this.useLegacyEvent(el, sType)) {
var legacyIndex = this.getLegacyIndex(el, sType);
if ( legacyIndex == -1 ||
el != legacyEvents[legacyIndex][0] ) {
legacyIndex = legacyEvents.length;
legacyMap[el.id + sType] = legacyIndex;
// cache the signature for the DOM0 event, and
// include the existing handler for the event, if any
legacyEvents[legacyIndex] =
[el, sType, el["on" + sType]];
legacyHandlers[legacyIndex] = [];
el["on" + sType] =
function(e) {
SunRise.Event.fireLegacyEvent(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -