Opinion.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.xDesktop.requireApp("process.Work", "lp."+o2.language, null, false);
  3. /** @class Opinion 意见输入框。
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var field = this.form.get("fieldId"); //获取组件对象
  8. * //方法2
  9. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  10. *
  11. * var data = field.getData(); //获取值
  12. * field.setData("字符串值"); //设置值
  13. * field.hide(); //隐藏字段
  14. * var id = field.json.id; //获取字段标识
  15. * var flag = field.isEmpty(); //字段是否为空
  16. * field.resetData(); //重置字段的值为默认值或置空
  17. * @extends MWF.xApplication.process.Xform.$Input
  18. * @o2category FormComponents
  19. * @o2range {Process}
  20. * @hideconstructor
  21. */
  22. MWF.xApplication.process.Xform.Opinion = MWF.APPOpinion = new Class(
  23. /** @lends MWF.xApplication.process.Xform.Opinion# */
  24. {
  25. Implements: [Events],
  26. Extends: MWF.APP$Input,
  27. _loadUserInterface: function(){
  28. this._loadNode();
  29. if (this.json.compute == "show"){
  30. this._setValue(this._computeValue());
  31. }else{
  32. this._loadValue();
  33. }
  34. },
  35. _loadNode: function(){
  36. if (this.readonly){
  37. this._loadNodeRead();
  38. }else{
  39. this._loadNodeEdit();
  40. }
  41. },
  42. _loadNodeRead: function(){
  43. this.node.empty();
  44. this.node.set({
  45. "nodeId": this.json.id,
  46. "MWFType": this.json.type
  47. });
  48. this.node.setStyle("display", "none");
  49. },
  50. validationConfigItem: function(routeName, data){
  51. var flag = (data.status==="all") ? true: (routeName === data.decision);
  52. if (flag){
  53. var n = this.getInputData();
  54. var v = (data.valueType==="value") ? n : n.length;
  55. switch (data.operateor){
  56. case "isnull":
  57. if (!v && !(this.handwritingFile && this.handwritingFile[layout.session.user.distinguishedName])){
  58. this.notValidationMode(data.prompt);
  59. return false;
  60. }
  61. break;
  62. case "notnull":
  63. if (v){
  64. this.notValidationMode(data.prompt);
  65. return false;
  66. }
  67. break;
  68. case "gt":
  69. if (v>data.value){
  70. this.notValidationMode(data.prompt);
  71. return false;
  72. }
  73. break;
  74. case "lt":
  75. if (v<data.value){
  76. this.notValidationMode(data.prompt);
  77. return false;
  78. }
  79. break;
  80. case "equal":
  81. if (v==data.value){
  82. this.notValidationMode(data.prompt);
  83. return false;
  84. }
  85. break;
  86. case "neq":
  87. if (v!=data.value){
  88. this.notValidationMode(data.prompt);
  89. return false;
  90. }
  91. break;
  92. case "contain":
  93. if (v.indexOf(data.value)!=-1){
  94. this.notValidationMode(data.prompt);
  95. return false;
  96. }
  97. break;
  98. case "notcontain":
  99. if (v.indexOf(data.value)==-1){
  100. this.notValidationMode(data.prompt);
  101. return false;
  102. }
  103. break;
  104. }
  105. }
  106. return true;
  107. },
  108. _resetNodeEdit: function(){
  109. var input = new Element("textarea", {"styles": {
  110. "background": "transparent",
  111. "width": "100%",
  112. "border": "0px"
  113. }});
  114. input.set(this.json.properties);
  115. var node = new Element("div", {"styles": {
  116. "ovwrflow": "hidden",
  117. "position": "relative",
  118. "padding-right": "2px"
  119. }}).inject(this.node, "after");
  120. input.inject(node);
  121. this.node.destroy();
  122. this.node = node;
  123. },
  124. _loadNodeEdit: function(){
  125. if (!this.json.preprocessing) this._resetNodeEdit();
  126. var input = this.node.getFirst();
  127. input.set(this.json.properties);
  128. //this.node = input;
  129. this.node.set({
  130. "id": this.json.id,
  131. "MWFType": this.json.type
  132. });
  133. this.input = input;
  134. this.mediaActionArea = new Element("div", {"styles": this.form.css.inputOpinionMediaActionArea}).inject(this.node);
  135. if (this.json.isHandwriting!=="no"){
  136. /**
  137. * @summary 手写意见按钮按钮。
  138. * @member {Element}
  139. */
  140. this.handwritingAction = new Element("div", {"styles": this.form.css.inputOpinionHandwritingAction, "text": MWF.xApplication.process.Work.LP.handwriting}).inject(this.mediaActionArea);
  141. this.handwritingAction.addEvent("click", function(){
  142. this.handwriting();
  143. }.bind(this));
  144. }
  145. if (this.json.isAudio!=="no"){
  146. if (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia){
  147. /**
  148. * @summary 音频按钮.在浏览器支持HTML5的getUserMedia才可用。
  149. * @member {Element}
  150. */
  151. this.audioRecordAction = new Element("div", {"styles": this.form.css.inputOpinionAudioRecordAction, "text": MWF.xApplication.process.Work.LP.audioRecord}).inject(this.mediaActionArea);
  152. this.audioRecordAction.addEvent("click", function(){
  153. this.audioRecord();
  154. }.bind(this));
  155. }
  156. }
  157. this.node.addEvent("change", function(){
  158. this._setBusinessData(this.getInputData());
  159. }.bind(this));
  160. this.node.getFirst().addEvent("blur", function(){
  161. this.validation();
  162. this.hideSelectOpinionNode();
  163. }.bind(this));
  164. this.node.getFirst().addEvent("keyup", function(){
  165. this.validationMode();
  166. }.bind(this));
  167. this.node.getFirst().addEvent("keydown", function(e){
  168. if (this.selectOpinionNode && (this.selectOpinionNode.getStyle("display")!="none") && this.selectOpinionNode.getFirst()){
  169. if (e.code == 38){ //up
  170. if (this.selectedOpinionNode){
  171. var node = this.selectedOpinionNode.getPrevious();
  172. if (!node) node = this.selectOpinionNode.getLast();
  173. this.unselectedOpinion(this.selectedOpinionNode);
  174. this.selectedOpinion(node)
  175. }else{
  176. node = this.selectOpinionNode.getLast();
  177. this.selectedOpinion(node)
  178. }
  179. }
  180. if (e.code == 40){ //down
  181. if (this.selectedOpinionNode){
  182. var node = this.selectedOpinionNode.getNext();
  183. if (!node) node = this.selectOpinionNode.getFirst();
  184. this.unselectedOpinion(this.selectedOpinionNode);
  185. this.selectedOpinion(node)
  186. }else{
  187. node = this.selectOpinionNode.getFirst();
  188. this.selectedOpinion(node)
  189. }
  190. }
  191. if (e.code == 27){ //esc
  192. this.hideSelectOpinionNode();
  193. e.preventDefault();
  194. }
  195. if (e.code == 32 || e.code == 13){ //space
  196. if (this.selectedOpinionNode){
  197. this.setOpinion(this.selectedOpinionNode.get("text"));
  198. e.preventDefault();
  199. }
  200. }
  201. }
  202. }.bind(this));
  203. if( !this.form.json.notLoadUserOpinion ){
  204. MWF.UD.getDataJson("userOpinion", function(json){
  205. this.userOpinions = json;
  206. }.bind(this), false);
  207. }
  208. this.node.getFirst().addEvent("input", function(e){
  209. this.startSearchOpinion();
  210. }.bind(this));
  211. this.node.getFirst().addEvent("focus", function(){
  212. this.startSearchOpinion();
  213. }.bind(this));
  214. },
  215. audioRecord: function(){
  216. if (!this.audioRecordNode) this.createAudioRecordNode();
  217. this.audioRecordNode.show();
  218. this.audioRecordNode.position({
  219. "relativeTo": this.node,
  220. "position": "center",
  221. "edge": "center"
  222. });
  223. var p = this.audioRecordNode.getPosition(this.form.app.content);
  224. var top = p.y;
  225. var left = p.x;
  226. if (p.y<0) top = 10;
  227. if (p.x<0) left = 10;
  228. this.audioRecordNode.setStyles({
  229. "top": ""+top+"px",
  230. "left": ""+left+"px"
  231. });
  232. this.soundFile = {};
  233. MWF.require("MWF.widget.AudioRecorder", function () {
  234. /**
  235. * @summary 音频意见组件.
  236. * @member {o2.widget.AudioRecorder}
  237. */
  238. this.audioRecorder = new MWF.widget.AudioRecorder(this.audioRecordNode, {
  239. "onSave" : function(audioFile){
  240. this.soundFile[layout.session.user.distinguishedName] = audioFile;
  241. if (this.previewNode){
  242. this.previewNode.destroy();
  243. this.previewNode = null;
  244. }
  245. this.previewNode = new Element("audio", {"controls": true, "src": window.URL.createObjectURL(audioFile)}).inject(this.node);
  246. this.audioRecordNode.hide();
  247. // this.page.get("div_image").node.set("src",base64Image);
  248. }.bind(this),
  249. "onCancel": function(){
  250. this.soundFile[layout.session.user.distinguishedName] = null;
  251. delete this.soundFile[layout.session.user.distinguishedName];
  252. if (this.previewNode){
  253. this.previewNode.destroy();
  254. this.previewNode = null;
  255. }
  256. this.audioRecordNode.hide();
  257. }.bind(this)
  258. }, null );
  259. }.bind(this));
  260. },
  261. createAudioRecordNode: function(){
  262. this.audioRecordNode = new Element("div", {"styles": this.form.css.handwritingNode}).inject(this.node, "after");
  263. var size = this.node.getSize();
  264. var y = Math.max(size.y, 320);
  265. var x = Math.max(size.x, 400);
  266. x = Math.min(x, 600);
  267. y = 320;
  268. x = 500;
  269. var zidx = this.node.getStyle("z-index").toInt() || 0;
  270. zidx = (zidx<1000) ? 1000 : zidx;
  271. this.audioRecordNode.setStyles({
  272. "height": ""+y+"px",
  273. "width": ""+x+"px",
  274. "z-index": zidx+1
  275. });
  276. // this.audioRecordNode.position({
  277. // "relativeTo": this.node,
  278. // "position": "center",
  279. // "edge": "center"
  280. // });
  281. },
  282. handwriting: function(){
  283. if (!this.handwritingNode) this.createHandwriting();
  284. this.handwritingNode.show();
  285. this.handwritingNode.position({
  286. "relativeTo": this.node,
  287. "position": "center",
  288. "edge": "center"
  289. });
  290. //var p = this.handwritingNode.getPosition(this.form.app.content);
  291. var p = this.handwritingNode.getPosition(this.handwritingNode.getOffsetParent());
  292. var top = p.y;
  293. var left = p.x;
  294. if (p.y<0) top = 10;
  295. if (p.x<0) left = 10;
  296. this.handwritingNode.setStyles({
  297. "top": ""+top+"px",
  298. "left": ""+left+"px"
  299. });
  300. },
  301. createHandwriting: function(){
  302. /**
  303. * @summary 手写板容器.
  304. * @member {Element}
  305. */
  306. this.handwritingNode = new Element("div", {"styles": this.form.css.handwritingNode}).inject(this.node, "after");
  307. var size = this.node.getSize();
  308. var x = Math.max( this.json.tabletWidth || size.x , 500);
  309. var y = Math.max(this.json.tabletHeight ? (parseInt(this.json.tabletHeight) + 110) : size.y, 320);
  310. //x = Math.min(x, 600);
  311. //y = 320;
  312. //x = 500;
  313. var zidx = this.node.getStyle("z-index").toInt() || 0;
  314. zidx = (zidx<1000) ? 1000 : zidx;
  315. this.handwritingNode.setStyles({
  316. "height": ""+y+"px",
  317. "width": ""+x+"px",
  318. "z-index": zidx+1
  319. });
  320. this.handwritingNode.position({
  321. "relativeTo": this.node,
  322. "position": "center",
  323. "edge": "center"
  324. });
  325. this.handwritingAreaNode = new Element("div", {"styles": this.form.css.handwritingAreaNode}).inject(this.handwritingNode);
  326. this.handwritingActionNode = new Element("div", {"styles": this.form.css.handwritingActionNode, "text": MWF.xApplication.process.Work.LP.saveWrite}).inject(this.handwritingNode);
  327. var h = this.handwritingActionNode.getSize().y+this.handwritingActionNode.getStyle("margin-top").toInt()+this.handwritingActionNode.getStyle("margin-bottom").toInt();
  328. h = y - h;
  329. this.handwritingAreaNode.setStyle("height", ""+h+"px");
  330. this.handwritingFile = {};
  331. MWF.require("MWF.widget.Tablet", function () {
  332. /**
  333. * @summary 手写板组件.
  334. * @member {o2.widget.Tablet}
  335. */
  336. this.tablet = new MWF.widget.Tablet(this.handwritingAreaNode, {
  337. "style": "default",
  338. "contentWidth" : this.json.tabletWidth || 0,
  339. "contentHeight" : this.json.tabletHeight || 0,
  340. "onSave" : function( base64code, base64Image, imageFile ){
  341. this.handwritingFile[layout.session.user.distinguishedName] = imageFile;
  342. if (this.previewNode){
  343. this.previewNode.destroy();
  344. this.previewNode = null;
  345. }
  346. if(this.json.isHandwritingPreview!=="no") this.previewNode = new Element("img", {"src": base64Image}).inject(this.node);
  347. this.handwritingNode.hide();
  348. // this.page.get("div_image").node.set("src",base64Image);
  349. }.bind(this),
  350. "onCancel": function(){
  351. this.handwritingFile[layout.session.user.distinguishedName] = null;
  352. delete this.handwritingFile[layout.session.user.distinguishedName];
  353. if (this.previewNode){
  354. this.previewNode.destroy();
  355. this.previewNode = null;
  356. }
  357. this.handwritingNode.hide();
  358. }.bind(this)
  359. }, null );
  360. this.tablet.load();
  361. }.bind(this));
  362. this.handwritingActionNode.addEvent("click", function(){
  363. //this.handwritingNode.hide();
  364. if (this.tablet) this.tablet.save();
  365. }.bind(this));
  366. },
  367. unselectedOpinion: function(node){
  368. node.setStyle("background-color", "#ffffff");
  369. this.selectedOpinionNode = null;
  370. },
  371. selectedOpinion: function(node){
  372. node.setStyle("background-color", "#d2ddf5");
  373. this.selectedOpinionNode = node;
  374. },
  375. startSearchOpinion: function(){
  376. var t = this.input.get("value");
  377. var arr = t.split(/(,\s*){1}|(;\s*){1}|\s+/g);
  378. t = arr[arr.length-1];
  379. if (t.length){
  380. this.clearSearcheOpinionId();
  381. this.searcheOpinionId = window.setTimeout(function(){
  382. this.searchOpinions(t);
  383. }.bind(this), 500);
  384. }else{
  385. this.clearSearcheOpinionId();
  386. }
  387. },
  388. clearSearcheOpinionId: function(){
  389. if (this.searcheOpinionId) {
  390. window.clearTimeout(this.searcheOpinionId);
  391. this.searcheOpinionId = "";
  392. }
  393. },
  394. searchOpinions: function(t) {
  395. var value = this.input.get("value");
  396. var arr = value.split(/[\n\r]/g);
  397. lines = arr.length;
  398. value = arr[arr.length - 1];
  399. var offsetValue = value;
  400. //var offsetValue = value.substr(0, value.length-t.length);
  401. if (this.userOpinions){
  402. var ops = this.userOpinions.filter(function (v, i) {
  403. return v.contains(t) && (v != t);
  404. }.bind(this));
  405. if (ops.length) {
  406. this.showSelectOpinionNode(ops, offsetValue, lines);
  407. } else {
  408. this.hideSelectOpinionNode(ops);
  409. }
  410. }
  411. },
  412. hideSelectOpinionNode: function(){
  413. if (this.selectOpinionNode) this.selectOpinionNode.setStyle("display", "none");
  414. },
  415. showSelectOpinionNode: function(ops, offsetValue, lines){
  416. if (!this.selectOpinionNode) this.createSelectOpinionNode();
  417. this.selectOpinionNode.empty();
  418. ops.each(function(op){
  419. this.createSelectOpinionOption(op);
  420. }.bind(this));
  421. var inputSize = this.input.getSize();
  422. var size = MWF.getTextSize(offsetValue, this.json.inputStyles);
  423. var offY = ((size.y-3)*lines)+3;
  424. if (offY>inputSize.y) offY = inputSize.y;
  425. this.selectOpinionNode.setStyle("display","block");
  426. this.selectOpinionNode.position({
  427. "relativeTo": this.node,
  428. "position": "leftTop",
  429. "edge": "leftTop",
  430. "offset": {"x": size.x, "y": offY}
  431. });
  432. },
  433. createSelectOpinionNode: function(){
  434. this.selectOpinionNode = new Element("div", {"styles": this.form.css.opinionSelectNode}).inject(this.node);
  435. },
  436. createSelectOpinionOption: function(op){
  437. var option = new Element("div", {"styles": this.form.css.opinionSelectOption, "text": op}).inject(this.selectOpinionNode);
  438. if (this.json.selectItemStyles) option.setStyles(this.json.selectItemStyles);
  439. option.addEvents({
  440. "mouseover": function(){this.setStyle("background-color", "#d2ddf5")},
  441. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  442. "mousedown": function(){this.setOpinion(op)}.bind(this)
  443. });
  444. },
  445. setOpinion: function(op){
  446. var v = this.input.get("value");
  447. var arr = v.split(/(,\s*){1}|(;\s*){1}|\s+/g);
  448. t = arr[arr.length-1];
  449. var leftStr = v.substr(0, v.length-t.length);
  450. this.input.set("value", leftStr+op);
  451. this.hideSelectOpinionNode();
  452. this._setBusinessData(this.getInputData());
  453. },
  454. _afterLoaded: function(){
  455. if (!this.readonly){
  456. this.loadDescription();
  457. }
  458. },
  459. loadDescription: function(){
  460. if (this.readonly || this.json.isReadonly)return;
  461. var v = this._getBusinessData();
  462. if (!v){
  463. if (this.json.description){
  464. var size = this.node.getFirst().getSize();
  465. var w = size.x - 3
  466. if( this.json.showIcon!='no' && !this.form.json.hideModuleIcon ) {
  467. w = size.x - 23;
  468. }
  469. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  470. this.descriptionNode.setStyles({
  471. "width": ""+w+"px",
  472. "height": ""+size.y+"px",
  473. "line-height": ""+size.y+"px"
  474. });
  475. this.setDescriptionEvent();
  476. }
  477. }
  478. },
  479. setDescriptionEvent: function(){
  480. if (this.descriptionNode){
  481. if (COMMON.Browser.Platform.name==="ios"){
  482. this.descriptionNode.addEvents({
  483. "click": function(){
  484. this.descriptionNode.setStyle("display", "none");
  485. this.node.getFirst().focus();
  486. }.bind(this)
  487. });
  488. }else if (COMMON.Browser.Platform.name==="android"){
  489. this.descriptionNode.addEvents({
  490. "click": function(){
  491. this.descriptionNode.setStyle("display", "none");
  492. this.node.getFirst().focus();
  493. }.bind(this)
  494. });
  495. }else{
  496. this.descriptionNode.addEvents({
  497. "click": function(){
  498. this.descriptionNode.setStyle("display", "none");
  499. this.node.getFirst().focus();
  500. }.bind(this)
  501. });
  502. }
  503. this.node.getFirst().addEvents({
  504. "focus": function(){
  505. this.descriptionNode.setStyle("display", "none");
  506. }.bind(this),
  507. "blur": function(){
  508. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  509. }.bind(this)
  510. });
  511. }
  512. }
  513. });