Browse Source

整理代码

es6
guy 3 years ago
parent
commit
ed05129bcf
  1. 95
      examples/dev.html
  2. 125
      examples/test-id.html

95
examples/dev.html

@ -2,51 +2,106 @@
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../dist/2.0/fineui.css"/>
<!-- <script src="../dist/2.0/fineui.js"></script>-->
<script src="http://localhost:9001/fineui.js"></script>
<link rel="stylesheet" type="text/css" href="http://fanruan.design/fineui/2.0/fineui.min.css"/>
<script src="http://fanruan.design/fineui/2.0/fineui.min.js"></script>
</head>
<body>
<div id="wrapper"></div>
<script>
!(function () {
var element2InstanceMap = new WeakMap();
BI.getInstanceByElement = function (element) {
return element2InstanceMap.get(element);
};
BI.Plugin.config(function (options) {
}, function (shortcut, instance) {
instance.element.attr("shortcut", shortcut);
if (instance.options.$testId) {
setIDAttribute(instance.element[0], instance.options.$testId);
}
element2InstanceMap.set(instance.element[0], instance);
});
function setIDAttribute (element, id) {
if (element.id !== "") {
throw new Error("不能修改有默认id的元素");
}
element.setAttribute("id", id);
}
function registerWidgetIdGenerator () {
const idSet = new Set();
return function (shortcut, id) {
if (idSet.has(id)) {
throw new Error("id重复了");
}
idSet.add(id);
BI.Plugin.registerObject(shortcut, function (widget) {
setIDAttribute(widget.element[0], id);
});
};
}
BI.registerWidgetId = registerWidgetIdGenerator();
}());
</script>
<script>
function setTestId (parentShortcut, childShortcut, testId) {
BI.Plugin.configRender(parentShortcut, function (rendered) {
var queue = BI.isArray(rendered) ? rendered : [rendered];// 广度遍历
while (queue.length > 0) {
var element = queue.shift();
BI.each(element, function (key, value) {
if (BI.isArray(value)) {
queue = queue.concat(value);
} else if (BI.isPlainObject(value)) {
queue.push(value);
} else if ("type" === key && value === childShortcut) {
element.$testId = testId;
}
});
}
return rendered;
});
}
setTestId("demo.parent", "bi.button_group", "测试testId");
var Widget = BI.inherit(BI.Widget, {
setup: function () {
var wrapper, list;
var list;
return function () {
return {
type: "bi.vertical",
items: [{
type: "bi.vertical",
invisible: true,
ref: function (_ref) {
wrapper = _ref;
},
items: [{
type: "bi.button_group",
height: 100,
ref: function (_ref) {
list = _ref;
},
items: [],
items: BI.range(10).map(function (i) {
return {
type: "bi.label",
text: i,
cls: "bi-border"
};
}),
layouts: [{
type: "bi.inline",
tgap: 10,
lgap: 10
}]
}]
}, {
type: "bi.button",
text: "点击",
handler: function () {
list.populate(BI.range(10).map(function (i) {
return {
type: "bi.label",
text: i,
cls: "bi-border"
};
}));
wrapper.setVisible(true);
}
}]
};

125
examples/test-id.html

@ -0,0 +1,125 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../dist/2.0/fineui.css"/>
<script src="../dist/2.0/fineui.js"></script>
</head>
<body>
<div id="wrapper"></div>
<script>
!(function () {
var element2InstanceMap = new WeakMap();
BI.getInstanceByElement = function (element) {
return element2InstanceMap.get(element);
};
BI.Plugin.config(function (options) {
}, function (shortcut, instance) {
instance.element.attr("shortcut", shortcut);
if (instance.options.$testId) {
setIDAttribute(instance.element[0], instance.options.$testId);
}
element2InstanceMap.set(instance.element[0], instance);
});
function setIDAttribute (element, id) {
if (element.id !== "") {
throw new Error("不能修改有默认id的元素");
}
element.setAttribute("id", id);
}
function registerWidgetIdGenerator () {
const idSet = new Set();
return function (shortcut, id) {
if (idSet.has(id)) {
throw new Error("id重复了");
}
idSet.add(id);
BI.Plugin.registerObject(shortcut, function (widget) {
setIDAttribute(widget.element[0], id);
});
};
}
BI.registerWidgetId = registerWidgetIdGenerator();
}());
</script>
<script>
function setTestId (parentShortcut, childShortcut, testId) {
BI.Plugin.configRender(parentShortcut, function (rendered) {
var queue = BI.isArray(rendered) ? rendered : [rendered];// 广度遍历
while (queue.length > 0) {
var element = queue.shift();
BI.each(element, function (key, value) {
if (BI.isArray(value)) {
queue = queue.concat(value);
} else if (BI.isPlainObject(value)) {
queue.push(value);
} else if ("type" === key && value === childShortcut) {
element.$testId = testId;
}
});
}
return rendered;
});
}
setTestId("demo.parent", "bi.button_group", "测试testId");
var Widget = BI.inherit(BI.Widget, {
setup: function () {
var list;
return function () {
return {
type: "bi.vertical",
items: [{
type: "bi.button_group",
height: 100,
ref: function (_ref) {
list = _ref;
},
items: BI.range(10).map(function (i) {
return {
type: "bi.label",
text: i,
cls: "bi-border"
};
}),
layouts: [{
type: "bi.inline",
tgap: 10,
lgap: 10
}]
}, {
type: "bi.button",
text: "点击",
handler: function () {
}
}]
};
};
}
});
BI.shortcut("demo.parent", Widget);
BI.createWidget({
type: "bi.absolute",
items: [{
el: {
type: "demo.parent"
},
top: 100,
left: 100
}],
element: "#wrapper"
});
</script>
</body>
</html>
Loading…
Cancel
Save