Menu继承于MenuComponent接口(var MenuComponent = new Interface("MenuComponent", [["getElement"]]);),并且在上一篇组合模式讲过,它作为Composite复合元素。
functionMenuItem(text,title,href,command){
this.text=text;
this.title=title;
this.href=href;
this.command=command;
Interface.registerImplements(this,MenuComponent);
}
MenuItem.prototype={
getElement:function(){
varliElement=document.createElement("li");
liElement.className="Menu-Leaf";
liElement.title=this.title;
varanchor=document.createElement("a");
anchor.href=this.href;
liElement.appendChild(anchor);
anchor.innerHTML=this.text;
varcommand=this.command;
addEvent(anchor,"click",function(){
command.execute();
});
returnliElement;
}
}
其中参数command是作为传递进来的“命令对象”,比如前面的onBoldCommand的“加粗”命令对象,并且通过方法getElement()实现菜单按钮的点击触发达到命令对象的触发请求,而MenuItem不需要知道命令对象的具体实现;
addEvent(anchor, "click", function(){
command.execute();
});
通过这句代码来添加点击事件从而执行命令对象的实现;
4. 既然有“主菜单”,“子菜单”,那么就需要有个“导航条”来“挂接”它们了,这里我添加了一个MenuBar对象,它作为一个初始化菜单显示和所有命令按钮行为实现方法的“容器”,代码如下:
varMenuBar={
list:newArray(),
add:function(component){
this.list.push(component);
},
show:function(container){
varulElement=document.createElement("ul");
ulElement.className="Menu";
for(vari=0,len=this.list.length;i<len;i++){
ulElement.appendChild(this.list[i].getElement());
}
document.getElementById(container).appendChild(ulElement);
}
}
通过show方法进行初始化菜单显示和所有命令按钮行为的实现方法;
5. 现在利用命令模式来进行在线编辑器的实现,新建HTML页面,在window.onload方法中实现:
varfile_menu=newMenu("文件","文件","#");
file_menu.add(newMenuItem("新建","新建","#",newonNewCommand()));
file_menu.add(newMenuItem("导出","导出","#",newonExportCommand()));
file_menu.add(newMenuItem("退出","退出","#",newonExitCommand()));
varedit_menu=newMenu("编辑","编辑","#");
edit_menu.add(newMenuItem("剪切","剪切","#",newonCutCommand()));
edit_menu.add(newMenuItem("复制","复制","#",newonCopyCommand()));
edit_menu.add(newMenuItem("粘贴","粘贴","#",newonPasteCommand()));
edit_menu.add(newMenuItem("删除","删除","#",newonDeleteCommand()));
varformat_menu=newMenu("格式","格式","#");
format_menu.add(newMenuItem("字体","字体","#",newonFontFaceCommand()));
format_menu.add(newMenuItem("字号","字号","#",newonFontSizeCommand()));
format_menu.add(newMenuItem("加粗","加粗","#",newonBoldCommand()));
format_menu.add(newMenuItem("斜体","斜体","#",newonItalicCommand()));
format_menu.add(newMenuItem("下划线","下划线","#",newonUnderlineCommand()));
varformat_menu_1=newMenu("位置","位置","#");
format_menu_1.add(newMenuItem("居左对齐","居左对齐","#",newonLeftCommand()));
format_menu_1.add(newMenuItem("居中对齐","居中对齐","#",newonCenterCommand()));
format_menu_1.add(newMenuItem("居右对齐","居右对齐","#",newonRightCommand()));
format_menu_1.add(newMenuItem("减少缩进","减少缩进","#",newonOutdentCommand()));
format_menu_1.add(newMenuItem("增加缩进","增加缩进","#",newonIndentCommand()));
format_menu.add(format_menu_1);
varformat_menu_2=newMenu("编号","编号","#");
format_menu_2.add(newMenuItem("数字编号","数字编号","#",newonOrderedCommand()));
format_menu_2.add(newMenuItem("项目编号","项目编号","#",newonUnorderedCommand()));
format_menu.add(format_menu_2);
varformat_menu_3=newMenu("字体颜色","字体颜色","#");
format_menu_3.add(newMenuItem("前景颜色","前景颜色","#",newonForeColorCommand()));
format_menu_3.add(newMenuItem("背景颜色","背景颜色","#",newonBackColorCommand()));
format_menu.add(format_menu_3);
varinsert_menu=newMenu("插入","插入","#");
insert_menu.add(newMenuItem("插入链接","插入链接","#",newonLinkCommand()));
insert_menu.add(newMenuItem("插入图片","插入图片","#",newonImageCommand()));
varopr_menu=newMenu("操作","操作","#");
opr_menu.add(newMenuItem("撤销","撤销","#",newonUndoCommand()));
opr_menu.add(newMenuItem("重做","重做","#",newonRedoCommand()));
opr_menu.add(newMenuItem("切换HTML","切换HTML","#",newonToHtmlCommand()));
varcustom_menu=newMenu("自定义格式","自定义格式","#");
custom_menu.add(newMenuItem("格式1","加粗+斜体+下划线","#",newonMacro1Command(newonBoldCommand(),newonItalicCommand(),newonUnderlineCommand())));
varhelp_menu=newMenu("帮助","帮助","#");
help_menu.add(newMenuItem("关于作者","关于作者","#",newonAuthorCommand()));
MenuBar.add(file_menu);
MenuBar.add(edit_menu);
MenuBar.add(format_menu);
MenuBar.add(insert_menu);
MenuBar.add(opr_menu);
MenuBar.add(custom_menu);
MenuBar.add(help_menu);
MenuBar.show("main_container");
各个命令对象作为命令参数传递给对应的子菜单项对象中,其中我们发现有个onMacro1Command的命令对象,它里面包含一系列的其他单命令对象,这个菜单按钮属于“自定义格式”。顾名思义,这里执行一个新的命令,它包括一连串的单命令,“加粗+斜体+下划线”,onMacro1Command类实现如下:
functiononMacro1Command(){
this.commands=newArray();
for(vari=0,len=arguments.length;i<len;i++)
{
this.commands.push(arguments[i]);
}
Interface.registerImplements(this,ICommand);
}
onMacro1Command.prototype.execute=function(){
for(vari=0,len=this.commands.length;i<len;i++)
{
this.commands[i].execute();
}
};
这里通过一个commands数组存储这一系列的命令对象,当onMacro1Command对象执行execute方法时,就一次性地执行数组中的所有命令对象;
6. 还有其他一些JS函数介绍下:
functionaddEvent(target,event_type,handler){
if(target.addEventListener)
target.addEventListener(event_type,handler,false);
elseif(target.attachEvent)
target.attachEvent("on"+event_type,handler);
else
target["on"+event_type]=handler;
}
//弹出DIV层
functionshowDiver(str,width,height){
variWidth=width;
variHeight=height;
document.getElementById("diver").style.width=iWidth+"px";
document.getElementById("diver").style.height=iHeight+"px";
document.getElementById("diver").style.left=(document.body.clientWidth-iWidth)/2+"px";
document.getElementById("diver").style.top=(document.body.clientHeight-iHeight)/2+"px";
document.getElementById("diver").style.display="inline";
document.getElementById("divMore").innerHTML=str;
}
//关闭DIV层
functioncloseDiver(){
document.getElementById("diver").style.display="none";
document.getElementById("divMore").innerHTML="";
}
其中addEvent方法为了兼容各个浏览器的绑定事件的实现。
7. 至于ConcreteCommand各种命令类的实现,请下载源代码自己查看研究吧,这里不进行讲述了。
附:源代码下载
总结
该篇文章用Javascript设计命令模式的思路,实现一个简单的在线编辑器。
本篇到此为止,谢谢大家阅读!
最后祝:大家在新的一年里,工作顺利,事业进步,牛年牛运!Fighting!!!
参考文献:《Head First Design Pattern》
《Professional Javascript Design Patterns》
本系列文章转载时请注明出处,谢谢合作!
相关系列文章:
Javascript乱弹设计模式系列(6) - 单件模式(Singleton)
Javascript乱弹设计模式系列(5) - 命令模式(Command)
Javascript乱弹设计模式系列(4) - 组合模式(Composite)
Javascript乱弹设计模式系列(3) - 装饰者模式(Decorator)
Javascript乱弹设计模式系列(2) - 抽象工厂以及工厂方法模式(Factory)
Javascript乱弹设计模式系列(1) - 观察者模式(Observer)
Javascript乱弹设计模式系列(0) - 面向对象基础以及接口和继承类的实现
优质内容筛选与推荐>>
1、SOA (service oriented architecture)2、RabbitMQ-Windows单机集群搭建3、站外seo详解!4、mysql 批量更新和批量插入5、算法模型定义介绍
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
分享想法到看一看
确定
最多200字,当前共字
微信扫一扫
关注该公众号