define(function (require, exports, module) { var BaseWindow = require('system/views/pages/window'); var tpl = require('text!./scriptwin5.tpl'); require('css!./scriptwin5.css'); function Window(args) { this.options = { title: '脚本查看器', content: tpl, width: 800, height: 600, modal: true, closed: true, closable: true, data: null } this.args = args; BaseWindow.call(this, this.options); this.scriptType = args.scriptType; this.dsMaster = new DataSource(); this.dsMaster.uiObjCode = 'tFrmFuncMenu'; this.dsMaster.fixQuery['MenuGroup'] = args.dataRow['MenuGroup']; } inherits(Window, BaseWindow); Window.prototype.init = function () { var self = this; } Window.prototype.select = function () { this.close(); }; Window.prototype.doOp = function (opCode) { var isHandled = false; switch (opCode) { case 'ok': this.select(); break; case 'exit': this.$txtScript.select(); break; case 'refresh': this.refresh(); break; case 'export': this.exportScript(); break; } }; Window.prototype.refresh = function () { var self = this; this.dsMaster.search(); }; Window.prototype.customerize = function ($container) { var self = this; this.$txtScript = this.getElement('.txtscript'); this.$scriptNameInput = this.getElement('.edp-frd-script-name-input'); var $btnOk = this.getElement('.btn-script-ok'); if ($btnOk) { $btnOk.on('click', function () { self.doOp('ok'); }); } var $btnCancel = this.getElement('.btn-script-cancel'); if ($btnCancel) { $btnCancel.click(function () { self.doOp('exit'); }); } var $btnExport = this.getElement('.btn-script-export'); if ($btnExport) { $btnExport.click(function() { self.doOp('export'); }); } this.$scriptNameInput.val(this.scriptName); self.loadScript(); }; Window.prototype.open = function () { var self = this; var code = self.dsMaster.uiObjCode; var post_data = { funcCode: '', uiObjCodes: code } self.makeScriptName(); Store.services.getUiObjSchema(post_data, function (isOk, data) { if (isOk) { self.dsMaster.initUIObj(data[code]); BaseWindow.prototype.open.call(self); } }); }; Window.prototype.makeScriptName = function() { this.scriptName = Store.dbScriptsProjectNo + '07_XX' + '_预置菜单组' + this.args.dataRow['MenuGroup'] + '.sql'; }; Window.prototype.exportScript = function() { var self = this, scriptContent = self.$txtScript.val(), scriptName = self.$scriptNameInput.val(), blob = new Blob( [scriptContent], {type: "text/plain;charset=" + document.characterSet} ), url = window.URL.createObjectURL(blob), download_link = $('.export-script')[0]; Store.services.fileExport({ filename: scriptName, content: scriptContent }, function (isok, data) { if (isok) { download_link.href = Store.host + data.path; download_link.download = scriptName; download_link.click(); window.URL.revokeObjectURL(url); } }) }; Window.prototype.loadScript = function () { var self = this; //数据脚本特殊处理 self.dsMaster.search(function (isOk, ds) { if (isOk) { self.createDictScript() } }); }; function getSpace(num) { var str = ''; for (var i = 0; i < num; i++) { str += ' '; } ; return str; } Window.prototype.createDictScript = function () { var self = this; var dictScriptTpl = require('text!./dictScript5.tpl'); var mysqlScriptTpl = require('text!./dictMysqlScript5.tpl'); var deleteSql = "DELETE FROM tFrmFuncMenu WHERE MenuId='" + this.args.dataRow['MenuId'] + "'"; //主表相关脚本 var sqlObj = ['']; var objRow = this.args.dataRow; ; var sqlObjValues = [""]; appendStr(sqlObj, "INSERT INTO tFrmFuncMenu (", true); for (var i = 0; i < objRow.dataSrc.columns.length; i++) { var col = objRow.dataSrc.columns[i]; if (col.isCalc) { continue; } var _value = getSqlValue(col, objRow); appendStr(sqlObj, col.fieldName); appendStr(sqlObjValues, _value); } removeComma(sqlObj); removeComma(sqlObjValues); appendStr(sqlObj, ") VALUES (", true); appendStr(sqlObj, sqlObjValues[0], true); appendStr(sqlObj, ");\n", true); deleteSql += ";\n"; sqlObj = deleteSql + sqlObj; //合成最后的脚本 var finalScript = (this.scriptType === 'oracle' ? dictScriptTpl : mysqlScriptTpl); finalScript = finalScript.replace('#MASTER', sqlObj); self.$txtScript.val(finalScript); }; //获取数据库值的字符串 function getSqlValue(col, row) { var value = row[col.fieldName]; if (col.dataType == Store.Enums.DataType.STRING || Store.Enums.DataType.TEXT) { return value == null ? value : "'" + value + "'"; } if (col.dataType == Store.Enums.DataType.DATETIME) { return "sysdate"; } return value == null ? value : ''; }; function removeComma(str) { var len = str[0].length; if (str[0][len - 1] == ',') { str[0] = str[0].slice(0, -1); } }; function appendStr(src, str, noDot) { src[0] = src[0] + str; if (!noDot) { src[0] += ','; } }; return Window; })