fineui是帆软报表和BI产品线所使用的前端框架。
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.

319 lines
9.2 KiB

8 years ago
/**
* Created by GUY on 2015/6/26.
* @class BI.ButtonGroup
* @extends BI.Widget
*/
BI.ButtonGroup = BI.inherit(BI.Widget, {
_defaultConfig: function () {
return BI.extend(BI.ButtonGroup.superclass._defaultConfig.apply(this, arguments), {
baseCls: "bi-button-group",
behaviors: {},
8 years ago
items: [],
8 years ago
chooseType: BI.Selection.Single,
layouts: [{
type: "bi.center",
hgap: 0,
vgap: 0
}]
})
},
_init: function () {
BI.ButtonGroup.superclass._init.apply(this, arguments);
var behaviors = {};
BI.each(this.options.behaviors, function (key, rule) {
behaviors[key] = BI.BehaviorFactory.createBehavior(key, {
rule: rule
})
});
this.behaviors = behaviors;
this.populate(this.options.items);
},
_createBtns: function (items) {
var o = this.options;
return BI.createWidgets(BI.createItems(items, {
type: "bi.text_button"
}));
},
_btnsCreator: function (items) {
var self = this, args = Array.prototype.slice.call(arguments), o = this.options;
var buttons = this._createBtns(items);
args[0] = buttons;
BI.each(this.behaviors, function (i, behavior) {
behavior.doBehavior.apply(behavior, args);
});
BI.each(buttons, function (i, btn) {
btn.on(BI.Controller.EVENT_CHANGE, function (type, value, obj) {
if (type === BI.Events.CLICK) {
switch (o.chooseType) {
case BI.ButtonGroup.CHOOSE_TYPE_SINGLE:
self.setValue(btn.getValue());
break;
case BI.ButtonGroup.CHOOSE_TYPE_NONE:
self.setValue([]);
break;
}
self.fireEvent(BI.ButtonGroup.EVENT_CHANGE, value, obj);
}
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments);
});
btn.on(BI.Events.DESTROY, function () {
BI.remove(self.buttons, btn);
})
});
return buttons;
},
_packageBtns: function (btns) {
var o = this.options;
for (var i = o.layouts.length - 1; i > 0; i--) {
btns = BI.map(btns, function (k, it) {
return BI.extend({}, o.layouts[i], {
items: [
BI.extend({}, o.layouts[i].el, {
el: it
})
]
})
})
}
return btns;
},
8 years ago
_packageSimpleItems: function (btns) {
var o = this.options;
return BI.map(o.items, function (i, item) {
if (BI.stripEL(item) === item) {
return btns[i];
}
return BI.extend({}, item, {
el: btns[i]
})
})
},
8 years ago
_packageItems: function (items, packBtns) {
return BI.createItems(BI.makeArrayByArray(items, {}), BI.clone(packBtns));
},
_packageLayout: function (items) {
var o = this.options, layout = BI.deepClone(o.layouts[0]);
var lay = BI.formatEL(layout).el;
while (lay && lay.items && !BI.isEmpty(lay.items)) {
lay = BI.formatEL(lay.items[0]).el;
}
lay.items = items;
return layout;
},
8 years ago
//如果是一个简单的layout
_isSimpleLayout: function () {
var o = this.options;
return o.layouts.length === 1
},
8 years ago
doBehavior: function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(this.buttons);
BI.each(this.behaviors, function (i, behavior) {
behavior.doBehavior.apply(behavior, args);
})
},
prependItems: function (items) {
var o = this.options;
var btns = this._btnsCreator.apply(this, arguments);
this.buttons = BI.concat(btns, this.buttons);
8 years ago
if (this._isSimpleLayout() && this.layouts && this.layouts.prependItems) {
8 years ago
this.layouts.prependItems(btns);
return;
}
8 years ago
items = this._packageItems(items, this._packageBtns(btns));
this.layouts.prependItems(this._packageLayout(items).items);
8 years ago
},
addItems: function (items) {
var o = this.options;
var btns = this._btnsCreator.apply(this, arguments);
this.buttons = BI.concat(this.buttons, btns);
//如果是一个简单的layout
8 years ago
if (this._isSimpleLayout() && this.layouts && this.layouts.addItems) {
8 years ago
this.layouts.addItems(btns);
return;
}
8 years ago
items = this._packageItems(items, this._packageBtns(btns));
this.layouts.addItems(this._packageLayout(items).items);
8 years ago
},
8 years ago
removeItemAt: function (index) {
this.buttons[index].destroy();
this.layouts.removeItemAt(index);
8 years ago
},
populate: function (items) {
8 years ago
items = items || [];
8 years ago
this.empty();
8 years ago
this.options.items = items;
8 years ago
this.buttons = this._btnsCreator.apply(this, arguments);
8 years ago
if (this._isSimpleLayout()) {
items = this._packageSimpleItems(this.buttons);
} else {
items = this._packageItems(items, this._packageBtns(this.buttons));
}
8 years ago
8 years ago
this.layouts = BI.createWidget(BI.extend({element: this}, this._packageLayout(items)));
8 years ago
},
setEnable: function (b) {
BI.ButtonGroup.superclass.setEnable.apply(this, arguments);
BI.each(this.buttons, function (i, item) {
item.setEnable(b);
});
},
setNotSelectedValue: function (v) {
v = BI.isArray(v) ? v : [v];
BI.each(this.buttons, function (i, item) {
if (BI.deepContains(v, item.getValue())) {
item.setSelected && item.setSelected(false);
} else {
item.setSelected && item.setSelected(true);
}
});
},
setEnabledValue: function (v) {
v = BI.isArray(v) ? v : [v];
BI.each(this.buttons, function (i, item) {
if (BI.deepContains(v, item.getValue())) {
item.setEnable(true);
} else {
item.setEnable(false);
}
});
},
setValue: function (v) {
v = BI.isArray(v) ? v : [v];
BI.each(this.buttons, function (i, item) {
if (BI.deepContains(v, item.getValue())) {
item.setSelected && item.setSelected(true);
} else {
item.setSelected && item.setSelected(false);
}
});
},
getNotSelectedValue: function () {
var v = [];
BI.each(this.buttons, function (i, item) {
if (item.isEnabled() && !(item.isSelected && item.isSelected())) {
v.push(item.getValue());
}
});
return v;
},
getValue: function () {
var v = [];
BI.each(this.buttons, function (i, item) {
if (item.isEnabled() && item.isSelected && item.isSelected()) {
v.push(item.getValue());
}
});
return v;
},
getAllButtons: function () {
return this.buttons;
},
getAllLeaves: function () {
return this.buttons;
},
getSelectedButtons: function () {
var btns = [];
BI.each(this.buttons, function (i, item) {
if (item.isSelected && item.isSelected()) {
btns.push(item);
}
});
return btns;
},
getNotSelectedButtons: function () {
var btns = [];
BI.each(this.buttons, function (i, item) {
if (item.isSelected && !item.isSelected()) {
btns.push(item);
}
});
return btns;
},
getIndexByValue: function (value) {
var index = -1;
BI.any(this.buttons, function (i, item) {
if (item.isEnabled() && item.getValue() === value) {
index = i;
return true;
}
});
return index;
},
getNodeById: function (id) {
var node;
BI.any(this.buttons, function (i, item) {
if (item.isEnabled() && item.options.id === id) {
node = item;
return true;
}
});
return node;
},
getNodeByValue: function (value) {
var node;
BI.any(this.buttons, function (i, item) {
if (item.isEnabled() && item.getValue() === value) {
node = item;
return true;
}
});
return node;
},
8 years ago
empty: function () {
BI.ButtonGroup.superclass.empty.apply(this, arguments);
this.options.items = [];
},
destroy: function () {
BI.ButtonGroup.superclass.destroy.apply(this, arguments);
8 years ago
this.options.items = [];
8 years ago
}
});
BI.extend(BI.ButtonGroup, {
CHOOSE_TYPE_SINGLE: BI.Selection.Single,
CHOOSE_TYPE_MULTI: BI.Selection.Multi,
CHOOSE_TYPE_ALL: BI.Selection.All,
CHOOSE_TYPE_NONE: BI.Selection.None,
CHOOSE_TYPE_DEFAULT: BI.Selection.Default
});
BI.ButtonGroup.EVENT_CHANGE = "EVENT_CHANGE";
8 years ago
BI.shortcut("bi.button_group", BI.ButtonGroup);