Opinion.js 21 KB

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