fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
12 KiB

8 years ago
/**
* Created by GUY on 2015/4/15.
* @class BI.Editor
* @extends BI.Single
*/
BI.Editor = BI.inherit(BI.Single, {
_defaultConfig: function () {
var conf = BI.Editor.superclass._defaultConfig.apply(this, arguments);
8 years ago
return BI.extend(conf, {
baseCls: "bi-editor bi-focus-shadow",
8 years ago
hgap: 4,
vgap: 2,
lgap: 0,
rgap: 0,
tgap: 0,
bgap: 0,
7 years ago
// title,warningTitle这两个属性没用
8 years ago
tipType: "warning",
inputType: "text",
validationChecker: BI.emptyFn,
quitChecker: BI.emptyFn,
allowBlank: false,
watermark: "",
errorText: "",
7 years ago
});
8 years ago
},
_init: function () {
BI.Editor.superclass._init.apply(this, arguments);
var self = this, o = this.options;
// 密码输入框设置autocomplete="new-password"的情况下Firefox和chrome不会自动填充密码
var autocomplete = o.autocomplete ? " autocomplete=" + o.autocomplete : "";
8 years ago
this.editor = this.addWidget(BI.createWidget({
type: "bi.input",
element: "<input type='" + o.inputType + "'" + autocomplete + " />",
8 years ago
root: true,
value: o.value,
8 years ago
watermark: o.watermark,
validationChecker: o.validationChecker,
quitChecker: o.quitChecker,
allowBlank: o.allowBlank
}));
this.editor.element.css({
7 years ago
width: "100%",
height: "100%",
border: "none",
outline: "none",
padding: "0",
margin: "0"
8 years ago
});
8 years ago
var items = [{
el: {
type: "bi.absolute",
ref: function(_ref) {
self.contentWrapper = _ref;
},
items: [{
el: this.editor,
left: 0,
right: 0,
top: 0,
bottom: 0
}]
8 years ago
},
left: o.hgap + o.lgap,
right: o.hgap + o.rgap,
top: o.vgap + o.tgap,
bottom: o.vgap + o.bgap
}];
BI.createWidget({
type: "bi.absolute",
8 years ago
element: this,
8 years ago
items: items
});
this.setWaterMark(this.options.watermark);
8 years ago
this.editor.on(BI.Controller.EVENT_CHANGE, function () {
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments);
});
this.editor.on(BI.Input.EVENT_FOCUS, function () {
self._checkError();
self.element.addClass("bi-editor-focus");
self.fireEvent(BI.Editor.EVENT_FOCUS, arguments);
});
this.editor.on(BI.Input.EVENT_BLUR, function () {
7 years ago
self._setErrorVisible(false);
8 years ago
self.element.removeClass("bi-editor-focus");
self.fireEvent(BI.Editor.EVENT_BLUR, arguments);
});
this.editor.on(BI.Input.EVENT_CLICK, function () {
self.fireEvent(BI.Editor.EVENT_CLICK, arguments);
});
this.editor.on(BI.Input.EVENT_CHANGE, function () {
self.fireEvent(BI.Editor.EVENT_CHANGE, arguments);
});
this.editor.on(BI.Input.EVENT_KEY_DOWN, function (v) {
self.fireEvent(BI.Editor.EVENT_KEY_DOWN, arguments);
});
this.editor.on(BI.Input.EVENT_QUICK_DOWN, function (e) {
// tab键就不要隐藏了
6 years ago
if (e.keyCode !== BI.KeyCode.TAB && self.watermark) {
self.watermark.invisible();
}
8 years ago
});
this.editor.on(BI.Input.EVENT_VALID, function () {
self._checkWaterMark();
7 years ago
self._setErrorVisible(false);
8 years ago
self.fireEvent(BI.Editor.EVENT_VALID, arguments);
});
this.editor.on(BI.Input.EVENT_ERROR, function () {
self._checkWaterMark();
self.fireEvent(BI.Editor.EVENT_ERROR, arguments);
7 years ago
self._setErrorVisible(self.isEditing());
8 years ago
});
this.editor.on(BI.Input.EVENT_RESTRICT, function () {
self._checkWaterMark();
7 years ago
var tip = self._setErrorVisible(true);
8 years ago
tip && tip.element.fadeOut(100, function () {
tip.element.fadeIn(100);
});
self.fireEvent(BI.Editor.EVENT_RESTRICT, arguments);
});
this.editor.on(BI.Input.EVENT_EMPTY, function () {
self._checkWaterMark();
self.fireEvent(BI.Editor.EVENT_EMPTY, arguments);
});
this.editor.on(BI.Input.EVENT_ENTER, function () {
self.fireEvent(BI.Editor.EVENT_ENTER, arguments);
});
this.editor.on(BI.Input.EVENT_SPACE, function () {
self.fireEvent(BI.Editor.EVENT_SPACE, arguments);
});
this.editor.on(BI.Input.EVENT_BACKSPACE, function () {
self.fireEvent(BI.Editor.EVENT_BACKSPACE, arguments);
});
this.editor.on(BI.Input.EVENT_REMOVE, function () {
self.fireEvent(BI.Editor.EVENT_REMOVE, arguments);
});
this.editor.on(BI.Input.EVENT_START, function () {
self.fireEvent(BI.Editor.EVENT_START, arguments);
});
this.editor.on(BI.Input.EVENT_PAUSE, function () {
self.fireEvent(BI.Editor.EVENT_PAUSE, arguments);
});
this.editor.on(BI.Input.EVENT_STOP, function () {
self.fireEvent(BI.Editor.EVENT_STOP, arguments);
});
this.editor.on(BI.Input.EVENT_CONFIRM, function () {
self.fireEvent(BI.Editor.EVENT_CONFIRM, arguments);
});
this.editor.on(BI.Input.EVENT_CHANGE_CONFIRM, function () {
self.fireEvent(BI.Editor.EVENT_CHANGE_CONFIRM, arguments);
});
8 years ago
this.element.click(function (e) {
e.stopPropagation();
return false;
});
if (BI.isKey(this.options.value) || BI.isEmptyString(this.options.value)) {
this._checkError();
this._checkWaterMark();
8 years ago
} else {
this._checkWaterMark();
}
},
_checkToolTip: function () {
var o = this.options;
var errorText = o.errorText;
if (BI.isFunction(errorText)) {
errorText = errorText(this.editor.getValue());
}
if (BI.isKey(errorText)) {
if (!this.isEnabled() || this.isValid() || (BI.Bubbles.has(this.getName()) && BI.Bubbles.get(this.getName()).isVisible())) {
this.setTitle("");
} else {
this.setTitle(errorText);
}
}
},
_assertWaterMark: function() {
var self = this, o = this.options;
if(BI.isNull(this.watermark)) {
this.watermark = BI.createWidget({
type: "bi.label",
cls: "bi-water-mark",
text: this.options.watermark,
height: o.height - 2 * o.vgap - o.tgap,
whiteSpace: "nowrap",
textAlign: "left"
});
this.watermark.element.bind({
mousedown: function (e) {
if (self.isEnabled()) {
self.editor.isEditing() || self.editor.focus();
} else {
self.editor.isEditing() && self.editor.blur();
}
e.stopEvent();
}
});
this.watermark.element.bind("click", function (e) {
if (self.isEnabled()) {
self.editor.isEditing() || self.editor.focus();
} else {
self.editor.isEditing() && self.editor.blur();
}
e.stopEvent();
});
}
},
8 years ago
_checkError: function () {
7 years ago
this._setErrorVisible(this.isEnabled() && !this.isValid());
8 years ago
this._checkToolTip();
},
_checkWaterMark: function () {
var o = this.options;
7 years ago
if (!this.disabledWaterMark && this.editor.getValue() === "" && BI.isKey(o.watermark)) {
8 years ago
this.watermark && this.watermark.visible();
} else {
this.watermark && this.watermark.invisible();
}
},
setErrorText: function (text) {
this.options.errorText = text;
},
getErrorText: function () {
return this.options.errorText;
},
setWaterMark: function(v) {
if (!BI.isKey(v)) {
return;
}
this.options.watermark = v;
if (BI.isNull(this.watermark)) {
this._assertWaterMark();
BI.createWidget({
type: "bi.absolute",
element: this.contentWrapper,
items: [{
el: this.watermark,
left: 3,
right: 3,
top: 0,
bottom: 0,
}],
});
}
this.watermark.setText(v);
},
7 years ago
_setErrorVisible: function (b) {
8 years ago
var o = this.options;
var errorText = o.errorText;
if (BI.isFunction(errorText)) {
errorText = errorText(BI.trim(this.editor.getValue()));
8 years ago
}
if (!this.disabledError && BI.isKey(errorText)) {
BI.Bubbles[b ? "show" : "hide"](this.getName(), errorText, this, {
adjustYOffset: 2
});
8 years ago
this._checkToolTip();
return BI.Bubbles.get(this.getName());
}
},
disableError: function () {
this.disabledError = true;
this._checkError();
},
enableError: function () {
this.disabledError = false;
this._checkError();
},
7 years ago
disableWaterMark: function () {
this.disabledWaterMark = true;
8 years ago
this._checkWaterMark();
},
7 years ago
enableWaterMark: function () {
this.disabledWaterMark = false;
8 years ago
this._checkWaterMark();
},
focus: function () {
this.element.addClass("text-editor-focus");
this.editor.focus();
},
blur: function () {
this.element.removeClass("text-editor-focus");
this.editor.blur();
},
selectAll: function () {
this.editor.selectAll();
},
onKeyDown: function (k) {
this.editor.onKeyDown(k);
},
setValue: function (v) {
BI.Editor.superclass.setValue.apply(this, arguments);
this.editor.setValue(v);
this._checkError();
this._checkWaterMark();
},
getLastValidValue: function () {
return this.editor.getLastValidValue();
8 years ago
},
getLastChangedValue: function () {
return this.editor.getLastChangedValue();
},
8 years ago
getValue: function () {
if (!this.isValid()) {
return BI.trim(this.editor.getLastValidValue());
8 years ago
}
return BI.trim(this.editor.getValue());
8 years ago
},
isEditing: function () {
return this.editor.isEditing();
},
isValid: function () {
return this.editor.isValid();
},
destroyed: function () {
BI.Bubbles.remove(this.getName());
8 years ago
}
});
BI.Editor.EVENT_CHANGE = "EVENT_CHANGE";
BI.Editor.EVENT_FOCUS = "EVENT_FOCUS";
BI.Editor.EVENT_BLUR = "EVENT_BLUR";
BI.Editor.EVENT_CLICK = "EVENT_CLICK";
BI.Editor.EVENT_KEY_DOWN = "EVENT_KEY_DOWN";
BI.Editor.EVENT_SPACE = "EVENT_SPACE";
BI.Editor.EVENT_BACKSPACE = "EVENT_BACKSPACE";
BI.Editor.EVENT_START = "EVENT_START";
BI.Editor.EVENT_PAUSE = "EVENT_PAUSE";
BI.Editor.EVENT_STOP = "EVENT_STOP";
BI.Editor.EVENT_CONFIRM = "EVENT_CONFIRM";
BI.Editor.EVENT_CHANGE_CONFIRM = "EVENT_CHANGE_CONFIRM";
8 years ago
BI.Editor.EVENT_VALID = "EVENT_VALID";
BI.Editor.EVENT_ERROR = "EVENT_ERROR";
BI.Editor.EVENT_ENTER = "EVENT_ENTER";
BI.Editor.EVENT_RESTRICT = "EVENT_RESTRICT";
BI.Editor.EVENT_REMOVE = "EVENT_REMOVE";
BI.Editor.EVENT_EMPTY = "EVENT_EMPTY";
8 years ago
BI.shortcut("bi.editor", BI.Editor);