Importer.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xDesktop.requireApp("process.Xform", "Button", null, false);
  3. /** @class Importer 导入数据组件,本组件通过导入模型来执行数据的导入,支持内容管理文档,流程管理work,自建表数据的导入。
  4. * @o2cn 导入数据组件
  5. * @example
  6. * //可以在脚本中获取该组件
  7. * //方法1:
  8. * var importer = this.form.get("fieldId"); //获取组件
  9. * //方法2
  10. * var importer = this.target; //在组件本身的脚本中获取
  11. * @extends MWF.xApplication.process.Xform.Button
  12. * @o2category FormComponents
  13. * @since v6.2
  14. * @o2range {Process|CMS|Portal}
  15. * @hideconstructor
  16. */
  17. MWF.xApplication.process.Xform.Importer = MWF.APPImporter = new Class(
  18. /** @lends MWF.xApplication.process.Xform.Importer# */
  19. {
  20. Implements: [Events],
  21. Extends: MWF.xApplication.process.Xform.Button,
  22. options: {
  23. /**
  24. * 加载importer(导入模型对象)的时候执行,可以通过this.target.importer获取导入模型对象。
  25. * @event MWF.xApplication.process.Xform.Importer#loadImporter
  26. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  27. */
  28. /**
  29. * 导入前触发,this.event指向导入的数据,您可以通过修改this.event来修改数据。
  30. * @event MWF.xApplication.process.Xform.Importer#beforeImport
  31. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  32. * @example
  33. * <caption>this.event数据格式如下:</caption>
  34. *[
  35. * [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
  36. * [ "标题二","李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
  37. *]
  38. */
  39. /**
  40. * 数据已经生成,前台进行数据校验时触发,this.event指向导入的数据。
  41. * @event MWF.xApplication.process.Xform.Importer#validImport
  42. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  43. * @example
  44. * <caption>this.event数据格式如下:</caption>
  45. * {
  46. * "data" : [
  47. * [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
  48. * [ "标题二","李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
  49. * ],
  50. * "rowList": [], //导入的行对象数组,数据格式常见本章API的afterCreateRowData说明。
  51. * "validted" : true //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
  52. * }
  53. */
  54. /**
  55. * 前台校验成功,并且后台执行完导入后触发,this.event指向后台返回的导入结果。
  56. * @event MWF.xApplication.process.Xform.Importer#afterImport
  57. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  58. * @example
  59. * <caption>this.event格式如下:</caption>
  60. * {
  61. * "status": "导入成功", //导入结果:状态有 "导入成功","部分成功","导入失败"
  62. * "data": {}, //前台组织好的需要导入的数据
  63. * "rowList": [], //前台组织的行对象数组
  64. * "count" : 10, //导入总数量
  65. * "failCount": 0, //失败数量
  66. * "distribution": "" //导入时候时的错误信息
  67. * }
  68. */
  69. /**
  70. * 创建每行需要导入的数据前触发,this.event指向当前行对象,您可以通过修改this.event.importData来修改数据。
  71. * @event MWF.xApplication.process.Xform.Importer#beforeCreateRowData
  72. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  73. */
  74. /**
  75. * 创建每行需要导入的数据后触发,this.event指向当前行对象。
  76. * @event MWF.xApplication.process.Xform.Importer#afterCreateRowData
  77. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  78. * @example
  79. * <caption>this.event格式如下:</caption>
  80. * {
  81. * "importData": [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //导入的数据
  82. * "data" : {//根据导入模型生成的业务数据
  83. * {
  84. * "subject", "标题一", //subject为导入模型列配置的路径
  85. * "name" : "张三",
  86. * ...
  87. * },
  88. * "document": { //如果导入目标是内容管理,则包含document对象
  89. * "title": "标题一"
  90. * "identity": "xxx@xxx@I"
  91. * ...
  92. * },
  93. * "work": { //如果导入目标是流程管理,则包含work对象
  94. * "title": "标题一"
  95. * "identity": "xxx@xxx@I"
  96. * ...
  97. * },
  98. * "errorTextList" : [], //错误信息
  99. * "errorTextListExcel": [] //在出错界面导出Excel时的错误信息
  100. * }
  101. */
  102. "moduleEvents": ["queryLoad","postLoad", "afterLoad", "loadImporter",
  103. "beforeImport", "validImport","afterImport", "beforeCreateRowData", "afterCreateRowData"]
  104. },
  105. _loadUserInterface: function(){
  106. var button = this.node.getElement("button");
  107. if (!button) button = new Element("button");
  108. button.inject(this.node, "after");
  109. this.node.destroy();
  110. this.node = button;
  111. this.node.set({
  112. "id": this.json.id,
  113. "text": this.json.name || this.json.id,
  114. "styles": this.form.json.buttonStyle || this.form.css.buttonStyles,
  115. "MWFType": this.json.type
  116. });
  117. if( this.json.properties ){
  118. this.node.set(this.json.properties )
  119. }
  120. this.node.addEvent("click", function(){
  121. this.upload();
  122. }.bind(this));
  123. if( this.json.allowDownloadTempalte && this.json.downloadTempalteFieldId ){
  124. this.setDownloadEvent();
  125. }
  126. },
  127. getImporter: function(callback){
  128. var options;
  129. if( this.json.queryImportModel.id ){
  130. options = { "id" : this.json.queryImportModel.id };
  131. }else{
  132. options = {
  133. "application": this.json.queryImportModel.application || this.json.queryImportModel.appName,
  134. "name": this.json.queryImportModel.alias || this.json.queryImportModel.name
  135. }
  136. }
  137. MWF.xDesktop.requireApp("query.Query", "Importer", function () {
  138. /**
  139. * @summary 导入模型对象.
  140. * @member {MWF.xApplication.query.Query.Importer}
  141. * @example
  142. * var importer = this.form.get("fieldId").importer; //获取组件
  143. * if(importer)importer.importFromExcel(); //执行导入
  144. */
  145. this.importer = new MWF.xApplication.query.Query.Importer(this.form.app.content, options, {
  146. "onQueryLoad": function () {
  147. this.fireEvent("loadImporter")
  148. }.bind(this),
  149. "onBeforeImport": function (importedData) {
  150. this.fireEvent("beforeImport", [importedData])
  151. }.bind(this),
  152. "onValidImport": function (arg) {
  153. this.fireEvent("validImport", [arg])
  154. }.bind(this),
  155. "onAfterImport": function ( infor ) {
  156. this.fireEvent("afterImport", [infor])
  157. }.bind(this),
  158. "onBeforeCreateRowData": function (row) {
  159. this.fireEvent("beforeCreateRowData", [row])
  160. }.bind(this),
  161. "onAfterCreateRowData": function (row) {
  162. this.fireEvent("afterCreateRowData", [row])
  163. }.bind(this),
  164. }, this.form.app, this.form.Macro);
  165. if(callback)callback();
  166. }.bind(this));
  167. },
  168. upload: function () {
  169. if( this.importer ){
  170. this.importer.importFromExcel();
  171. }else{
  172. this.getImporter(function(){
  173. this.importer.load();
  174. }.bind(this))
  175. }
  176. },
  177. setDownloadEvent: function () {
  178. this.bindEvent = function () {
  179. var module = this._getModuleByPath(this.json.downloadTempalteFieldId);
  180. if(module)module.node.addEvent("click", function () {
  181. this.downloadTemplate();
  182. }.bind(this))
  183. this.fireEvent("afterLoad");
  184. //加载完成以后,删除事件
  185. this.form.removeEvent("afterModulesLoad", this.bindEvent );
  186. }.bind(this);
  187. //去要表单的所有组件加载完成以后再去获取外部组件
  188. this.form.addEvent("afterModulesLoad", this.bindEvent );
  189. },
  190. downloadTemplate: function(){
  191. if( this.importer ){
  192. this.importer.downloadTemplate( this.getExcelName() );
  193. }else{
  194. this.getImporter(function(){
  195. this.importer.downloadTemplate( this.getExcelName() );
  196. }.bind(this))
  197. }
  198. },
  199. getExcelName: function(){
  200. var title;
  201. if( this.json.excelName && this.json.excelName.code ){
  202. title = this.form.Macro.exec(this.json.excelName.code, this);
  203. }
  204. return title || ""
  205. }
  206. });