Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var formGestaoFila = function(view) {
- var orderingJsonArray = [];
- var reasons = [];
- var checkIfExists = function(val, arrayToCheck) {
- // Element does not exist in array so should proceed in if statement
- var dupElement = true;
- arrayToCheck.forEach(function(el) {
- for(var prop in el) {
- if(val[prop] === el[prop]) {
- // Element already in array so should not proceed in if statement
- dupElement = false;
- break;
- }
- }
- });
- return dupElement;
- };
- var removeItem = function(jsonArray, keyName, value) {
- jsonArray.forEach(function(el, index) {
- if(el[keyName] === value) {
- jsonArray.splice(index,1);
- }
- });
- }
- // Available items
- $(".source-list").on("click","li:not(.parent-element)", function(e) {
- // get id of element for further reference
- var listItem = $(this);
- var itemId = listItem.attr("id");
- listItem.find('span').attr('id', itemId);
- // clone the item to a variable
- var cloneItem = listItem.clone();
- // append cloned item to selectedColumns
- cloneItem.addClass('regular-node-style');
- cloneItem.data("itemid", itemId)
- .removeAttr('id')
- .appendTo("#selectedColumns");
- // hide the original item
- listItem.hide();
- var orderingItem = {};
- orderingItem.value = listItem.find('span').attr('id');
- orderingItem.content = listItem.find('span').text();
- orderingJsonArray.push(orderingItem);
- setOrderingInput(orderingJsonArray);
- });
- // Selected items
- $("#selectedColumns").on("click","li:not(.parent-element)", function(e) {
- var listItem = $(this);
- var itemId = listItem.data("itemid");
- var inputItem = $("#" + itemId).find('input');
- removeItem(orderingJsonArray, 'value', listItem.find('span').attr('id'));
- setOrderingInput(orderingJsonArray);
- $("#" + itemId).show();
- listItem.removeClass('regular-node-style');
- listItem.remove();
- });
- var setOrderingInput = function(jsonObj) {
- $('#orderingInputHidden').val(JSON.stringify(jsonObj));
- }
- var showOptionsForAreas = function(targetSelect, areas) {
- targetSelect.find('option').each(function() {
- // Creates array from option text and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
- var area = $(this).text().split(/[\[\]]/)[1];
- // If > -1 it matches
- if(areas.indexOf(area.toLowerCase()) < 0) {
- $(this).hide();
- } else {
- $(this).show();
- }
- });
- }
- // End showOptionsForAreas
- var showOptionsForSelect = function(targetSelect, sourceSelectVal) {
- targetSelect.find('option').each(function() {
- // Creates array from option's id and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
- var targetSelectOptionVal = $(this).val().split(/[\[\]]/)[1];
- // If > -1 it matches
- if(sourceSelectVal.indexOf(targetSelectOptionVal.toLowerCase()) < 0) {
- $(this).hide();
- } else {
- $(this).show();
- }
- });
- }
- // End showOptionsForSelect
- showOptionsForSelect($('#queue-operators'), "teleperformance");
- $('#queue-eps').change(function() {
- showOptionsForSelect($('#queue-operators'), $(this).val());
- });
- var showOptionsForArea = function(targetSelect, sourceSelectVal) {
- for(var i = 0; i < sourceSelectVal.length; i++) {
- sourceSelectVal[i] = sourceSelectVal[i].split(/[\[\]]/)[1];
- }
- targetSelect.find('option').each(function() {
- // Creates array from option's id and gets second element; e.g. [ "TRATAR ", "SAC", " - FATURAMENTO INCORRETO" ]
- var targetSelectOptionVal = $(this).val();
- // If > -1 it matches
- if(sourceSelectVal.indexOf(targetSelectOptionVal.toLowerCase()) < 0) {
- $(this).hide();
- } else {
- $(this).show();
- }
- });
- }
- // End showOptionsForArea
- // Add selected reasons to filter in Ordering section
- $('#queue-reason').change(function() {
- var selectedReasons = [];
- var obj = {};
- $(this).val().forEach(function(el) {
- obj = {
- value : el.replace(/[\[|\]]/g, ''),
- content : $('#queue-reason').find('option[value="' + el + '"]').text()
- }
- selectedReasons.push(obj);
- });
- $('.appended-item').remove();
- selectedReasons.forEach(function(el) {
- var elementToInsert = '<li class="list-group-item child-element appended-item" id="' + el.value + '"><span> ' + el.content + '</span></li>';
- $(elementToInsert).insertAfter('#orderingReasonParent');
- });
- });
- var getValuesIntoArray = function(element, arrayToInsert) {
- // Cleans array
- arrayToInsert.splice(0, arrayToInsert.length);
- var obj = {};
- // If select is multiselect
- if(element.val() instanceof Array) {
- element.val().forEach(function(el) {
- obj = {
- value: el,
- content: element.find('option[value="' + el + '"]').text()
- };
- if(checkIfExists(obj, arrayToInsert)) {
- arrayToInsert.push(obj);
- }
- });
- }
- // If select is single select
- else {
- obj = {
- value: element.val(),
- content: element.find('option[value="' + element.val() + '"]').text()
- };
- if(checkIfExists(obj, arrayToInsert)) {
- arrayToInsert.push(obj);
- }
- }
- };
- var bindJsonBuilderOnSelectChange = function(element, arrayToInsert) {
- element.bind('change', function() {
- getValuesIntoArray(element, arrayToInsert);
- });
- };
- var populateTable = function(arraySource, htmlElementId) {
- var result = [];
- arraySource.forEach(function(el) {
- result.push(el.content);
- });
- $(htmlElementId).text(result.join(', '));
- }
- $('#btn-queue-done').click(function() {
- populateTable(reasons, '#reasons');
- populateTable(areas, '#areas');
- populateTable(tasks, '#tasks');
- populateTable(segment, '#segment');
- populateTable(areaLocation, '#areaLocation');
- populateTable(eps, '#eps');
- populateTable(operators, '#operators');
- populateTable(orderingJsonArray, '#ordering');
- });
- var initOptionsForAreas = function() {
- showOptionsForArea($('#areasSelect'), "sac");
- $('#queue-reason').change(function() {
- showOptionsForArea($('#areasSelect'), $(this).val());
- });
- showOptionsForAreas($('#tasksSelect'), "sac");
- $('#areasSelect').change(function() {
- showOptionsForAreas($('#tasksSelect'), $(this).val());
- });
- }
- // Last elements
- var init = function() {
- inicializarCombos();
- initOptionsForAreas();
- bindJsonBuilderOnSelectChange($('#queue-reason'), reasons);
- bindJsonBuilderOnSelectChange($('#areasSelect'), areas);
- bindJsonBuilderOnSelectChange($('#tasksSelect'), tasks);
- bindJsonBuilderOnSelectChange($('#queue-segment'), segment);
- bindJsonBuilderOnSelectChange($('#queue-area-location'), areaLocation);
- bindJsonBuilderOnSelectChange($('#queue-eps'), eps);
- bindJsonBuilderOnSelectChange($('#queue-operators'), operators);
- }
- try {
- return init();
- } catch(err) {
- throw("Erro ao iniciar o form de Gestão de fila. ", err);
- }
- }();
Advertisement
Add Comment
Please, Sign In to add comment