Opinion.js 19 KB

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