Opinion.js 19 KB

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