Opinion.js 19 KB

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