快速搭建fineui开发环境。
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.

482 lines
18 KiB

6 years ago
!(function() {
/**
* 顶部组件,提供输入框添加todo项目
* 布局: bi.horizontal_auto 实现水平居中. bi.left_right_vertical_adapt 实现标题是输入框的靠左靠右垂直居中
*/
6 years ago
var ToDoListHeader = BI.inherit(BI.Widget, {
props: {
6 years ago
// 指定组件的className
6 years ago
baseCls: "my-todolist-header"
},
6 years ago
render: function() {
6 years ago
var self = this, o = this.options;
return {
6 years ago
type: "bi.horizontal_auto", // 水平居中布局
6 years ago
items: [
{
el: {
6 years ago
type: "bi.left_right_vertical_adapt", // 左右垂直居中布局
6 years ago
width: 600,
height: o.height,
items: {
left: [
{
el: {
type: "bi.label",
cls: "my-todolist-title",
text: "FineUI ToDoList",
height: o.height
}
}
],
right: [
{
el: {
type: "bi.editor",
6 years ago
ref: function(_ref) {
self.editor = _ref;
},
6 years ago
allowBlank: true,
cls: "my-todolist-header-editor",
watermark: "添加ToDo",
width: 300,
height: 24,
listeners: [
6 years ago
{ // 监听bi.editor 组件的"EVENT_ENTER"事件(即敲回车),触发事件ToDoListHeader.EVENT_ADD事件并将输入框值置空
6 years ago
eventName: "EVENT_ENTER",
6 years ago
action: function() {
// 注意:在这里this指向的是bi.editor的实例.通过bi.editor的getValue()方法获取输入框输入值.
6 years ago
self.fireEvent(ToDoListHeader.EVENT_ADD, this.getValue());
6 years ago
self.editor.setValue("");
6 years ago
}
}
]
}
}
]
}
}
}
]
};
}
});
ToDoListHeader.EVENT_ADD = "EVENT_ADD";
BI.shortcut("my.todolist.header", ToDoListHeader);
6 years ago
})();!(function() {
/**
* todo项列表
*
*/
6 years ago
var List = BI.inherit(BI.Widget, {
props: {
6 years ago
// 指定组件的className
6 years ago
baseCls: "my-todolist-list",
text: "正在进行"
},
6 years ago
render: function() {
6 years ago
var self = this, o = this.options;
return {
type: "bi.vertical",
items: [
{
el: {
type: "bi.vertical_adapt",
height: 40,
items: [
{
type: "bi.label",
cls: "my-todolist-list-text",
textAlign: "left",
text: o.text,
width: 580
}, {
type: "bi.center_adapt",
cls: "my-todolist-list-count-container",
width: 20,
height: 20,
items: [
{
el: {
type: "bi.label",
6 years ago
ref: function(_ref) {
6 years ago
self.count = _ref;
},
text: "0"
}
}
]
}
]
}
6 years ago
}, { // 用bi.vertical布局作为列表项的容器.
type: "bi.vertical",
vgap: 10,
ref: function(_ref) {
6 years ago
self.list = _ref;
},
6 years ago
items: this._createItems(o.items)
6 years ago
}
]
};
},
6 years ago
_createItems: function(items) {
var self = this;
return BI.map(items, function(index, item) {
return BI.extend(item, {
type: "bi.multi_select_item", // 节点采用复选节点展示
selected: item.done, // 已完成的todo项置为选中状态
disabled: item.done, // 已完成的todo项置为灰化状态
listeners: [
{ // 为每个todo项添加"EVENT_CHANGE"事件监听,触发组件自身"EVENT_CHANGE"事件
eventName: "EVENT_CHANGE",
action: function(v) {
self.fireEvent("EVENT_CHANGE", v);
}
}
]
});
});
6 years ago
},
6 years ago
_setCount: function(count) {
6 years ago
this.count.setText(count);
},
6 years ago
populate: function(items) {
6 years ago
this.list.populate(this._createItems(items));
this._setCount(items.length);
}
});
BI.shortcut("my.todolist.list", List);
6 years ago
})();!(function() {
/**
* todolist 组件
*/
6 years ago
var ToDoList = BI.inherit(BI.Widget, {
props: {
baseCls: "fine-to-do-list"
},
6 years ago
// 生命周期函数,在组件创建前
beforeCreate: function() {
// 初始化存储数据
this.list = localStorage.getItem("fine-todolist") ? JSON.parse(localStorage.getItem("fine-todolist")) : [];
6 years ago
},
6 years ago
render: function() {
6 years ago
var self = this, o = this.options;
return {
6 years ago
type: "bi.vtape", // vtape布局,顶部高度固定,下部分列表占满高度
6 years ago
items: [
{
el: {
6 years ago
type: "my.todolist.header", // 顶部组件
6 years ago
listeners: [
6 years ago
{ // 监听组件的EVENT_ADD事件,新建todo项
6 years ago
eventName: "EVENT_ADD",
6 years ago
action: function(v) {
self.addToDo(v);
6 years ago
}
}
],
height: 40
},
height: 40
}, {
6 years ago
type: "bi.horizontal_auto", // 水平居中布局
cls: "my-todolist-background", // 添加className
6 years ago
items: [
{
el: {
6 years ago
type: "my.todolist.list", // need todo项列表
ref: function(_ref) {
6 years ago
self.todolist = _ref;
},
6 years ago
items: this._getNeedTodoList(),
6 years ago
text: "正在进行",
listeners: [
6 years ago
{ // 监听EVENT_CHANGE事件,完成某一项todo
6 years ago
eventName: "EVENT_CHANGE",
6 years ago
action: function(v) {
self.finishTodo(v);
6 years ago
}
}
],
width: 600
}
}, {
el: {
6 years ago
type: "my.todolist.list", // 已经完成的todo项列表
6 years ago
text: "已经完成",
6 years ago
items: this._getAlreadyDoneList(),
ref: function(_ref) {
6 years ago
self.donelist = _ref;
},
width: 600
}
}
]
}
]
};
6 years ago
},
6 years ago
6 years ago
_updateLocalStorage: function() {
localStorage.setItem("fine-todolist", JSON.stringify(this.list));
},
6 years ago
6 years ago
_getNeedTodoList: function() {
return BI.filter(this.list, function(index, item) {
return !item.done;
});
6 years ago
},
6 years ago
_getAlreadyDoneList: function() {
return BI.filter(this.list, function(index, item) {
return item.done;
});
6 years ago
},
6 years ago
/**
* 添加todo项
* @param text todo项的内容
*/
addToDo: function(text) {
this.list.push({
value: BI.UUID(),
text: text,
done: false
});
this.todolist.populate(this._getNeedTodoList());
this._updateLocalStorage();
},
6 years ago
6 years ago
/**
* 完成某一项todo
* @param v todo项的value
*/
finishTodo: function(v) {
BI.some(this.list, function(index, item) {
if (item.value === v) {
item.done = true;
}
});
this.todolist.populate(this._getNeedTodoList());
this.donelist.populate(this._getAlreadyDoneList());
this._updateLocalStorage();
6 years ago
}
});
6 years ago
BI.shortcut("my.todolist", ToDoList);
6 years ago
})();
!(function () {
/**
* 多层级单选树增加无效值时状态使用BI.config覆盖原组件只能调用一次
* 新增defaultText参数表示空默认值不传默认值为请选择注意不能替代text只是为了跟text区分开text现在包含空默认值和暂不可用值两种情况
* value存在且不存在于items中为无效值文本框标红
* value不存在text不为defaultText时为暂不可用值文本框不变去除水印
*/
BI.config("bi.multilayer_single_tree_combo", function (config) {
var ERROR_CLASS = "combo-error";
var WATERMARK_CLASS = "bi-water-mark";
var DEFAULT_TEXT = "请选择";
/**
* 判断是否是有效值
* @param {Object} config
* @param {String} value setValue传入的值
* @returns {Boolean}
*/
function isValueValid (options, value) {
if (!BI.isObject(options)) {
return true;
}
value = value || options.value;
// null/undefined/空字符串作为空值
if (BI.isNull(value) || (value === "")) {
return true;
}
var items = options.items || [];
var itemIds = [];
// 获取id列表
BI.each(items, function (i, item) {
item && item.value && itemIds.push(item.value);
});
return BI.contains(itemIds, value);
}
/**
* 增加/去除标红className
* @param {Object} widget
* @param {String} value setValue传入的值
*/
function changeInvalidClassName (widget, value) {
if (!BI.isObject(widget)) {
return;
}
var combo = widget.combo;
var trigger = widget.textTrigger && widget.textTrigger.trigger && widget.textTrigger.trigger.trigger && widget.textTrigger.trigger.trigger.text && widget.textTrigger.trigger.trigger.text.element;
var options = widget.options;
var defaultText = options.defaultText || DEFAULT_TEXT;
if (isValueValid(options, value)) {
combo && combo.element.removeClass(ERROR_CLASS);
} else {
combo && combo.element.addClass(ERROR_CLASS);
}
if (options.text !== defaultText) {
trigger.removeClass(WATERMARK_CLASS);
}
}
config.listeners = config.listeners || [];
// 新增mount生命周期回调
config.listeners.push({
eventName: BI.Events.MOUNT,
action: function () {
// 覆盖原setValue方法,调用原setValue前,修改样式
var _setValue = this.setValue;
this.setValue = function (value) {
_setValue.call(this, value);
changeInvalidClassName(this, value && value[0]);
};
// 立即触发一次样式变更,不需要传value
changeInvalidClassName(this);
}
});
return config;
});
var treeItems = [
{id: -1, pId: -2, value: "根目录", text: "根目录"},
{id: 1, pId: -1, value: "第一级目录1", text: "第一级目录1"},
{id: 11, pId: 1, value: "第二级文件1", text: "第二级文件1"},
{id: 12, pId: 1, value: "第二级目录2", text: "第二级目录2"},
{id: 121, pId: 12, value: "第三级目录1", text: "第三级目录1"},
{id: 122, pId: 12, value: "第三级文件1", text: "第三级文件1"},
{id: 1211, pId: 121, value: "第四级目录1", text: "第四级目录1"},
{id: 12111, pId: 1211, value: "第五级文件1", text: "第五级文件111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"},
{id: 2, pId: -1, value: "第一级目录2", text: "第一级目录2"},
{id: 21, pId: 2, value: "第二级目录3", text: "第二级目录3"},
{id: 22, pId: 2, value: "第二级文件2", text: "第二级文件2"},
{id: 211, pId: 21, value: "第三级目录2", text: "第三级目录2"},
{id: 212, pId: 21, value: "第三级文件2", text: "第三级文件2"},
{id: 2111, pId: 211, value: "第四级文件1", text: "第四级文件1"}
];
var TestTreeModel = BI.inherit(Fix.Model, {
state: function () {
return {
items: BI.deepClone(treeItems),
value: "第三级文件1"
};
},
childContext: ["items"]
});
BI.model("dec.amiba.test.tree.model", TestTreeModel);
var TestTree = BI.inherit(BI.Widget, {
_store: function () {
return BI.Models.getModel("dec.amiba.test.tree.model");
},
props: {
baseCls: "fine-test-tree"
},
watch: {
items: function (items) {
console.log("items", items);
this.treeRef.populate(items);
},
value: function (v) {
console.log("value", v);
}
},
render: function () {
var self = this, o = this.options;
return {
type: "bi.vertical",
height: 150,
lgap: 20,
tgap: 20,
mounted: function () {
// 测试增加树节点
var m = self.model;
var newItems = [
{id: 3, pId: -1, value: "第三级目录1", text: "第三级目录1"},
{id: 31, pId: 3, value: "第三级文件1", text: "第三级文件1"}
];
m.items = m.items.concat(newItems);
// 测试修改value;
setTimeout(function () {
console.log("setValue");
self.treeRef.setValue("不存在的值");
}, 3000);
},
items: [
{
type: "bi.label",
textAlign: "left",
text: "叶子节点单选"
},
{
type: "bi.multilayer_single_tree_combo",
text: "无效值",
defaultText: "无效值",
allowEdit: false,
items: self.model.items,
ref: function (ref) {
self.treeRef = ref;
},
width: 250,
value: self.model.value,
listeners: [{
eventName: "EVENT_CHANGE",
action: function () {
self.model.value = self.treeRef.getValue()[0];
}
}]
}
]
};
}
});
BI.shortcut("dec.amiba.test.tree", TestTree);
})();!(function () {
6 years ago
// 将todolist组件挂载到#wrapper上.
// BI.createWidget({
// type: "my.todolist",
// element: "#wrapper"
// });
// 挂载下拉树测试组件
6 years ago
BI.createWidget({
type: "dec.amiba.test.tree",
6 years ago
element: "#wrapper"
});
})();