Opinion.js 18 KB

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