define(function (require, exports, module) { /** * [Toolbar 类] * @param {[String]} id [id] * @param {[Object]} args [参数] */ function ToolbarAdapter(id, args, pageId) { this.id = id; this.pageId = pageId; this.opts = {}; this.actions = {}; this.vmodel = {}; this.data = []; _init(this, args); } function _init(self, args) { //业务类相关操作和事件 if (args.data && args.data.isBiz) { _initBiz(self, args); } else { _.extend(self, args); } } function _initBiz(self, args) { self.biz = args.data; args.data = self.biz.funcOp; if (self.biz.dsMaster) { self.biz.dsMaster.on('onCurrentChanged', function (ds, e) { self.update(true); }, self); } self.biz.on('onStateChanged', function (s, e) { self.update(false); }, self); if (!args.actions || !args.actions.click) { var actions = { click: function (opCode) { self.biz.opRouter(opCode); } }; args.actions = _.extend(actions, args.actions); } _.extend(self, args); } /** * 设置可用 * @param {[type]} opCode 按钮操作码 */ ToolbarAdapter.prototype.setEnable = function (opCode) { if (this.vmodel.$enable) { this.vmodel.$enable(opCode); } }; ToolbarAdapter.prototype.setText = function (opCode, text) { if (this.vmodel.$setText) { this.vmodel.$setText(opCode, text); } }; ToolbarAdapter.prototype.resize = function (width) { if (this.vmodel.$resize) { this.vmodel.$resize(width); } }; /** * 设置不可用 * @param {[type]} opCode 按钮操作码 */ ToolbarAdapter.prototype.setDisabled = function (opCode) { if (this.vmodel.$disable) { this.vmodel.$disable(opCode); } }; /** * 调用VM上的方法 * @param {[type]} method 方法名 * @return {[type]} [description] */ ToolbarAdapter.prototype.invoke = function (method) { method = '$' + method; if (this.vmodel && this.vmodel[method]) { var slice = Array.prototype.slice; var args = slice.call(arguments, 1); return this.vmodel[method].apply(this.vmodel, args); } }; /** * 更新按钮的可用状态 * @param {Boolean} isRecord 是否是行触发的更新 * @param {[type]} iteratee 返回按钮是否可用 * @return {[type]} [description] */ ToolbarAdapter.prototype.update = function (isRecord, iteratee) { var self = this; if (!iteratee || typeof iteratee !== 'function') { iteratee = function (opCode) { if (self.biz) { return self.biz.getOpEnabled(opCode); } else { return false; } }; } for (var i = 0; i < this.data.length; i++) { var btn = this.data[i]; if (isRecord) { if (btn.isRecord) { if (iteratee(btn.opCode)) { this.setEnable(btn.opCode); } else { this.setDisabled(btn.opCode); } } } else { if (iteratee(btn.opCode)) { this.setEnable(btn.opCode); } else { this.setDisabled(btn.opCode); } } } }; ToolbarAdapter.prototype.resetPrintItems = function (opCode, items) { if (this.vmodel.$resetPrintItems) { this.vmodel.$resetPrintItems(opCode, items); } }; ToolbarAdapter.prototype.addItem = function (opCode, caption, hint) { var btn = { opCode: opCode, opClass: null, caption: caption, hint: hint }; this.data.push(btn); }; ToolbarAdapter.prototype.destroy = function () { if (this.biz) { this.biz.unmount(this) if (this.biz.dsMaster) { this.biz.dsMaster.unmount(this) } } } return ToolbarAdapter; });