forked from fanruan/fineui
iapyang
3 years ago
49 changed files with 4777 additions and 4411 deletions
Binary file not shown.
Before Width: | Height: | Size: 456 KiB After Width: | Height: | Size: 455 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,513 @@
|
||||
!(function () { |
||||
/** |
||||
* @class BI.Bubble |
||||
* @extends BI.Widget |
||||
*/ |
||||
BI.Bubble = BI.inherit(BI.Widget, { |
||||
_defaultConfig: function () { |
||||
var conf = BI.Bubble.superclass._defaultConfig.apply(this, arguments); |
||||
return BI.extend(conf, { |
||||
baseCls: (conf.baseCls || "") + " bi-popper", |
||||
attributes: { |
||||
tabIndex: -1 |
||||
}, |
||||
trigger: "click", // click || hover || click-hover || ""
|
||||
toggle: true, |
||||
direction: "", |
||||
placement: "bottom-start", // top-start/top/top-end/bottom-start/bottom/bottom-end/left-start/left/left-end/right-start/right/right-end
|
||||
logic: { |
||||
dynamic: true |
||||
}, |
||||
container: null, // popupview放置的容器,默认为this.element
|
||||
isDefaultInit: false, |
||||
destroyWhenHide: false, |
||||
hideWhenClickOutside: true, |
||||
showArrow: true, |
||||
hideWhenBlur: false, |
||||
isNeedAdjustHeight: true, // 是否需要高度调整
|
||||
isNeedAdjustWidth: true, |
||||
stopEvent: false, |
||||
stopPropagation: false, |
||||
adjustLength: 0, // 调整的距离
|
||||
adjustXOffset: 0, |
||||
adjustYOffset: 0, |
||||
hideChecker: BI.emptyFn, |
||||
offsetStyle: "left", // left,right,center
|
||||
el: {}, |
||||
popup: {}, |
||||
comboClass: "bi-combo-popup", |
||||
hoverClass: "bi-combo-hover", |
||||
}); |
||||
}, |
||||
|
||||
render: function () { |
||||
var self = this, o = this.options; |
||||
this._initCombo(); |
||||
// 延迟绑定事件,这样可以将自己绑定的事情优先执行
|
||||
BI.nextTick(this._initPullDownAction.bind(this)); |
||||
this.combo.on(BI.Controller.EVENT_CHANGE, function (type, value, obj) { |
||||
if (self.isEnabled() && self.isValid()) { |
||||
if (type === BI.Events.EXPAND) { |
||||
self._popupView(); |
||||
} |
||||
if (type === BI.Events.COLLAPSE) { |
||||
self._hideView(); |
||||
} |
||||
if (type === BI.Events.EXPAND) { |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments); |
||||
self.fireEvent(BI.Bubble.EVENT_EXPAND); |
||||
} |
||||
if (type === BI.Events.COLLAPSE) { |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments); |
||||
self.isViewVisible() && self.fireEvent(BI.Bubble.EVENT_COLLAPSE); |
||||
} |
||||
if (type === BI.Events.CLICK) { |
||||
self.fireEvent(BI.Bubble.EVENT_TRIGGER_CHANGE, obj); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
self.element.on("mouseenter." + self.getName(), function (e) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid()) { |
||||
self.element.addClass(o.hoverClass); |
||||
} |
||||
}); |
||||
self.element.on("mouseleave." + self.getName(), function (e) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid()) { |
||||
self.element.removeClass(o.hoverClass); |
||||
} |
||||
}); |
||||
|
||||
BI.createWidget(BI.extend({ |
||||
element: this |
||||
}, BI.LogicFactory.createLogic("vertical", BI.extend(o.logic, { |
||||
items: [ |
||||
{el: this.combo} |
||||
] |
||||
})))); |
||||
o.isDefaultInit && (this._assertPopupView()); |
||||
}, |
||||
|
||||
_toggle: function (e) { |
||||
this._assertPopupViewRender(); |
||||
if (this.popupView.isVisible()) { |
||||
this._hideView(e); |
||||
} else { |
||||
if (this.isEnabled()) { |
||||
this._popupView(e); |
||||
} |
||||
} |
||||
}, |
||||
|
||||
_initPullDownAction: function () { |
||||
var self = this, o = this.options; |
||||
var evs = (this.options.trigger || "").split(","); |
||||
var st = function (e) { |
||||
if (o.stopEvent) { |
||||
e.stopEvent(); |
||||
} |
||||
if (o.stopPropagation) { |
||||
e.stopPropagation(); |
||||
} |
||||
}; |
||||
|
||||
var enterPopup = false; |
||||
|
||||
function hide (e) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid() && o.toggle === true) { |
||||
self._hideView(e); |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, BI.Events.COLLAPSE, "", self.combo); |
||||
self.fireEvent(BI.Bubble.EVENT_COLLAPSE); |
||||
} |
||||
self.popupView && self.popupView.element.off("mouseenter." + self.getName()).off("mouseleave." + self.getName()); |
||||
enterPopup = false; |
||||
} |
||||
|
||||
BI.each(evs, function (i, ev) { |
||||
switch (ev) { |
||||
case "hover": |
||||
self.element.on("mouseenter." + self.getName(), function (e) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid()) { |
||||
self._popupView(e); |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, BI.Events.EXPAND, "", self.combo); |
||||
self.fireEvent(BI.Bubble.EVENT_EXPAND); |
||||
} |
||||
}); |
||||
self.element.on("mouseleave." + self.getName(), function (e) { |
||||
if (self.popupView) { |
||||
self.popupView.element.on("mouseenter." + self.getName(), function (e) { |
||||
enterPopup = true; |
||||
self.popupView.element.on("mouseleave." + self.getName(), function (e) { |
||||
hide(e); |
||||
}); |
||||
self.popupView.element.off("mouseenter." + self.getName()); |
||||
}); |
||||
BI.defer(function () { |
||||
if (!enterPopup) { |
||||
hide(e); |
||||
} |
||||
}, 50); |
||||
} |
||||
}); |
||||
break; |
||||
case "click": |
||||
var debounce = BI.debounce(function (e) { |
||||
if (self.combo.element.__isMouseInBounds__(e)) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid()) { |
||||
// if (!o.toggle && self.isViewVisible()) {
|
||||
// return;
|
||||
// }
|
||||
o.toggle ? self._toggle(e) : self._popupView(e); |
||||
if (self.isViewVisible()) { |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, BI.Events.EXPAND, "", self.combo); |
||||
self.fireEvent(BI.Bubble.EVENT_EXPAND); |
||||
} else { |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, BI.Events.COLLAPSE, "", self.combo); |
||||
self.fireEvent(BI.Bubble.EVENT_COLLAPSE); |
||||
} |
||||
} |
||||
} |
||||
}, BI.EVENT_RESPONSE_TIME, { |
||||
"leading": true, |
||||
"trailing": false |
||||
}); |
||||
self.element.off(ev + "." + self.getName()).on(ev + "." + self.getName(), function (e) { |
||||
debounce(e); |
||||
st(e); |
||||
}); |
||||
break; |
||||
case "click-hover": |
||||
var debounce = BI.debounce(function (e) { |
||||
if (self.combo.element.__isMouseInBounds__(e)) { |
||||
if (self.isEnabled() && self.isValid() && self.combo.isEnabled() && self.combo.isValid()) { |
||||
// if (self.isViewVisible()) {
|
||||
// return;
|
||||
// }
|
||||
self._popupView(e); |
||||
if (self.isViewVisible()) { |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, BI.Events.EXPAND, "", self.combo); |
||||
self.fireEvent(BI.Bubble.EVENT_EXPAND); |
||||
} |
||||
} |
||||
} |
||||
}, BI.EVENT_RESPONSE_TIME, { |
||||
"leading": true, |
||||
"trailing": false |
||||
}); |
||||
self.element.off("click." + self.getName()).on("click." + self.getName(), function (e) { |
||||
debounce(e); |
||||
st(e); |
||||
}); |
||||
self.element.on("mouseleave." + self.getName(), function (e) { |
||||
if (self.popupView) { |
||||
self.popupView.element.on("mouseenter." + self.getName(), function (e) { |
||||
enterPopup = true; |
||||
self.popupView.element.on("mouseleave." + self.getName(), function (e) { |
||||
hide(e); |
||||
}); |
||||
self.popupView.element.off("mouseenter." + self.getName()); |
||||
}); |
||||
BI.delay(function () { |
||||
if (!enterPopup) { |
||||
hide(e); |
||||
} |
||||
}, 50); |
||||
} |
||||
}); |
||||
break; |
||||
} |
||||
}); |
||||
}, |
||||
|
||||
_initCombo: function () { |
||||
this.combo = BI.createWidget(this.options.el, { |
||||
value: this.options.value |
||||
}); |
||||
|
||||
if (this.options.showArrow) { |
||||
this.arrow = BI.createWidget({ |
||||
type: "bi.absolute", |
||||
cls: "bi-bubble-arrow", |
||||
items: [{ |
||||
type: "bi.layout", |
||||
cls: "bubble-arrow" |
||||
}] |
||||
}); |
||||
} |
||||
}, |
||||
|
||||
_assertPopupView: function () { |
||||
var self = this, o = this.options; |
||||
if (this.popupView == null) { |
||||
this.popupView = BI.createWidget(this.options.popup, { |
||||
type: "bi.bubble_popup_view", |
||||
value: o.value |
||||
}, this); |
||||
if (this.options.showArrow) { |
||||
BI.createWidget({ |
||||
type: "bi.absolute", |
||||
element: this.popupView, |
||||
items: [{ |
||||
el: this.arrow |
||||
}] |
||||
}); |
||||
} |
||||
this.popupView.on(BI.Controller.EVENT_CHANGE, function (type, value, obj) { |
||||
if (type === BI.Events.CLICK) { |
||||
self.combo.setValue(self.getValue()); |
||||
self.fireEvent(BI.Bubble.EVENT_CHANGE, value, obj); |
||||
} |
||||
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments); |
||||
}); |
||||
this.popupView.setVisible(false); |
||||
BI.nextTick(function () { |
||||
self.fireEvent(BI.Bubble.EVENT_AFTER_INIT); |
||||
}); |
||||
} |
||||
}, |
||||
|
||||
_assertPopupViewRender: function () { |
||||
this._assertPopupView(); |
||||
if (!this._rendered) { |
||||
BI.createWidget({ |
||||
type: "bi.vertical", |
||||
scrolly: false, |
||||
element: this.options.container || this, |
||||
items: [ |
||||
{el: this.popupView} |
||||
] |
||||
}); |
||||
this._rendered = true; |
||||
} |
||||
}, |
||||
|
||||
_hideIf: function (e, skipTriggerChecker) { |
||||
// if (this.element.__isMouseInBounds__(e) || (this.popupView && this.popupView.element.__isMouseInBounds__(e))) {
|
||||
// return;
|
||||
// }
|
||||
// BI-10290 公式combo双击公式内容会收起
|
||||
if (e && ((skipTriggerChecker !== true && this.element.find(e.target).length > 0) |
||||
|| (this.popupView && this.popupView.element.find(e.target).length > 0) |
||||
|| e.target.className === "CodeMirror-cursor" || BI.Widget._renderEngine.createElement(e.target).closest(".CodeMirror-hints").length > 0)) {// BI-9887 CodeMirror的公式弹框需要特殊处理下
|
||||
var directions = this.options.direction.split(","); |
||||
if (BI.contains(directions, "innerLeft") || BI.contains(directions, "innerRight")) { |
||||
// popup可以出现在trigger内部的combo,滚动时不需要消失,而是调整位置
|
||||
this.adjustWidth(); |
||||
this.adjustHeight(); |
||||
} |
||||
|
||||
return; |
||||
} |
||||
var isHide = this.options.hideChecker.apply(this, [e]); |
||||
if (isHide === false) { |
||||
return; |
||||
} |
||||
this._hideView(e); |
||||
return true; |
||||
}, |
||||
|
||||
_hideView: function (e) { |
||||
var o = this.options; |
||||
this.fireEvent(BI.Bubble.EVENT_BEFORE_HIDEVIEW); |
||||
if (this.options.destroyWhenHide === true) { |
||||
this.popupView && this.popupView.destroy(); |
||||
this.popupView = null; |
||||
this._rendered = false; |
||||
} else { |
||||
this.popupView && this.popupView.invisible(); |
||||
} |
||||
|
||||
if (!e || !this.combo.element.__isMouseInBounds__(e)) { |
||||
this.element.removeClass(this.options.hoverClass); |
||||
// 应对bi-focus-shadow在收起时不失焦
|
||||
this.element.blur(); |
||||
} |
||||
|
||||
if (this.popper) { |
||||
this.popper.destroy(); |
||||
this.popper = null; |
||||
} |
||||
|
||||
this.element.removeClass(this.options.comboClass); |
||||
|
||||
BI.Widget._renderEngine.createElement(document).unbind("mousedown." + this.getName()).unbind("mousewheel." + this.getName()); |
||||
BI.EVENT_BLUR && o.hideWhenBlur && BI.Widget._renderEngine.createElement(window).unbind("blur." + this.getName()); |
||||
this.fireEvent(BI.Bubble.EVENT_AFTER_HIDEVIEW); |
||||
}, |
||||
|
||||
_popupView: function (e) { |
||||
var self = this, o = this.options; |
||||
this._assertPopupViewRender(); |
||||
this.fireEvent(BI.Bubble.EVENT_BEFORE_POPUPVIEW); |
||||
// popupVisible是为了获取其宽高, 放到可视范围之外以防止在IE下闪一下
|
||||
this.popupView.css({left: -999999999, top: -99999999}); |
||||
this.popupView.visible(); |
||||
this.adjustWidth(e); |
||||
|
||||
if (this.popper) { |
||||
this.popper.destroy(); |
||||
} |
||||
var modifiers = [{ |
||||
name: "offset", |
||||
options: { |
||||
offset: function () { |
||||
return [o.adjustXOffset, (o.showArrow ? 9 : 0) + (o.adjustYOffset || o.adjustLength)]; |
||||
} |
||||
} |
||||
}]; |
||||
if (this.options.showArrow) { |
||||
modifiers.push({ |
||||
name: "arrow", |
||||
options: { |
||||
padding: 5, |
||||
element: this.arrow.element[0] |
||||
} |
||||
}); |
||||
} |
||||
this.popper = BI.Popper.createPopper(this.combo.element[0], this.popupView.element[0], { |
||||
placement: o.placement, |
||||
strategy: "fixed", |
||||
modifiers: modifiers |
||||
}); |
||||
|
||||
// this.adjustHeight(e);
|
||||
|
||||
this.element.addClass(this.options.comboClass); |
||||
o.hideWhenClickOutside && BI.Widget._renderEngine.createElement(document).unbind("mousedown." + this.getName()); |
||||
BI.EVENT_BLUR && o.hideWhenBlur && BI.Widget._renderEngine.createElement(window).unbind("blur." + this.getName()); |
||||
|
||||
o.hideWhenClickOutside && BI.Widget._renderEngine.createElement(document).bind("mousedown." + this.getName(), BI.bind(this._hideIf, this)); |
||||
BI.EVENT_BLUR && o.hideWhenBlur && BI.Widget._renderEngine.createElement(window).bind("blur." + this.getName(), BI.bind(this._hideIf, this)); |
||||
this.fireEvent(BI.Bubble.EVENT_AFTER_POPUPVIEW); |
||||
}, |
||||
|
||||
adjustWidth: function (e) { |
||||
var o = this.options; |
||||
if (!this.popupView) { |
||||
return; |
||||
} |
||||
if (o.isNeedAdjustWidth === true) { |
||||
this.resetListWidth(""); |
||||
var width = this.popupView.element.outerWidth(); |
||||
var maxW = this.element.outerWidth() || o.width; |
||||
// BI-93885 最大列宽算法调整
|
||||
if (maxW < 500) { |
||||
if (width >= 500) { |
||||
maxW = 500; |
||||
} else if (width > maxW) { |
||||
// 防止小数导致差那么一点
|
||||
maxW = width + 1; |
||||
} |
||||
} |
||||
|
||||
// if (width > maxW + 80) {
|
||||
// maxW = maxW + 80;
|
||||
// } else if (width > maxW) {
|
||||
// maxW = width;
|
||||
// }
|
||||
this.resetListWidth(maxW < 100 ? 100 : maxW); |
||||
} |
||||
}, |
||||
|
||||
adjustHeight: function () { |
||||
|
||||
}, |
||||
|
||||
resetListHeight: function (h) { |
||||
this._assertPopupView(); |
||||
this.popupView.resetHeight && this.popupView.resetHeight(h); |
||||
}, |
||||
|
||||
resetListWidth: function (w) { |
||||
this._assertPopupView(); |
||||
this.popupView.resetWidth && this.popupView.resetWidth(w); |
||||
}, |
||||
|
||||
populate: function (items) { |
||||
this._assertPopupView(); |
||||
this.popupView.populate.apply(this.popupView, arguments); |
||||
this.combo.populate && this.combo.populate.apply(this.combo, arguments); |
||||
}, |
||||
|
||||
_setEnable: function (arg) { |
||||
BI.Bubble.superclass._setEnable.apply(this, arguments); |
||||
if (arg === true) { |
||||
this.element.removeClass("base-disabled disabled"); |
||||
} else if (arg === false) { |
||||
this.element.addClass("base-disabled disabled"); |
||||
} |
||||
!arg && this.element.removeClass(this.options.hoverClass); |
||||
!arg && this.isViewVisible() && this._hideView(); |
||||
}, |
||||
|
||||
setValue: function (v) { |
||||
this.combo.setValue(v); |
||||
if (BI.isNull(this.popupView)) { |
||||
this.options.popup.value = v; |
||||
} else { |
||||
this.popupView.setValue(v); |
||||
} |
||||
}, |
||||
|
||||
getValue: function () { |
||||
if (BI.isNull(this.popupView)) { |
||||
return this.options.popup.value; |
||||
} else { |
||||
return this.popupView.getValue(); |
||||
} |
||||
}, |
||||
|
||||
isViewVisible: function () { |
||||
return this.isEnabled() && this.combo.isEnabled() && !!this.popupView && this.popupView.isVisible(); |
||||
}, |
||||
|
||||
showView: function (e) { |
||||
// 减少popup 调整宽高的次数
|
||||
if (this.isEnabled() && this.combo.isEnabled() && !this.isViewVisible()) { |
||||
this._popupView(e); |
||||
} |
||||
}, |
||||
|
||||
hideView: function (e) { |
||||
this._hideView(e); |
||||
}, |
||||
|
||||
getView: function () { |
||||
return this.popupView; |
||||
}, |
||||
|
||||
getPopupPosition: function () { |
||||
return this.position; |
||||
}, |
||||
|
||||
toggle: function () { |
||||
this._toggle(); |
||||
}, |
||||
|
||||
destroyed: function () { |
||||
BI.Widget._renderEngine.createElement(document) |
||||
.unbind("click." + this.getName()) |
||||
.unbind("mousedown." + this.getName()) |
||||
.unbind("mouseenter." + this.getName()) |
||||
.unbind("mouseleave." + this.getName()); |
||||
BI.Widget._renderEngine.createElement(window) |
||||
.unbind("blur." + this.getName()); |
||||
this.popper && this.popper.destroy(); |
||||
this.popper = null; |
||||
this.popupView && this.popupView._destroy(); |
||||
} |
||||
}); |
||||
BI.Bubble.EVENT_TRIGGER_CHANGE = "EVENT_TRIGGER_CHANGE"; |
||||
BI.Bubble.EVENT_CHANGE = "EVENT_CHANGE"; |
||||
BI.Bubble.EVENT_EXPAND = "EVENT_EXPAND"; |
||||
BI.Bubble.EVENT_COLLAPSE = "EVENT_COLLAPSE"; |
||||
BI.Bubble.EVENT_AFTER_INIT = "EVENT_AFTER_INIT"; |
||||
|
||||
|
||||
BI.Bubble.EVENT_BEFORE_POPUPVIEW = "EVENT_BEFORE_POPUPVIEW"; |
||||
BI.Bubble.EVENT_AFTER_POPUPVIEW = "EVENT_AFTER_POPUPVIEW"; |
||||
BI.Bubble.EVENT_BEFORE_HIDEVIEW = "EVENT_BEFORE_HIDEVIEW"; |
||||
BI.Bubble.EVENT_AFTER_HIDEVIEW = "EVENT_AFTER_HIDEVIEW"; |
||||
|
||||
BI.shortcut("bi.bubble", BI.Bubble); |
||||
}()); |
@ -0,0 +1,627 @@
|
||||
(function () { |
||||
var Events = { |
||||
|
||||
// Bind an event to a `callback` function. Passing `"all"` will bind
|
||||
// the callback to all events fired.
|
||||
on: function (name, callback, context) { |
||||
if (!eventsApi(this, "on", name, [callback, context]) || !callback) return this; |
||||
this._events || (this._events = {}); |
||||
var events = this._events[name] || (this._events[name] = []); |
||||
events.push({callback: callback, context: context, ctx: context || this}); |
||||
return this; |
||||
}, |
||||
|
||||
// Bind an event to only be triggered a single time. After the first time
|
||||
// the callback is invoked, it will be removed.
|
||||
once: function (name, callback, context) { |
||||
if (!eventsApi(this, "once", name, [callback, context]) || !callback) return this; |
||||
var self = this; |
||||
var once = _.once(function () { |
||||
self.off(name, once); |
||||
callback.apply(this, arguments); |
||||
}); |
||||
once._callback = callback; |
||||
return this.on(name, once, context); |
||||
}, |
||||
|
||||
// Remove one or many callbacks. If `context` is null, removes all
|
||||
// callbacks with that function. If `callback` is null, removes all
|
||||
// callbacks for the event. If `name` is null, removes all bound
|
||||
// callbacks for all events.
|
||||
off: function (name, callback, context) { |
||||
if (!this._events || !eventsApi(this, "off", name, [callback, context])) return this; |
||||
|
||||
// Remove all callbacks for all events.
|
||||
if (!name && !callback && !context) { |
||||
this._events = void 0; |
||||
return this; |
||||
} |
||||
|
||||
var names = name ? [name] : _.keys(this._events); |
||||
for (var i = 0, length = names.length; i < length; i++) { |
||||
name = names[i]; |
||||
|
||||
// Bail out if there are no events stored.
|
||||
var events = this._events[name]; |
||||
if (!events) continue; |
||||
|
||||
// Remove all callbacks for this event.
|
||||
if (!callback && !context) { |
||||
delete this._events[name]; |
||||
continue; |
||||
} |
||||
|
||||
// Find any remaining events.
|
||||
var remaining = []; |
||||
for (var j = 0, k = events.length; j < k; j++) { |
||||
var event = events[j]; |
||||
if ( |
||||
callback && callback !== event.callback && |
||||
callback !== event.callback._callback || |
||||
context && context !== event.context |
||||
) { |
||||
remaining.push(event); |
||||
} |
||||
} |
||||
|
||||
// Replace events if there are any remaining. Otherwise, clean up.
|
||||
if (remaining.length) { |
||||
this._events[name] = remaining; |
||||
} else { |
||||
delete this._events[name]; |
||||
} |
||||
} |
||||
|
||||
return this; |
||||
}, |
||||
|
||||
un: function () { |
||||
this.off.apply(this, arguments); |
||||
}, |
||||
|
||||
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
||||
// passed the same arguments as `trigger` is, apart from the event name
|
||||
// (unless you're listening on `"all"`, which will cause your callback to
|
||||
// receive the true name of the event as the first argument).
|
||||
trigger: function (name) { |
||||
if (!this._events) return this; |
||||
var args = slice.call(arguments, 1); |
||||
if (!eventsApi(this, "trigger", name, args)) return this; |
||||
var events = this._events[name]; |
||||
var allEvents = this._events.all; |
||||
if (events) triggerEvents(events, args); |
||||
if (allEvents) triggerEvents(allEvents, arguments); |
||||
return this; |
||||
}, |
||||
|
||||
fireEvent: function () { |
||||
this.trigger.apply(this, arguments); |
||||
}, |
||||
|
||||
// Inversion-of-control versions of `on` and `once`. Tell *this* object to
|
||||
// listen to an event in another object ... keeping track of what it's
|
||||
// listening to.
|
||||
listenTo: function (obj, name, callback) { |
||||
var listeningTo = this._listeningTo || (this._listeningTo = {}); |
||||
var id = obj._listenId || (obj._listenId = _.uniqueId("l")); |
||||
listeningTo[id] = obj; |
||||
if (!callback && typeof name === "object") callback = this; |
||||
obj.on(name, callback, this); |
||||
return this; |
||||
}, |
||||
|
||||
listenToOnce: function (obj, name, callback) { |
||||
if (typeof name === "object") { |
||||
for (var event in name) this.listenToOnce(obj, event, name[event]); |
||||
return this; |
||||
} |
||||
if (eventSplitter.test(name)) { |
||||
var names = name.split(eventSplitter); |
||||
for (var i = 0, length = names.length; i < length; i++) { |
||||
this.listenToOnce(obj, names[i], callback); |
||||
} |
||||
return this; |
||||
} |
||||
if (!callback) return this; |
||||
var once = _.once(function () { |
||||
this.stopListening(obj, name, once); |
||||
callback.apply(this, arguments); |
||||
}); |
||||
once._callback = callback; |
||||
return this.listenTo(obj, name, once); |
||||
}, |
||||
|
||||
// Tell this object to stop listening to either specific events ... or
|
||||
// to every object it's currently listening to.
|
||||
stopListening: function (obj, name, callback) { |
||||
var listeningTo = this._listeningTo; |
||||
if (!listeningTo) return this; |
||||
var remove = !name && !callback; |
||||
if (!callback && typeof name === "object") callback = this; |
||||
if (obj) (listeningTo = {})[obj._listenId] = obj; |
||||
for (var id in listeningTo) { |
||||
obj = listeningTo[id]; |
||||
obj.off(name, callback, this); |
||||
if (remove || _.isEmpty(obj._events)) delete this._listeningTo[id]; |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
}; |
||||
|
||||
// Regular expression used to split event strings.
|
||||
var eventSplitter = /\s+/; |
||||
|
||||
// Implement fancy features of the Events API such as multiple event
|
||||
// names `"change blur"` and jQuery-style event maps `{change: action}`
|
||||
// in terms of the existing API.
|
||||
var eventsApi = function (obj, action, name, rest) { |
||||
if (!name) return true; |
||||
|
||||
// Handle event maps.
|
||||
if (typeof name === "object") { |
||||
for (var key in name) { |
||||
obj[action].apply(obj, [key, name[key]].concat(rest)); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
// Handle space separated event names.
|
||||
if (eventSplitter.test(name)) { |
||||
var names = name.split(eventSplitter); |
||||
for (var i = 0, length = names.length; i < length; i++) { |
||||
obj[action].apply(obj, [names[i]].concat(rest)); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
}; |
||||
|
||||
// A difficult-to-believe, but optimized internal dispatch function for
|
||||
// triggering events. Tries to keep the usual cases speedy (most internal
|
||||
// BI events have 3 arguments).
|
||||
var triggerEvents = function (events, args) { |
||||
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2]; |
||||
switch (args.length) { |
||||
case 0: |
||||
while (++i < l) (ev = events[i]).callback.call(ev.ctx); |
||||
return; |
||||
case 1: |
||||
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); |
||||
return; |
||||
case 2: |
||||
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); |
||||
return; |
||||
case 3: |
||||
while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); |
||||
return; |
||||
default: |
||||
while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); |
||||
return; |
||||
} |
||||
}; |
||||
|
||||
// BI.Router
|
||||
// ---------------
|
||||
|
||||
// Routers map faux-URLs to actions, and fire events when routes are
|
||||
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
||||
var Router = BI.Router = function (options) { |
||||
options || (options = {}); |
||||
if (options.routes) this.routes = options.routes; |
||||
this._bindRoutes(); |
||||
this._init.apply(this, arguments); |
||||
}; |
||||
|
||||
// Cached regular expressions for matching named param parts and splatted
|
||||
// parts of route strings.
|
||||
var optionalParam = /\((.*?)\)/g; |
||||
var namedParam = /(\(\?)?:\w+/g; |
||||
var splatParam = /\*\w+/g; |
||||
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g; |
||||
|
||||
// Set up all inheritable **BI.Router** properties and methods.
|
||||
_.extend(Router.prototype, Events, { |
||||
|
||||
// _init is an empty function by default. Override it with your own
|
||||
// initialization logic.
|
||||
_init: function () { |
||||
}, |
||||
|
||||
// Manually bind a single named route to a callback. For example:
|
||||
//
|
||||
// this.route('search/:query/p:num', 'search', function(query, num) {
|
||||
// ...
|
||||
// });
|
||||
//
|
||||
route: function (route, name, callback) { |
||||
if (!_.isRegExp(route)) route = this._routeToRegExp(route); |
||||
if (_.isFunction(name)) { |
||||
callback = name; |
||||
name = ""; |
||||
} |
||||
if (!callback) callback = this[name]; |
||||
var router = this; |
||||
BI.history.route(route, function (fragment) { |
||||
var args = router._extractParameters(route, fragment); |
||||
if (router.execute(callback, args, name) !== false) { |
||||
router.trigger.apply(router, ["route:" + name].concat(args)); |
||||
router.trigger("route", name, args); |
||||
BI.history.trigger("route", router, name, args); |
||||
} |
||||
}); |
||||
return this; |
||||
}, |
||||
|
||||
// Execute a route handler with the provided parameters. This is an
|
||||
// excellent place to do pre-route setup or post-route cleanup.
|
||||
execute: function (callback, args, name) { |
||||
if (callback) callback.apply(this, args); |
||||
}, |
||||
|
||||
// Simple proxy to `BI.history` to save a fragment into the history.
|
||||
navigate: function (fragment, options) { |
||||
BI.history.navigate(fragment, options); |
||||
return this; |
||||
}, |
||||
|
||||
// Bind all defined routes to `BI.history`. We have to reverse the
|
||||
// order of the routes here to support behavior where the most general
|
||||
// routes can be defined at the bottom of the route map.
|
||||
_bindRoutes: function () { |
||||
if (!this.routes) return; |
||||
this.routes = _.result(this, "routes"); |
||||
var route, routes = _.keys(this.routes); |
||||
while ((route = routes.pop()) != null) { |
||||
this.route(route, this.routes[route]); |
||||
} |
||||
}, |
||||
|
||||
// Convert a route string into a regular expression, suitable for matching
|
||||
// against the current location hash.
|
||||
_routeToRegExp: function (route) { |
||||
route = route.replace(escapeRegExp, "\\$&") |
||||
.replace(optionalParam, "(?:$1)?") |
||||
.replace(namedParam, function (match, optional) { |
||||
return optional ? match : "([^/?]+)"; |
||||
}) |
||||
.replace(splatParam, "([^?]*?)"); |
||||
return new RegExp("^" + route + "(?:\\?([\\s\\S]*))?$"); |
||||
}, |
||||
|
||||
// Given a route, and a URL fragment that it matches, return the array of
|
||||
// extracted decoded parameters. Empty or unmatched parameters will be
|
||||
// treated as `null` to normalize cross-browser behavior.
|
||||
_extractParameters: function (route, fragment) { |
||||
var params = route.exec(fragment).slice(1); |
||||
return _.map(params, function (param, i) { |
||||
// Don't decode the search params.
|
||||
if (i === params.length - 1) return param || null; |
||||
var resultParam = null; |
||||
if (param) { |
||||
try { |
||||
resultParam = decodeURIComponent(param); |
||||
} catch (e) { |
||||
resultParam = param; |
||||
} |
||||
} |
||||
return resultParam; |
||||
}); |
||||
} |
||||
|
||||
}); |
||||
|
||||
// History
|
||||
// ----------------
|
||||
|
||||
// Handles cross-browser history management, based on either
|
||||
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
|
||||
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
|
||||
// and URL fragments. If the browser supports neither (old IE, natch),
|
||||
// falls back to polling.
|
||||
var History = function () { |
||||
this.handlers = []; |
||||
this.checkUrl = _.bind(this.checkUrl, this); |
||||
|
||||
// Ensure that `History` can be used outside of the browser.
|
||||
if (typeof window !== "undefined") { |
||||
this.location = _global.location; |
||||
this.history = _global.history; |
||||
} |
||||
}; |
||||
|
||||
// Cached regex for stripping a leading hash/slash and trailing space.
|
||||
var routeStripper = /^[#\/]|\s+$/g; |
||||
|
||||
// Cached regex for stripping leading and trailing slashes.
|
||||
var rootStripper = /^\/+|\/+$/g; |
||||
|
||||
// Cached regex for stripping urls of hash.
|
||||
var pathStripper = /#.*$/; |
||||
|
||||
// Has the history handling already been started?
|
||||
History.started = false; |
||||
|
||||
// Set up all inheritable **BI.History** properties and methods.
|
||||
_.extend(History.prototype, Events, { |
||||
|
||||
// The default interval to poll for hash changes, if necessary, is
|
||||
// twenty times a second.
|
||||
interval: 50, |
||||
|
||||
// Are we at the app root?
|
||||
atRoot: function () { |
||||
var path = this.location.pathname.replace(/[^\/]$/, "$&/"); |
||||
return path === this.root && !this.getSearch(); |
||||
}, |
||||
|
||||
// In IE6, the hash fragment and search params are incorrect if the
|
||||
// fragment contains `?`.
|
||||
getSearch: function () { |
||||
var match = this.location.href.replace(/#.*/, "").match(/\?.+/); |
||||
return match ? match[0] : ""; |
||||
}, |
||||
|
||||
// Gets the true hash value. Cannot use location.hash directly due to bug
|
||||
// in Firefox where location.hash will always be decoded.
|
||||
getHash: function (window) { |
||||
var match = (window || this).location.href.match(/#(.*)$/); |
||||
return match ? match[1] : ""; |
||||
}, |
||||
|
||||
// Get the pathname and search params, without the root.
|
||||
getPath: function () { |
||||
var path = this.location.pathname + this.getSearch(); |
||||
try { |
||||
path = decodeURI(path); |
||||
} catch(e) { |
||||
} |
||||
var root = this.root.slice(0, -1); |
||||
if (!path.indexOf(root)) path = path.slice(root.length); |
||||
return path.charAt(0) === "/" ? path.slice(1) : path; |
||||
}, |
||||
|
||||
// Get the cross-browser normalized URL fragment from the path or hash.
|
||||
getFragment: function (fragment) { |
||||
if (fragment == null) { |
||||
if (this._hasPushState || !this._wantsHashChange) { |
||||
fragment = this.getPath(); |
||||
} else { |
||||
fragment = this.getHash(); |
||||
} |
||||
} |
||||
return fragment.replace(routeStripper, ""); |
||||
}, |
||||
|
||||
// Start the hash change handling, returning `true` if the current URL matches
|
||||
// an existing route, and `false` otherwise.
|
||||
start: function (options) { |
||||
if (History.started) throw new Error("BI.history has already been started"); |
||||
History.started = true; |
||||
|
||||
// Figure out the initial configuration. Do we need an iframe?
|
||||
// Is pushState desired ... is it available?
|
||||
this.options = _.extend({root: "/"}, this.options, options); |
||||
this.root = this.options.root; |
||||
this._wantsHashChange = this.options.hashChange !== false; |
||||
this._hasHashChange = "onhashchange" in window; |
||||
this._wantsPushState = !!this.options.pushState; |
||||
this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState); |
||||
this.fragment = this.getFragment(); |
||||
|
||||
// Normalize root to always include a leading and trailing slash.
|
||||
this.root = ("/" + this.root + "/").replace(rootStripper, "/"); |
||||
|
||||
// Transition from hashChange to pushState or vice versa if both are
|
||||
// requested.
|
||||
if (this._wantsHashChange && this._wantsPushState) { |
||||
|
||||
// If we've started off with a route from a `pushState`-enabled
|
||||
// browser, but we're currently in a browser that doesn't support it...
|
||||
if (!this._hasPushState && !this.atRoot()) { |
||||
var root = this.root.slice(0, -1) || "/"; |
||||
this.location.replace(root + "#" + this.getPath()); |
||||
// Return immediately as browser will do redirect to new url
|
||||
return true; |
||||
|
||||
// Or if we've started out with a hash-based route, but we're currently
|
||||
// in a browser where it could be `pushState`-based instead...
|
||||
} else if (this._hasPushState && this.atRoot()) { |
||||
this.navigate(this.getHash(), {replace: true}); |
||||
} |
||||
|
||||
} |
||||
|
||||
// Proxy an iframe to handle location events if the browser doesn't
|
||||
// support the `hashchange` event, HTML5 history, or the user wants
|
||||
// `hashChange` but not `pushState`.
|
||||
if (!this._hasHashChange && this._wantsHashChange && (!this._wantsPushState || !this._hasPushState)) { |
||||
var iframe = document.createElement("iframe"); |
||||
iframe.src = "javascript:0"; |
||||
iframe.style.display = "none"; |
||||
iframe.tabIndex = -1; |
||||
var body = document.body; |
||||
// Using `appendChild` will throw on IE < 9 if the document is not ready.
|
||||
this.iframe = body.insertBefore(iframe, body.firstChild).contentWindow; |
||||
this.iframe.document.open().close(); |
||||
this.iframe.location.hash = "#" + this.fragment; |
||||
} |
||||
|
||||
// Add a cross-platform `addEventListener` shim for older browsers.
|
||||
var addEventListener = _global.addEventListener || function (eventName, listener) { |
||||
return attachEvent("on" + eventName, listener); |
||||
}; |
||||
|
||||
// Depending on whether we're using pushState or hashes, and whether
|
||||
// 'onhashchange' is supported, determine how we check the URL state.
|
||||
if (this._hasPushState) { |
||||
addEventListener("popstate", this.checkUrl, false); |
||||
} else if (this._wantsHashChange && this._hasHashChange && !this.iframe) { |
||||
addEventListener("hashchange", this.checkUrl, false); |
||||
} else if (this._wantsHashChange) { |
||||
this._checkUrlInterval = setInterval(this.checkUrl, this.interval); |
||||
} |
||||
|
||||
if (!this.options.silent) return this.loadUrl(); |
||||
}, |
||||
|
||||
// Disable BI.history, perhaps temporarily. Not useful in a real app,
|
||||
// but possibly useful for unit testing Routers.
|
||||
stop: function () { |
||||
// Add a cross-platform `removeEventListener` shim for older browsers.
|
||||
var removeEventListener = _global.removeEventListener || function (eventName, listener) { |
||||
return detachEvent("on" + eventName, listener); |
||||
}; |
||||
|
||||
// Remove window listeners.
|
||||
if (this._hasPushState) { |
||||
removeEventListener("popstate", this.checkUrl, false); |
||||
} else if (this._wantsHashChange && this._hasHashChange && !this.iframe) { |
||||
removeEventListener("hashchange", this.checkUrl, false); |
||||
} |
||||
|
||||
// Clean up the iframe if necessary.
|
||||
if (this.iframe) { |
||||
document.body.removeChild(this.iframe.frameElement); |
||||
this.iframe = null; |
||||
} |
||||
|
||||
// Some environments will throw when clearing an undefined interval.
|
||||
if (this._checkUrlInterval) clearInterval(this._checkUrlInterval); |
||||
History.started = false; |
||||
}, |
||||
|
||||
// Add a route to be tested when the fragment changes. Routes added later
|
||||
// may override previous routes.
|
||||
route: function (route, callback) { |
||||
this.handlers.unshift({route: route, callback: callback}); |
||||
}, |
||||
|
||||
// check route is Exist. if exist, return the route
|
||||
checkRoute: function (route) { |
||||
for (var i = 0; i < this.handlers.length; i++) { |
||||
if (this.handlers[i].route.toString() === Router.prototype._routeToRegExp(route).toString()) { |
||||
return this.handlers[i]; |
||||
} |
||||
} |
||||
|
||||
return null; |
||||
}, |
||||
|
||||
// remove a route match in routes
|
||||
unRoute: function (route) { |
||||
var index = _.findIndex(this.handlers, function (handler) { |
||||
return handler.route.test(route); |
||||
}); |
||||
if (index > -1) { |
||||
this.handlers.splice(index, 1); |
||||
} |
||||
}, |
||||
|
||||
// Checks the current URL to see if it has changed, and if it has,
|
||||
// calls `loadUrl`, normalizing across the hidden iframe.
|
||||
checkUrl: function (e) { |
||||
var current = this.getFragment(); |
||||
try { |
||||
// getFragment 得到的值是编码过的,而this.fragment是没有编码过的
|
||||
// 英文路径没有问题,遇上中文和空格有问题了
|
||||
current = decodeURIComponent(current); |
||||
} catch(e) { |
||||
} |
||||
// If the user pressed the back button, the iframe's hash will have
|
||||
// changed and we should use that for comparison.
|
||||
if (current === this.fragment && this.iframe) { |
||||
current = this.getHash(this.iframe); |
||||
} |
||||
|
||||
if (current === this.fragment) return false; |
||||
if (this.iframe) this.navigate(current); |
||||
this.loadUrl(); |
||||
}, |
||||
|
||||
// Attempt to load the current URL fragment. If a route succeeds with a
|
||||
// match, returns `true`. If no defined routes matches the fragment,
|
||||
// returns `false`.
|
||||
loadUrl: function (fragment) { |
||||
fragment = this.fragment = this.getFragment(fragment); |
||||
return _.some(this.handlers, function (handler) { |
||||
if (handler.route.test(fragment)) { |
||||
handler.callback(fragment); |
||||
return true; |
||||
} |
||||
}); |
||||
}, |
||||
|
||||
// Save a fragment into the hash history, or replace the URL state if the
|
||||
// 'replace' option is passed. You are responsible for properly URL-encoding
|
||||
// the fragment in advance.
|
||||
//
|
||||
// The options object can contain `trigger: true` if you wish to have the
|
||||
// route callback be fired (not usually desirable), or `replace: true`, if
|
||||
// you wish to modify the current URL without adding an entry to the history.
|
||||
navigate: function (fragment, options) { |
||||
if (!History.started) return false; |
||||
if (!options || options === true) options = {trigger: !!options}; |
||||
|
||||
// Normalize the fragment.
|
||||
fragment = this.getFragment(fragment || ""); |
||||
|
||||
// Don't include a trailing slash on the root.
|
||||
var root = this.root; |
||||
if (fragment === "" || fragment.charAt(0) === "?") { |
||||
root = root.slice(0, -1) || "/"; |
||||
} |
||||
var url = root + fragment; |
||||
|
||||
// Strip the hash and decode for matching.
|
||||
fragment = fragment.replace(pathStripper, "") |
||||
try { |
||||
fragment = decodeURI(fragment); |
||||
} catch(e) { |
||||
} |
||||
|
||||
if (this.fragment === fragment) return; |
||||
this.fragment = fragment; |
||||
|
||||
// If pushState is available, we use it to set the fragment as a real URL.
|
||||
if (this._hasPushState) { |
||||
this.history[options.replace ? "replaceState" : "pushState"]({}, document.title, url); |
||||
|
||||
// If hash changes haven't been explicitly disabled, update the hash
|
||||
// fragment to store history.
|
||||
} else if (this._wantsHashChange) { |
||||
this._updateHash(this.location, fragment, options.replace); |
||||
if (this.iframe && (fragment !== this.getHash(this.iframe))) { |
||||
// Opening and closing the iframe tricks IE7 and earlier to push a
|
||||
// history entry on hash-tag change. When replace is true, we don't
|
||||
// want this.
|
||||
if (!options.replace) this.iframe.document.open().close(); |
||||
this._updateHash(this.iframe.location, fragment, options.replace); |
||||
} |
||||
|
||||
// If you've told us that you explicitly don't want fallback hashchange-
|
||||
// based history, then `navigate` becomes a page refresh.
|
||||
} else { |
||||
return this.location.assign(url); |
||||
} |
||||
if (options.trigger) return this.loadUrl(fragment); |
||||
}, |
||||
|
||||
// Update the hash location, either replacing the current entry, or adding
|
||||
// a new one to the browser history.
|
||||
_updateHash: function (location, fragment, replace) { |
||||
if (replace) { |
||||
var href = location.href.replace(/(javascript:|#).*$/, ""); |
||||
location.replace(href + "#" + fragment); |
||||
} else { |
||||
// Some browsers require that `hash` contains a leading #.
|
||||
location.hash = "#" + fragment; |
||||
} |
||||
} |
||||
|
||||
}); |
||||
|
||||
// Create the default BI.history.
|
||||
BI.history = new History; |
||||
}()); |
Loading…
Reference in new issue