Opinion.js 19 KB

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