Guest User

Untitled

a guest
May 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. // Custom transition for Scriptaculous
  2. Effect.Transitions.easeInBack = function(pos) {
  3. var x = 1.618;
  4. return Math.pow(pos, 2) * ((x + 1) * pos - x);
  5. };
  6.  
  7. var WebcallPlugin = {
  8. actionInProcess : false,
  9. validateResult : false,
  10. interval : null,
  11. webcall_id : null,
  12.  
  13. // Initialize plugin
  14. initialize: function(options) {
  15. this.defaults = options;
  16.  
  17. // Action button handling
  18. this.defaults.actionButton.observe('mouseup', function() {
  19.  
  20. if (this.actionInProcess) return;
  21. this.actionInProcess = true;
  22.  
  23. // Making validation
  24. this.validate();
  25.  
  26. if (this.validateResult) {
  27. this.doCall();
  28.  
  29. $('webcall-calling').setStyle({
  30. 'display' : this.defaults.haveMoney ? 'block' : 'none'
  31. });
  32.  
  33. $('webcall-no-money').setStyle({
  34. 'display' : this.defaults.haveMoney ? 'none' : 'block'
  35. });
  36.  
  37. this.slideTo(-this.defaults.frameWidth);
  38. this.defaults.actionButton.addClassName('form-submit-disabled');
  39. }
  40. }.bind(this));
  41.  
  42.  
  43. // Back to main screen
  44. [$$('#webcall-calling a')[0], $$('#webcall-no-money a')[0]].each(function(a, i) {
  45. a.observe('mouseup', function() {
  46. if (this.actionInProcess) return;
  47. this.actionInProcess = true;
  48. this.slideTo(0, function() {
  49. this.defaults.actionButton.removeClassName('form-submit-disabled')
  50. clearInterval(this.interval);
  51. }.bind(this));
  52. }.bind(this));
  53. }.bind(this));
  54.  
  55. // Select element handling
  56. $$('.webcall-selectbox').each(this.change.bind(this));
  57.  
  58. // Input handling
  59. /*$$('.webcall-input input').each(function(input) {
  60. input.observe('keyup', this.setDescription.bind(this, input));
  61. }.bind(this));
  62. */
  63. $$('.webcall-destin input')[0].observe('keyup',
  64. this.setRatePerMin.bind(this));
  65. },
  66.  
  67. // Set rate per min (when no money)
  68. setRatePerMin: function() {
  69. // Make AJAX request here
  70. var phone = $$('.webcall-destin input')[0].value;
  71. new Ajax.Request('/webcalls/single_cost', {parameters: {number: phone}});
  72. //$$('.webcall-rate input')[0].value = '0.777';
  73. },
  74.  
  75. // Set description under input
  76. setDescription: function(input) {
  77. // Make AJAX request here
  78. input.next('p').update('text')
  79. },
  80.  
  81. // Input fields validation
  82. validate: function() {
  83. this.validateResult = true;
  84.  
  85. $$('#webcall-main .form-row').each(function(row) {
  86. var valid = true;
  87. var select = row.select('select')[0];
  88. var input = row.select('input')[0];
  89. var option = select.options[select.selectedIndex];
  90.  
  91. // Validate phone num
  92. if (option.hasClassName('webcall-phone-icon')) {
  93. if (!/^\+{0,1}[\d ]+$/.test(input.value))
  94. valid = false
  95. }
  96. // Validate sip address
  97. else if (option.hasClassName('webcall-sip-icon')) {
  98. if (!/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/i.test(input.value))
  99. valid = false
  100. }
  101. // Validate skype name
  102. else if (option.hasClassName('webcall-skype-icon')) {
  103. if (!/^.+$/.test(input.value))
  104. valid = false
  105. }
  106.  
  107. if (!valid) {
  108. new Effect.Highlight(input, {
  109. startcolor : '#ff2c2c',
  110. endcolor : '#ffffff',
  111. duration : 1,
  112. afterFinish : function() {
  113. this.actionInProcess = false;
  114. }.bind(this)
  115. })
  116.  
  117. this.validateResult = false;
  118.  
  119. }
  120. }.bind(this));
  121. },
  122.  
  123. // Calling
  124. doCall: function() {
  125. if (!this.defaults.haveMoney) {
  126. return;
  127. }
  128.  
  129. $('webcall_form').onsubmit();
  130.  
  131. var callFrom = $$('#webcall-calling .phonenum')[0];
  132. var callStatusFrom = $$('#webcall-calling .phonenum-from')[0];
  133. var callStatusTo = $$('#webcall-calling .phonenum-to')[0];
  134. var callAnsStatusFrom = $$('#webcall-calling .call-status-from')[0];
  135. var callAnsStatusTo = $$('#webcall-calling .call-status-to')[0];
  136. var callFromIcons = $$('#webcall-calling .call-from-icon');
  137. var callToIcons = $$('#webcall-calling .call-to-icon');
  138.  
  139. // Update values
  140. callFrom.update( $('webcall-from').value );
  141. callStatusFrom.update( $('webcall-from').value );
  142. callStatusTo.update( $('webcall-to').value );
  143.  
  144. // Update icons
  145. [callFromIcons, callToIcons].each(function(icons) {
  146. icons.each(function(icon) {
  147. var classNames = icon.classNames().toArray();
  148.  
  149. // For each element className
  150. classNames.each(function(className) {
  151. if (className != 'call-from-icon' && className != 'call-to-icon') {
  152. icon.removeClassName(className);
  153. }
  154. });
  155.  
  156. var select = icon.hasClassName('call-from-icon')
  157. ? $('webcall-selectbox-1')
  158. : $('webcall-selectbox-2');
  159.  
  160. var option = select.options[select.selectedIndex];
  161. icon.addClassName(option.className);
  162. });
  163. });
  164.  
  165. // Make periodical AJAX request and update call statuses
  166. this.updateCallStatus(function(callStatuses) {
  167. callStatuses.each(function(status, statusIndex) {
  168. [callAnsStatusFrom, callAnsStatusTo].each(function(container, containerIndex) {
  169. if (statusIndex != containerIndex) return;
  170.  
  171. switch (status) {
  172. case 0: // Calling 0
  173. container.addClassName('success-calling').update('Calling...');
  174. break;
  175. case 1: // Answered 1
  176. container.addClassName('success-calling').update('Answered');
  177. break;
  178. case 2: // Not Answered 2
  179. container.addClassName('fail-calling').update('Not answered');
  180. break;
  181. }
  182. });
  183. });
  184. });
  185. },
  186.  
  187. // Set webcall ID
  188. setWebcallId : function(webcall_id) {
  189. this.webcall_id = webcall_id;
  190. },
  191.  
  192. // Try to get call status
  193. updateCallStatus: function(callback) {
  194. var scope = this;
  195. this.interval = setInterval(function() {
  196. if (scope.webcall_id == null) return;
  197. var url = '/webcalls/' + scope.webcall_id + '/status';
  198. new Ajax.Request(url, {
  199. onSuccess: function(transport) {
  200. var result = transport.responseText.evalJSON();
  201.  
  202. if ((result[0] == 1 && result[1] == 1)
  203. || (result[0] == 2 || result[1] == 2)) {
  204. clearInterval(scope.interval);
  205. }
  206.  
  207. callback(result);
  208. }, method: 'get'
  209. });
  210. }.bind(this), 500);
  211. },
  212.  
  213. // Slide to pos
  214. slideTo: function(pos, afterFinishCallback) {
  215. afterFinishCallback = typeof afterFinishCallback == 'undefined'
  216. ? function() {}
  217. : afterFinishCallback;
  218.  
  219. return this.defaults.frameContainer.morph({ marginLeft : pos + 'px'}, {
  220. duration : 0.45,
  221. transition : Effect.Transitions.easeInBack,
  222. afterFinish : function() {
  223. afterFinishCallback();
  224. this.actionInProcess = false;
  225. }.bind(this)
  226. });
  227. },
  228.  
  229. // Change select
  230. change: function(el) {
  231. var span = el.previous('h3').select('span')[0];
  232. var select = el.select('select')[0];
  233. var handler = function() {
  234. var option = select.options[select.selectedIndex];
  235. var input = el.next('div').select('input')[0];
  236. var i = el.next('div').select('i')[0];
  237.  
  238. i.setStyle({ display : 'none' });
  239. input.setStyle({ padding : '0' });
  240.  
  241. if (option.hasClassName('webcall-phone-icon')) {
  242. span.update(this.defaults.captions.phone);
  243. i.setStyle({ display : 'block' });
  244. input.setStyle({ padding : '0 0 0 18px' });
  245. } else if (option.hasClassName('webcall-sip-icon')) {
  246. span.update(this.defaults.captions.sip);
  247. } else if (option.hasClassName('webcall-skype-icon')) {
  248. span.update(this.defaults.captions.skype);
  249. }
  250. }.bind(this);
  251.  
  252. handler();
  253. select.observe('change', handler);
  254. }
  255. };
Add Comment
Please, Sign In to add comment