|
/* Attention:
* if use "iscroll5" component for 'wm-dropmenu'-element's ancestor,
* pleas don't set transform property,transform property will make "position-fixed" like "position-absolute";
* example:
* new IScroll("#content",{tap:true,scrollbars: true,useTransform:false});
*/
define(["jcl","wmWebUI"],function($,WmWebUI){
/*下拉菜单构造方法*/
function WmDropmenu(id) {
this.items = new Array();
var dropmenu = WmWebUI.getElement(id);
/* 使用children方法代替子元素选择器,是为了防止有子元素误判 */
this.labelButton = dropmenu.children("div").eq(0).children("span").eq(0);
this.menu = dropmenu.children("div").eq(1);
this.closeAction;// 关闭事件
this.openAction;// //打开事件
}
/*下拉菜单项构造方法*/
function WmDropmenuItem(dropmenu, index, dropmenuItem) {
this.dropmenu = dropmenu;
this.index = index;
this.dropmenuItem = dropmenuItem;
}
/*移除下拉选项*/
WmDropmenuItem.prototype.remove = function() {
this.dropmenuItem.parentNode.removeChild(this.dropmenuItem);
this.dropmenu.items.splice(this.index, 1);//删除第index个元素
for ( var i = this.index; i < this.dropmenu.items.length; i++) {
this.dropmenu.items[i].index = i;
}
this.dropmenu.initMenu();
}
/*设置下拉选项的文本内容*/
WmDropmenuItem.prototype.html = function(html) {
if (html == undefined) {
return this.dropmenuItem.innerHTML;
} else {
this.dropmenuItem.innerHTML = html;
}
}
/*设置下拉选项的事件*/
WmDropmenuItem.prototype.setAction = function(callback) {
var that = this;
this.dropmenuItem.onclick = function() {
callback(that);
}
}
/*获取下拉选项的序号*/
WmDropmenuItem.prototype.getIndex=function(){
return this.index;
}
/*初始化下拉菜单*/
WmDropmenu.prototype.initMenu=function(){
this.menu.css("display","");
//设置 dropmenu
//初始化(CSS已将它默认放到了界面之外,以下设置不会造成闪烁)
this.menu.children("ul").css("top",(this.menu.height())*(-1) + "px");
this.menu.children("ul").css("transition","-webkit-transform 0.2s ease-out");
this.menu.children("ul").css("-webkit-transition","transform 0.2s ease-out");
this.labelButton.children(".e_ico-unfold").css("transition","transform 0.2s ease-out");
this.labelButton.children(".e_ico-unfold").css("-webkit-transition","-webkit-transform 0.2s ease-out");
this.menu.css("visibility","hidden");
//位置
this.menu.css("left",this.labelButton.offset().left + this.labelButton.width() - this.menu.width());//靠右要减去这两个 width(),靠左则不需要
this.menu.css("top",this.labelButton.offset().top + 1.1 * this.labelButton.height());/*modified*/
this.menu.css("display","none");
this.menu.css("visibility","visible");
}
WmDropmenu.prototype.create = function() {
var that = this;
var lis = this.menu.children("ul").children("li");
lis.each(function(index) {
that.items.push(new WmDropmenuItem(that, index, this));
});
this.initMenu();
// 显隐
this.labelButton.tap(function() {
if (that.invisible()) {
that.show();
} else {
that.hidden();
}
});
};
/*判断下拉菜单是否可见*/
WmDropmenu.prototype.invisible = function() {
return this.menu.css("display") == "none";
};
/*显示下拉菜单*/
WmDropmenu.prototype.show = function() {
this.openAction();
this.menu.css("display", "block");
this.menu.children("ul").css("transform","translateY(" + this.menu.height() + "px)");
this.menu.children("ul").css("-webkit-transform","translateY(" + this.menu.height() + "px)");
this.labelButton.find(".e_ico-unfold").css("transform","rotate(180deg)");
this.labelButton.find(".e_ico-unfold").css("-webkit-transform","rotate(180deg)");
};
/*隐藏下拉菜单*/
WmDropmenu.prototype.hidden = function(){
this.closeAction();
this.menu.children("ul").css("transform","translateY(0)");
this.menu.children("ul").css("-webkit-transform","translateY(0)");
this.labelButton.find(".e_ico-unfold").css("transform","rotate(0)");
this.labelButton.find(".e_ico-unfold").css("-webkit-transform","rotate(0)");
this.menu.css("display","none");
};
/*删除所有下拉菜单选项*/
WmDropmenu.prototype.removeAll = function() {
this.menu.children("ul")[0].innerHTML = "";
this.items = new Array();
};
/*添加下拉菜单选项*/
WmDropmenu.prototype.push = function(obj){
this.menu.children("ul").append("<li>"+obj+"</li>");
this.initMenu();
var lis = this.menu.children("ul").children("li");
var item = new WmDropmenuItem(this,lis.length-1,lis.last()[0]);
this.items.push(item);
return item;
};
/* 设置下拉菜单文本 */
WmDropmenu.prototype.setLabel = function(label) {
this.labelButton.children("span")[0].innerHTML = label;
};
/* 获取下拉菜单文本 */
WmDropmenu.prototype.getLabel = function() {
return this.labelButton.children("span")[0].innerHTML;
};
/* 设置关闭下拉菜单事件 */
WmDropmenu.prototype.setCloseAction = function(callback) {
this.closeAction = callback;
};
/* 设置打开下拉菜单事件 */
WmDropmenu.prototype.setOpenAction = function(callback) {
this.openAction = callback;
};
return WmDropmenu;
});
|