Opinion.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. _loadNodeEdit: function(){
  25. var input = new Element("textarea", {"styles": {
  26. "background": "transparent",
  27. "width": "100%",
  28. "border": "0px"
  29. }});
  30. input.set(this.json.properties);
  31. var node = new Element("div", {"styles": {"ovwrflow": "hidden", "position": "relative"}}).inject(this.node, "after");
  32. input.inject(node);
  33. this.node.destroy();
  34. this.node = node;
  35. //this.node = input;
  36. this.node.set({
  37. "id": this.json.id,
  38. "MWFType": this.json.type
  39. });
  40. this.input = input;
  41. this.node.addEvent("change", function(){
  42. this._setBusinessData(this.getInputData());
  43. }.bind(this));
  44. this.node.getFirst().addEvent("blur", function(){
  45. this.validation();
  46. this.hideSelectOpinionNode();
  47. }.bind(this));
  48. this.node.getFirst().addEvent("keyup", function(){
  49. this.validationMode();
  50. }.bind(this));
  51. this.node.getFirst().addEvent("keydown", function(e){
  52. if (this.selectOpinionNode && (this.selectOpinionNode.getStyle("display")!="none") && this.selectOpinionNode.getFirst()){
  53. if (e.code == 38){ //up
  54. if (this.selectedOpinionNode){
  55. var node = this.selectedOpinionNode.getPrevious();
  56. if (!node) node = this.selectOpinionNode.getLast();
  57. this.unselectedOpinion(this.selectedOpinionNode);
  58. this.selectedOpinion(node)
  59. }else{
  60. node = this.selectOpinionNode.getLast();
  61. this.selectedOpinion(node)
  62. }
  63. }
  64. if (e.code == 40){ //down
  65. debugger;
  66. if (this.selectedOpinionNode){
  67. var node = this.selectedOpinionNode.getNext();
  68. if (!node) node = this.selectOpinionNode.getFirst();
  69. this.unselectedOpinion(this.selectedOpinionNode);
  70. this.selectedOpinion(node)
  71. }else{
  72. node = this.selectOpinionNode.getFirst();
  73. this.selectedOpinion(node)
  74. }
  75. }
  76. if (e.code == 27){ //esc
  77. this.hideSelectOpinionNode();
  78. e.preventDefault();
  79. }
  80. if (e.code == 32 || e.code == 13){ //space
  81. if (this.selectedOpinionNode){
  82. this.setOpinion(this.selectedOpinionNode.get("text"));
  83. e.preventDefault();
  84. }
  85. }
  86. }
  87. }.bind(this));
  88. MWF.UD.getDataJson("userOpinion", function(json){
  89. this.userOpinions = json;
  90. }.bind(this), false);
  91. this.node.getFirst().addEvent("input", function(e){
  92. this.startSearchOpinion();
  93. }.bind(this));
  94. this.node.getFirst().addEvent("focus", function(){
  95. this.startSearchOpinion();
  96. }.bind(this));
  97. },
  98. unselectedOpinion: function(node){
  99. node.setStyle("background-color", "#ffffff");
  100. this.selectedOpinionNode = null;
  101. },
  102. selectedOpinion: function(node){
  103. node.setStyle("background-color", "#d2ddf5");
  104. this.selectedOpinionNode = node;
  105. },
  106. startSearchOpinion: function(){
  107. var t = this.input.get("value");
  108. var arr = t.split(/(,\s*){1}|(;\s*){1}|\s+/g);
  109. t = arr[arr.length-1];
  110. if (t.length){
  111. this.clearSearcheOpinionId();
  112. this.searcheOpinionId = window.setTimeout(function(){
  113. this.searchOpinions(t);
  114. }.bind(this), 500);
  115. }else{
  116. this.clearSearcheOpinionId();
  117. }
  118. },
  119. clearSearcheOpinionId: function(){
  120. if (this.searcheOpinionId) {
  121. window.clearTimeout(this.searcheOpinionId);
  122. this.searcheOpinionId = "";
  123. }
  124. },
  125. searchOpinions: function(t){
  126. var value = this.input.get("value");
  127. var arr = value.split(/[\n\r]/g);
  128. lines = arr.length;
  129. value = arr[arr.length-1];
  130. var offsetValue = value;
  131. //var offsetValue = value.substr(0, value.length-t.length);
  132. var ops = this.userOpinions.filter(function(v, i){
  133. return v.contains(t) && (v!=t);
  134. }.bind(this));
  135. if (ops.length){
  136. this.showSelectOpinionNode(ops, offsetValue, lines);
  137. }else{
  138. this.hideSelectOpinionNode(ops);
  139. }
  140. },
  141. hideSelectOpinionNode: function(){
  142. if (this.selectOpinionNode) this.selectOpinionNode.setStyle("display", "none");
  143. },
  144. showSelectOpinionNode: function(ops, offsetValue, lines){
  145. if (!this.selectOpinionNode) this.createSelectOpinionNode();
  146. this.selectOpinionNode.empty();
  147. ops.each(function(op){
  148. this.createSelectOpinionOption(op);
  149. }.bind(this));
  150. var inputSize = this.input.getSize();
  151. var size = MWF.getTextSize(offsetValue, this.json.inputStyles);
  152. var offY = ((size.y-3)*lines)+3;
  153. if (offY>inputSize.y) offY = inputSize.y;
  154. this.selectOpinionNode.setStyle("display","block");
  155. this.selectOpinionNode.position({
  156. "relativeTo": this.node,
  157. "position": "leftTop",
  158. "edge": "leftTop",
  159. "offset": {"x": size.x, "y": offY}
  160. });
  161. },
  162. createSelectOpinionNode: function(){
  163. this.selectOpinionNode = new Element("div", {"styles": this.form.css.opinionSelectNode}).inject(this.node);
  164. },
  165. createSelectOpinionOption: function(op){
  166. var option = new Element("div", {"styles": this.form.css.opinionSelectOption, "text": op}).inject(this.selectOpinionNode);
  167. if (this.json.selectItemStyles) option.setStyles(this.json.selectItemStyles);
  168. option.addEvents({
  169. "mouseover": function(){this.setStyle("background-color", "#d2ddf5")},
  170. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  171. "mousedown": function(){this.setOpinion(op)}.bind(this)
  172. });
  173. },
  174. setOpinion: function(op){
  175. var v = this.input.get("value");
  176. var arr = v.split(/(,\s*){1}|(;\s*){1}|\s+/g);
  177. t = arr[arr.length-1];
  178. var leftStr = v.substr(0, v.length-t.length);
  179. this.input.set("value", leftStr+op);
  180. this.hideSelectOpinionNode();
  181. },
  182. _afterLoaded: function(){
  183. if (!this.readonly){
  184. this.loadDescription();
  185. }
  186. },
  187. loadDescription: function(){
  188. var v = this._getBusinessData();
  189. if (!v){
  190. if (this.json.description){
  191. var size = this.node.getFirst().getSize();
  192. var w = size.x-23;
  193. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  194. this.descriptionNode.setStyles({
  195. "width": ""+w+"px",
  196. "height": ""+size.y+"px",
  197. "line-height": ""+size.y+"px"
  198. });
  199. this.setDescriptionEvent();
  200. }
  201. }
  202. },
  203. setDescriptionEvent: function(){
  204. if (this.descriptionNode){
  205. if (COMMON.Browser.Platform.name==="ios"){
  206. this.descriptionNode.addEvents({
  207. "click": function(){
  208. this.descriptionNode.setStyle("display", "none");
  209. this.node.getFirst().focus();
  210. }.bind(this)
  211. });
  212. }else if (COMMON.Browser.Platform.name==="android"){
  213. this.descriptionNode.addEvents({
  214. "click": function(){
  215. this.descriptionNode.setStyle("display", "none");
  216. this.node.getFirst().focus();
  217. }.bind(this)
  218. });
  219. }else{
  220. this.descriptionNode.addEvents({
  221. "click": function(){
  222. this.descriptionNode.setStyle("display", "none");
  223. this.node.getFirst().focus();
  224. }.bind(this)
  225. });
  226. }
  227. this.node.getFirst().addEvents({
  228. "focus": function(){
  229. this.descriptionNode.setStyle("display", "none");
  230. }.bind(this),
  231. "blur": function(){
  232. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  233. }.bind(this)
  234. });
  235. }
  236. }
  237. });