<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.model-container {
position: fixed;
top: 50px;
left: 20%;
right: 20%;
border: 1px solid #ccc;
margin: 10px 0;
height: 300px;
border-radius: 5px;
overflow: hidden;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.model-container .header{
height: 50px;
line-height: 50px;
background-color: rgb(63, 58, 58);
padding-left: 20px;
color: #fff;
}
.success .header{
background-color: green;
}
.warning .header{
background-color: orange;
}
.error .header{
background-color: red;
}
.btn-box{
color: #fff;
margin-bottom: 50px;
padding: 10px;
}
.btn-box .on{
background-color: green;
}
.btn-box .off {
background-color: red;
}
.btn-box .warning {
background-color: orange;
}
</style>
</head>
<body>
<div class="btn-box">
<div id="btn-t" class="btn-row off">测试</div>
</div>
<div class="model-container" id="app">
<div class="header ">dfdsf</div>
</div>
<div class="button-box">
<button data-type="success">成功</button>
<button data-type="warning">警告</button>
<button data-type="error">失败</button>
</div>
<script>
function setClass(evt,className){
evt.target.className = className;
}
function toggle(...actions){
return function(...args){
console.log(actions);
let action = actions.shift();
actions.push(action)
return action.apply(this,args);
}
}
const oBtn = document.querySelector('#btn-t');
oBtn.onclick = toggle(
evt=>setClass(evt,'on') ,
evt=>setClass(evt,'off'),
evt=>setClass(evt,'warning'),
)
</script>
<script src="./js/factory.js"></script>
<script src="./js/index.js"></script>
</body>
</html>
;(function(){
const oBtn = document.querySelector('.button-box')
const oApp = document.querySelector('#app')
const factory = new Factory(oApp)
const getType = tar=>tar.getAttribute('data-type')
const init = ()=>(bindEvent())
const creates= {
'success' : e => {factory.create('success', '成功!')},
'warning' : e => {factory.create('warning', '警告!')},
'error' : e => {factory.create('error', '失败!')},
}
const bindEvent = ()=>{
oBtn.addEventListener('click',handleBtnClick(creates),false)
}
const handleBtnClick = actions=>{
return function(e){
const tar = e.target
tar.tagName.toLowerCase() == 'button' && actions[getType(tar)].apply([e])
}
}
init()
})()
class Model{
constructor(type){
this.type = type;
}
get getClassName(){
return 'model-container ' + this.type;
}
}
class SuccessModel extends Model{
constructor(){
super("success")
}
}
class WarningModel extends Model{
constructor(){
super("warning")
}
}
class ErrorModel extends Model{
constructor(){
super("error")
}
}
class Factory{
constructor(dom){
this.dom = dom;
this.models = {
success : new SuccessModel(),
warning : new WarningModel(),
error : new ErrorModel()
}
}
create(type, title){
const model = this.models[type];
if(!model){
throw new Error('no this model');
}
this.dom.className = model.getClassName;
this.dom.children[0].innerHTML = title;
return model
}
}