Opinion.js 19 KB

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