Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. var formGestaoFila = function(view) {
  2. var orderingJsonArray = [];
  3.  
  4. var reasons = [];
  5.  
  6. var checkIfExists = function(val, arrayToCheck) {
  7. // Element does not exist in array so should proceed in if statement
  8. var dupElement = true;
  9. arrayToCheck.forEach(function(el) {
  10. for(var prop in el) {
  11. if(val[prop] === el[prop]) {
  12. // Element already in array so should not proceed in if statement
  13. dupElement = false;
  14. break;
  15. }
  16. }
  17. });
  18. return dupElement;
  19. };
  20.  
  21. var removeItem = function(jsonArray, keyName, value) {
  22.  
  23. jsonArray.forEach(function(el, index) {
  24. if(el[keyName] === value) {
  25. jsonArray.splice(index,1);
  26. }
  27. });
  28. }
  29.  
  30. // Available items
  31. $(".source-list").on("click","li:not(.parent-element)", function(e) {
  32. // get id of element for further reference
  33. var listItem = $(this);
  34. var itemId = listItem.attr("id");
  35. listItem.find('span').attr('id', itemId);
  36.  
  37. // clone the item to a variable
  38. var cloneItem = listItem.clone();
  39. // append cloned item to selectedColumns
  40. cloneItem.addClass('regular-node-style');
  41. cloneItem.data("itemid", itemId)
  42. .removeAttr('id')
  43. .appendTo("#selectedColumns");
  44.  
  45. // hide the original item
  46. listItem.hide();
  47.  
  48. var orderingItem = {};
  49. orderingItem.value = listItem.find('span').attr('id');
  50. orderingItem.content = listItem.find('span').text();
  51. orderingJsonArray.push(orderingItem);
  52. setOrderingInput(orderingJsonArray);
  53.  
  54. });
  55.  
  56. // Selected items
  57. $("#selectedColumns").on("click","li:not(.parent-element)", function(e) {
  58. var listItem = $(this);
  59. var itemId = listItem.data("itemid");
  60. var inputItem = $("#" + itemId).find('input');
  61. removeItem(orderingJsonArray, 'value', listItem.find('span').attr('id'));
  62. setOrderingInput(orderingJsonArray);
  63. $("#" + itemId).show();
  64. listItem.removeClass('regular-node-style');
  65. listItem.remove();
  66. });
  67.  
  68. var setOrderingInput = function(jsonObj) {
  69. $('#orderingInputHidden').val(JSON.stringify(jsonObj));
  70. }
  71.  
  72. var showOptionsForAreas = function(targetSelect, areas) {
  73. targetSelect.find('option').each(function() {
  74. // Creates array from option text and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
  75. var area = $(this).text().split(/[\[\]]/)[1];
  76. // If > -1 it matches
  77. if(areas.indexOf(area.toLowerCase()) < 0) {
  78. $(this).hide();
  79. } else {
  80. $(this).show();
  81. }
  82. });
  83. }
  84. // End showOptionsForAreas
  85.  
  86. var showOptionsForSelect = function(targetSelect, sourceSelectVal) {
  87. targetSelect.find('option').each(function() {
  88. // Creates array from option's id and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
  89. var targetSelectOptionVal = $(this).val().split(/[\[\]]/)[1];
  90. // If > -1 it matches
  91. if(sourceSelectVal.indexOf(targetSelectOptionVal.toLowerCase()) < 0) {
  92. $(this).hide();
  93. } else {
  94. $(this).show();
  95. }
  96. });
  97. }
  98. // End showOptionsForSelect
  99.  
  100. showOptionsForSelect($('#queue-operators'), "teleperformance");
  101.  
  102. $('#queue-eps').change(function() {
  103. showOptionsForSelect($('#queue-operators'), $(this).val());
  104. });
  105.  
  106. var showOptionsForArea = function(targetSelect, sourceSelectVal) {
  107. for(var i = 0; i < sourceSelectVal.length; i++) {
  108. sourceSelectVal[i] = sourceSelectVal[i].split(/[\[\]]/)[1];
  109. }
  110.  
  111. targetSelect.find('option').each(function() {
  112. // Creates array from option's id and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
  113. var targetSelectOptionVal = $(this).val();
  114. // If > -1 it matches
  115. if(sourceSelectVal.indexOf(targetSelectOptionVal.toLowerCase()) < 0) {
  116. $(this).hide();
  117. } else {
  118. $(this).show();
  119. }
  120. });
  121. }
  122. // End showOptionsForArea
  123.  
  124. // Add selected reasons to filter in Ordering section
  125. $('#queue-reason').change(function() {
  126. var selectedReasons = [];
  127. var obj = {};
  128. $(this).val().forEach(function(el) {
  129. obj = {
  130. value : el.replace(/[\[|\]]/g, ''),
  131. content : $('#queue-reason').find('option[value="' + el + '"]').text()
  132. }
  133. selectedReasons.push(obj);
  134. });
  135. $('.appended-item').remove();
  136. selectedReasons.forEach(function(el) {
  137. var elementToInsert = '<li class="list-group-item child-element appended-item" id="' + el.value + '"><span> ' + el.content + '</span></li>';
  138. $(elementToInsert).insertAfter('#orderingReasonParent');
  139. });
  140. });
  141.  
  142. var getValuesIntoArray = function(element, arrayToInsert) {
  143. // Cleans array
  144. arrayToInsert.splice(0, arrayToInsert.length);
  145. var obj = {};
  146. // If select is multiselect
  147. if(element.val() instanceof Array) {
  148. element.val().forEach(function(el) {
  149. obj = {
  150. value: el,
  151. content: element.find('option[value="' + el + '"]').text()
  152. };
  153. if(checkIfExists(obj, arrayToInsert)) {
  154. arrayToInsert.push(obj);
  155. }
  156. });
  157. }
  158. // If select is single select
  159. else {
  160. obj = {
  161. value: element.val(),
  162. content: element.find('option[value="' + element.val() + '"]').text()
  163. };
  164. if(checkIfExists(obj, arrayToInsert)) {
  165. arrayToInsert.push(obj);
  166. }
  167. }
  168. };
  169.  
  170.  
  171. var bindJsonBuilderOnSelectChange = function(element, arrayToInsert) {
  172. element.bind('change', function() {
  173. getValuesIntoArray(element, arrayToInsert);
  174. });
  175. };
  176.  
  177. var populateTable = function(arraySource, htmlElementId) {
  178. var result = [];
  179. arraySource.forEach(function(el) {
  180. result.push(el.content);
  181. });
  182. $(htmlElementId).text(result.join(', '));
  183. }
  184.  
  185. $('#btn-queue-done').click(function() {
  186. populateTable(reasons, '#reasons');
  187. populateTable(areas, '#areas');
  188. populateTable(tasks, '#tasks');
  189. populateTable(segment, '#segment');
  190. populateTable(areaLocation, '#areaLocation');
  191. populateTable(eps, '#eps');
  192. populateTable(operators, '#operators');
  193. populateTable(orderingJsonArray, '#ordering');
  194. });
  195.  
  196. var initOptionsForAreas = function() {
  197.  
  198. showOptionsForArea($('#areasSelect'), "sac");
  199. $('#queue-reason').change(function() {
  200. showOptionsForArea($('#areasSelect'), $(this).val());
  201. });
  202.  
  203. showOptionsForAreas($('#tasksSelect'), "sac");
  204. $('#areasSelect').change(function() {
  205. showOptionsForAreas($('#tasksSelect'), $(this).val());
  206. });
  207. }
  208.  
  209. // Last elements
  210. var init = function() {
  211. inicializarCombos();
  212. initOptionsForAreas();
  213. bindJsonBuilderOnSelectChange($('#queue-reason'), reasons);
  214. bindJsonBuilderOnSelectChange($('#areasSelect'), areas);
  215. bindJsonBuilderOnSelectChange($('#tasksSelect'), tasks);
  216. bindJsonBuilderOnSelectChange($('#queue-segment'), segment);
  217. bindJsonBuilderOnSelectChange($('#queue-area-location'), areaLocation);
  218. bindJsonBuilderOnSelectChange($('#queue-eps'), eps);
  219. bindJsonBuilderOnSelectChange($('#queue-operators'), operators);
  220. }
  221.  
  222. try {
  223. return init();
  224. } catch(err) {
  225. throw("Erro ao iniciar o form de GestĆ£o de fila. ", err);
  226. }
  227. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement