Guest User

Untitled

a guest
Dec 21st, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.20 KB | None | 0 0
  1. (function ($) {
  2.  
  3. /**
  4. * Services Company
  5. */
  6.  
  7. service.company = {
  8. /*
  9. * Variables
  10. *
  11. *
  12. */
  13. metadataPir: [],
  14. separator: "/",
  15. prefixes: [],
  16. passwordLength: 8,
  17. genUserOptions: {},
  18. advValidationOptions: {},
  19. optSimulateApiUser: {},
  20. /********************************/
  21. test: function () {
  22. var container = $("#dt_service_company_container");
  23. container.load(Routing.generate('service.list', {'draw': true}));
  24. container.show();
  25. },
  26. /**
  27. * Listado de ubicaciones
  28. */
  29. list: function () {
  30. var id_table = "dt_service_company_location";
  31. var table = $('#' + id_table);
  32. var container = $("#dt_service_company_container");
  33. var url_crear = Routing.generate('service.form');
  34.  
  35. var dt = table.DataTable({
  36. serverSide: true,
  37. processing: true,
  38. autoWidth: true,
  39. ajax: Routing.generate('service.list'),
  40. order: [[0, "desc"]],
  41. pageLength: 20,
  42. sDom: datatables.dt_sDom_full,
  43. oLanguage: datatables.dt_oLanguage,
  44. lengthMenu: datatables.dt_lengthMenu,
  45. columns: [
  46. {
  47. data: "id",
  48. visible: false,
  49. title: "id"
  50. },
  51. {data: "codService", title: "Código servicio"},
  52. {data: "customer", title: "Customer"},
  53. {data: "typeService", title: "Tipo servicio"},
  54. {data: "description", title: "Descripción"},
  55. {data: "idState.state", title: "Estado"},
  56. ],
  57. oTableTools: {
  58. aButtons: []
  59. },
  60. buttons: [
  61. {
  62. text: '<i class="fa fa-refresh"></i>',
  63. action: function (e, dt, node, config) {
  64. dt.ajax.reload();
  65. }
  66. },
  67. {
  68. text: '<i class="fa fa-plus"></i>',
  69. action: function (e, dt, node, config) {
  70. window.location.href = '#' + url_crear;
  71. }
  72. },
  73. {text: '<i class="fa fa-file-excel-o"></i>',
  74. action: function (e, dt, node, config) {
  75. var url = Routing.generate('location.editor', {
  76. search: dt.search(),
  77. draw: 'excel'
  78. });
  79. window.open(url, '_blank');
  80. }
  81. }
  82. ],
  83. preDrawCallback: function () {
  84. if (!datatables.dt_responsiveHelper) {
  85. datatables.dt_responsiveHelper = new ResponsiveDatatablesHelper(table, datatables.dt_breakpointDefinition);
  86. }
  87. },
  88. drawCallback: function (oSettings) {
  89. datatables.dt_responsiveHelper.respond();
  90. $('.dataTables_processing', table.closest(".dataTables_wrapper")).show();
  91. },
  92. initComplete: function (settings, json) {
  93. container.show();
  94. }
  95. });
  96. $("#" + id_table + " tbody").on("dblclick", "tr", function (event) {
  97. var id = dt.row(this).data().id;
  98. var url = Routing.generate('service.form', {
  99. id: id
  100. });
  101. if (id) {
  102. window.location.href = '#' + url;
  103. }
  104. });
  105.  
  106. $("#" + id_table + " tbody").on("mousedown", "tr", function (event) {
  107. var id = dt.row(this).data().id;
  108. var url = Routing.generate('service.form', {
  109. id: id
  110. });
  111. if ('mousedown' === event.type && 2 === event.which) {
  112. var href = window.location.href;
  113. var hrefPart = href.split('#')[0] + '#' + url;
  114. window.open(hrefPart, '_blank');
  115. }
  116. });
  117. },
  118. /**
  119. * Guarda un formulario de ubicaciones
  120. */
  121. saveForm: function ()
  122. {
  123. $(".masmovil-ajax-form").unbind('submit');
  124. $(".masmovil-ajax-form").submit(function (e) {
  125. console.log('submit');
  126. e.preventDefault();
  127. $(".masmovil-ajax-submit").button('loading');
  128. var submitdata = $(this).serialize();
  129. var isValid = service.company.advancedValidation($(this));
  130. if (isValid === true) {
  131. $.ajax({
  132. url: e.currentTarget.action,
  133. type: 'post',
  134. dataType: 'json',
  135. data: submitdata,
  136. success: function (data) {
  137. masmovil.form.success(data);
  138. window.location.href = '#' + Routing.generate('service.form', {
  139. id: data.id
  140. });
  141. }
  142. }).fail(function (jqXHR, textStatus, error) {
  143. masmovil.form.fail(jqXHR, textStatus, error);
  144. });
  145. } else {
  146. service.company.showValidationErrors(isValid);
  147. masmovil.form.fail(false, isValid.title, isValid.error);
  148. }
  149.  
  150. });
  151. },
  152. setAdvacedValidation: function (validation) {
  153. servcomp = service.company;
  154. if (servcomp.isJSON(validation)) {
  155. servcomp.advValidationOptions = validation;
  156. // console.log(servcomp.advValidationOptions);
  157. }
  158.  
  159. },
  160. advancedValidation: function (form) {
  161. var arrayOptions = service.company.advValidationOptions;
  162. if (!$.isEmptyObject(arrayOptions)) {
  163. var isValid = true;
  164. for (index in arrayOptions) {
  165.  
  166. var options = arrayOptions[index];
  167. switch (options.type) {
  168.  
  169. case 'percent':
  170. var textProfile = $(options.profileId).find("option:selected").text();
  171. var valueProfile = $(options.profileId).val();
  172. if (textProfile == "A medida" || valueProfile == "") {
  173. var sum = 0;
  174. $(options.classtarget).each(function (i, element) {
  175. if ($(this).val() != "") {
  176. sum += parseInt($(this).val());
  177. }
  178.  
  179. });
  180. if (options.totalpercent == sum) {
  181. if (isValid === false) {
  182. isValid = true;
  183. }
  184. } else {
  185. isValid = {
  186. 'isValid': false,
  187. 'title': 'Atención.',
  188. 'error': 'Los porcentajes no suman ' + options.totalpercent,
  189. 'classtarget': options.classtarget,
  190. 'classErrorText': ''
  191. };
  192. }
  193. }
  194. break;
  195. case 'pw_blocks':
  196. nodeSrc = $(options.Nodes_ids.src).val();
  197. nodeDst = $(options.Nodes_ids.dst).val();
  198. nodeDstBackup = $(options.Nodes_ids.dstbackup).val();
  199. interfSrc = $(options.Interfaces_ids.src).val();
  200. interfDst = $(options.Interfaces_ids.dst).val();
  201.  
  202. if (nodeSrc != nodeDst) {
  203. if ((nodeDstBackup == nodeDst) && (nodeDstBackup == nodeSrc)) {
  204. isValid = {
  205. 'isValid': false,
  206. 'title': 'Atención.',
  207. 'error': ' Error en la configuración. Por favor, revise las configuraciones remarcadas.',
  208. 'classtarget': options.genericPwError,
  209. 'classErrorText': ''
  210. };
  211. }
  212. } else {
  213.  
  214. if (interfSrc == interfDst) {
  215. isValid = {
  216. 'isValid': false,
  217. 'title': 'Atención.',
  218. 'error': 'No pueden coincidir las configuraciones de Src y Dst',
  219. 'classtarget': options.classSrcEqualDstError,
  220. 'classErrorText': ''
  221. };
  222. }
  223. }
  224.  
  225. break;
  226. }
  227.  
  228. }
  229.  
  230.  
  231. return isValid;
  232. } else {
  233. return true;
  234.  
  235. }
  236.  
  237. },
  238. generateModal: function (action, options) {
  239. options || (options = {});
  240. if (options.hasOwnProperty("idModal")) {
  241. var modal = $(options.idModal);
  242. } else {
  243. var modal = $("#genericModal");
  244. }
  245.  
  246. var title = modal.find('#modal-title');
  247. var body = modal.find('#modal-body');
  248. if (!options.hasOwnProperty("title")) {
  249. options.title = "Alerta del sistema.";
  250. }
  251. if (!options.hasOwnProperty("body")) {
  252. options.body = "";
  253. }
  254. if (options.hasOwnProperty("size")) {
  255. switch (options.size) {
  256. case 'large':
  257. modal.find(".modal-dialog").removeClass("modal-lg");
  258. modal.find(".modal-dialog").removeClass("modal-sm");
  259. modal.find(".modal-dialog").addClass("modal-lg");
  260. break;
  261. case 'small':
  262. modal.find(".modal-dialog").removeClass("modal-lg");
  263. modal.find(".modal-dialog").removeClass("modal-sm");
  264. modal.find(".modal-dialog").addClass("modal-sm");
  265. break;
  266. }
  267. }
  268.  
  269. title.html(options.title);
  270. body.html(options.body);
  271. switch (action) {
  272. case 'show':
  273. modal.modal('show');
  274. break;
  275. case 'clear':
  276. console.log(body.html());
  277. body.html("");
  278. console.log(body.html());
  279. modal.modal('hide');
  280. break;
  281. }
  282.  
  283. return modal;
  284.  
  285.  
  286. },
  287. showValidationErrors: function (options) {
  288.  
  289. var errorClass = "state-error";
  290. $("." + errorClass).removeClass(errorClass);
  291. var label = $(options.classtarget).parent('label');
  292. label.each(function () {
  293. $(this).addClass(errorClass);
  294. });
  295.  
  296. },
  297. /***********************************************************/
  298. simulateApiUser: function (mode, options) {
  299. var form = $("#service_form");
  300. switch (mode) {
  301. case 'initialize':
  302. if (options.hasOwnProperty("idAccessType")) {
  303. if (options.hasOwnProperty("typeAuxForm") && options.hasOwnProperty("modalOptions")) {
  304. $('#service_apiValidate').show();
  305. $('#service_save').hide();
  306. service.company.optSimulateApiUser = options;
  307.  
  308.  
  309. $('#service_apiValidate').on('click', function () {
  310. var isValid = service.company.advancedValidation(form);
  311. console.log(isValid);
  312. isValid = true;
  313. if (isValid === true) {
  314. service.company.simulateApiUser('validate', {});
  315. } else {
  316. service.company.showValidationErrors(isValid);
  317. masmovil.form.fail(false, isValid.title, isValid.error);
  318. }
  319. });
  320.  
  321. } else {
  322. console.log('"options.typeAuxForm" is not defined.');
  323. }
  324. } else {
  325. console.log();
  326. }
  327. break;
  328. case 'validate':
  329. var simulateOptions = service.company.optSimulateApiUser;
  330. var accesstype = $(simulateOptions.idAccessType);
  331. if (accesstype.val() != "") {
  332. var accesstypeVal = accesstype.find("option:selected").text();
  333. var typeAuxForm = simulateOptions.typeAuxForm;
  334. var patt = new RegExp("^PPPoE");
  335. if (patt.test(accesstypeVal)) {
  336. if (typeAuxForm === 1 || typeAuxForm === 2) {
  337. var submitdata = form.serialize();
  338. $.ajax({
  339. url: Routing.generate('service.apiradius.simulate', {
  340. typeAuxForm: typeAuxForm
  341. }),
  342. type: 'post',
  343. // dataType: 'json',
  344. // async: false,
  345. data: submitdata,
  346. success: function (response) {
  347. var modalOptions = simulateOptions.modalOptions;
  348. modalOptions.body = service.company.processApiResponse(response,modalOptions.subtitle);
  349. modal = service.company.generateModal("show",modalOptions);
  350. modal.find('.actions').on('click',function(){
  351. var action = $(this).data('action');
  352. if (action == "ok") {
  353. form.trigger('submit');
  354. modal.modal('hide');
  355. }else if(action == "cancel"){
  356. service.company.generateModal("clear",modalOptions)
  357. }
  358. });
  359. }
  360. }).fail(function (jqXHR, textStatus, error) {
  361. masmovil.form.fail(jqXHR, textStatus, error);
  362. });
  363. }
  364. } else {
  365. // form.unbind('submit');
  366. form.trigger('submit');
  367. // console.log();
  368. }
  369. } else {
  370. // masmovil.form.fail(jqXHR, textStatus, error);
  371. }
  372. break;
  373. }
  374. },
  375. processApiResponse: function (response, subtitle) {
  376. var out = "";
  377. out += "<p class='alert alert-warning'>";
  378. out += " <i class='fa fa-warning'></i> " + subtitle;
  379. out += "</p>";
  380. // console.log(response);
  381. var content = response.replace(/\//g, "");
  382. content = content.replace(/{/g, "");
  383. content = content.replace(/\"/g, "");
  384. content = content.split('\\n');
  385. out += "<ul class='list-group'>";
  386. for (cont in content) {
  387. out += "<li class='list-group-item'>";
  388. out += content[cont];
  389. out += "</li>";
  390. }
  391. out += "</ul>";
  392. return out;
  393. },
  394. loadAux: function () {
  395.  
  396. if ($("#service_category").length && $("#service_category").val() != "") {
  397. service.company.ajaxGetForm(service.company.setParametersAux());
  398. }
  399. $("#service_category").on('change', function () {
  400. service.company.ajaxGetForm(service.company.setParametersAux());
  401.  
  402. });
  403.  
  404. },
  405. setParametersAux: function () {
  406. var parameters = new Object();
  407. parameters.category = ($("#service_category").length) ? $("#service_category").val() : null;
  408. parameters.idGestion = ($("#service_idGestion").length) ? $("#service_idGestion").val() : null;
  409. parameters.idInet = ($("#service_idInet").length) ? $("#service_idInet").val() : null;
  410. parameters.idVpn = ($("#service_idVpn").length) ? $("#service_idVpn").val() : null;
  411. parameters.idPw = ($("#service_idPw").length) ? $("#service_idPw").val() : null;
  412. return parameters;
  413. },
  414. ajaxGetForm: function (parameters) {
  415. $.ajax({
  416. type: "GET",
  417. url: Routing.generate('service.generate.form', parameters),
  418. success: function (html) {
  419. $("#service_form_aux").html(html);
  420. }
  421. });
  422. },
  423. initPir: function (metadata) {
  424. service.company.metadataPir = metadata;
  425. initValue = $("#" + metadata.id_hidden_pir).val();
  426. if (initValue != "") {
  427. var processValue = service.company.processPir(initValue);
  428. $("." + metadata.class_pir).each(function (i, element) {
  429. $(this).attr('name', '');
  430. $(this).val(processValue[i]);
  431. });
  432. }
  433. },
  434. watchPir: function () {
  435. metadataPir = service.company.metadataPir;
  436. $("." + metadataPir.class_pir).change(function () {
  437. var inputValues = [];
  438. $("." + metadataPir.class_pir).each(function () {
  439. inputValues.push($(this).val());
  440. });
  441. var hiddenValue = service.company.processPir(inputValues);
  442. $("#" + metadataPir.id_hidden_pir).val(hiddenValue);
  443. });
  444. },
  445. processPir: function (value) {
  446. separator = service.company.separator;
  447. if (Array.isArray(value)) {
  448. //Mergue interface values in the hidden field
  449. return value.join(separator);
  450. } else if (value != "") {
  451. //Split hidden value to a interface values
  452. return value.split(separator);
  453. } else {
  454. return array();
  455. }
  456.  
  457.  
  458. },
  459. watchNode: function (target_id, replace_id) {
  460. $(target_id).on('change', function (element) {
  461. var category_id = $('#service_category').val();
  462. service.company.getInterface($(this), category_id, replace_id);
  463. });
  464. },
  465. getInterface: function (idNode, category_id, target_id) {
  466. var parameters = new Object();
  467. parameters.idNode = idNode.val();
  468. parameters.category_id = category_id;
  469. if (typeof idNode.data('targetfield') !== 'undefined') {
  470. parameters.field_name = idNode.data('targetfield');
  471. }
  472. $.ajax({
  473. type: "GET",
  474. url: Routing.generate('service.load.interface', parameters),
  475. success: function (html) {
  476. $(target_id).replaceWith(html);
  477.  
  478. }
  479. });
  480. },
  481. /*********************DISABLE FIELDS BY ACCESS TECNOLOGY ******************************/
  482. initAccessTecnology: function (origin, data, ModeAnd) {
  483. ModeAnd || (ModeAnd = false);
  484. var options = {'ModeAnd': ModeAnd, 'GenCredentials': false};
  485. var value = $(origin).val();
  486. if (value !== "") {
  487. service.company.controlStateFields($(origin + " option:selected").text(), data, options);
  488. }
  489. },
  490. watchAccessTecnology: function (origin, data, ModeAnd) {
  491. ModeAnd || (ModeAnd = false);
  492. var options = {'ModeAnd': ModeAnd, 'GenCredentials': true};
  493.  
  494. $(origin).on('change', function () {
  495. if ($(this).val() !== "") {
  496. service.company.controlStateFields($(this).find("option:selected").text(), data, options);
  497. }
  498. });
  499. },
  500. controlStateFields: function (value, data, options) {
  501. if (service.company.isJSON(data)) {
  502. for (item in data) {
  503. var actions = {};
  504. if (data[item].hasOwnProperty('regexIndex')) {
  505. if (data[item].hasOwnProperty('idPrefix')) {
  506. actions.prefix = data[item]['idPrefix'];
  507.  
  508. } else {
  509. actions.prefix = "";
  510. }
  511. var patt = new RegExp(data[item]['regexIndex']);
  512. if (patt.test(value)) {
  513. actions.enable = data[item]['ifIs']['enable'];
  514. actions.disable = data[item]['ifIsNot']['enable'];
  515. if (options.ModeAnd) {
  516. actions = service.company.andAction(true, data[item], actions);
  517. }
  518. if (options.GenCredentials) {
  519. if (actions.enable.indexOf("username") != "-1") {
  520. service.company.genUser({'mode': 'gen'});
  521. }
  522. if (actions.enable.indexOf("password") != "-1") {
  523. $(actions.prefix + 'password').val(service.company.genPassword(8));
  524. }
  525. }
  526.  
  527. } else {
  528.  
  529. actions.enable = data[item]['ifIsNot']['enable'];
  530. actions.disable = data[item]['ifIs']['enable'];
  531. if (options.ModeAnd) {
  532. actions = service.company.andAction(false, data[item], actions);
  533. }
  534. }
  535. }
  536. service.company.makeStatusActions(actions);
  537.  
  538. }
  539. }
  540. },
  541. andAction: function (parent_status, data, actions) {
  542. var enable = Array.from(actions.enable);
  543. var disable = Array.from(actions.disable);
  544. if (parent_status) {
  545. // if(data['ifIs'].hasOwnProperty('AndConditions')){
  546. // var conditional = data['ifIs']['AndConditions'];
  547. // }
  548. if (data['ifIsNot'].hasOwnProperty('AndConditions')) {
  549. var conditional = data['ifIsNot']['AndConditions'];
  550. for (index in conditional) {
  551. disable.push(conditional[index]['target']);
  552. }
  553. }
  554.  
  555. } else {
  556. if (data['ifIsNot'].hasOwnProperty('AndConditions')) {
  557. var conditional = data['ifIsNot']['AndConditions'];
  558. for (index in conditional) {
  559. value = $(data['idPrefix'] + index + " option:selected").text();
  560. comparative = conditional[index]['comparative'];
  561.  
  562. if (comparative) {
  563. if (value == conditional[index]['value']) {
  564. enable.push(conditional[index]['target']);
  565. } else {
  566. disable.push(conditional[index]['target']);
  567. }
  568. } else {
  569. if (value != conditional[index]['value']) {
  570. enable.push(conditional[index]['target']);
  571. } else {
  572. disable.push(conditional[index]['target']);
  573. }
  574. }
  575.  
  576. }
  577. }
  578.  
  579.  
  580. }
  581. actions.enable = enable;
  582. actions.disable = disable;
  583. return actions;
  584. },
  585. makeStatusActions: function (actions) {
  586. if (actions.prefix) {
  587. for (item in actions.disable) {
  588. $(actions.prefix + "" + actions.disable[item]).prop("disabled", true);
  589. }
  590. for (item in actions.enable) {
  591. var enabledElem = $(actions.prefix + "" + actions.enable[item]);
  592. enabledElem.prop("disabled", false);
  593. if (actions.enable[item] == "password" && enabledElem.val() == "") {
  594. enabledElem.val(service.company.genPassword(service.company.passwordLength));
  595. }
  596. }
  597. }
  598.  
  599. },
  600. /*************************************************************/
  601. /*********************GESTION CPE*****************************/
  602. initGestionCPE: function (origin, target) {
  603. var value = $(origin).val();
  604. if (value == 1) {
  605. $(target).prop("disabled", false);
  606. } else {
  607. $(target).prop("disabled", true);
  608. }
  609. },
  610. watchGestionCPE: function (origin, target) {
  611. $(origin).on('change', function () {
  612. var value = $(this).val();
  613. if (value == 1) {
  614. $(target).prop("disabled", false);
  615. } else {
  616. $(target).prop("disabled", true);
  617. }
  618. });
  619. },
  620. /*********************************************************/
  621. initProfile: function (origin, target_class, prefix) {
  622. var text = $(origin).find("option:selected").text();
  623. var value = $(origin).val();
  624. if (text == "A medida" || value == "") {
  625. $(target_class).each(function (i, element) {
  626. if (value == "") {
  627. $(this).prop("readonly", true);
  628. } else {
  629. $(this).prop("readonly", false);
  630. }
  631. });
  632. } else {
  633. var percents = service.company.getPercents(value, prefix);
  634. $(target_class).each(function (i, element) {
  635. $(this).prop("readonly", true);
  636. });
  637.  
  638. }
  639. },
  640. watchProfile: function (origin, target_class, prefix) {
  641. $(origin).on('change', function () {
  642. var text = $(this).find("option:selected").text();
  643. var value = $(this).val();
  644. if (text == "A medida" || value == "") {
  645. $(target_class).each(function (i, element) {
  646. $(this).prop("readonly", false);
  647. $(this).val('');
  648. });
  649.  
  650.  
  651. } else {
  652. var percents = service.company.getPercents(value, prefix);
  653. $(target_class).each(function (i, element) {
  654. $(this).prop("readonly", true);
  655. });
  656.  
  657. }
  658. });
  659. },
  660. getPercents: function (profile_id, prefix) {
  661. var parameters = new Object();
  662. parameters.profile_id = profile_id;
  663. $.ajax({
  664. type: "GET",
  665. url: Routing.generate('service.profiles.get', parameters),
  666. success: function (response) {
  667. for (var property in response) {
  668. $("#" + prefix + property).val(response[property]);
  669. }
  670.  
  671. }
  672. });
  673. },
  674. genPassword: function (longitud)
  675. {
  676. var password = "";
  677. $.ajax({
  678. type: "GET",
  679. async: false,
  680. url: Routing.generate('service.password.gen', {
  681. lengthPass: longitud
  682. }),
  683. success: function (response) {
  684. password = response;
  685. }
  686. });
  687. return password;
  688. },
  689. /*
  690. *
  691. * Format Inet: $servicecode_$ACT/BCK_$bw_$VoIP@masmovil-ggcc
  692. * Format VPN: $vrf_$sede_$ACT/BCK_$bw_$qos
  693. */
  694. genUser: function (options) {
  695. if (options.hasOwnProperty('mode')) {
  696. if (options.mode == "init" && options.hasOwnProperty('type')) {
  697. service.company.genUserOptions = options;
  698. } else {
  699. var type = service.company.genUserOptions.type;
  700. var user = new Array();
  701. for (index in type.originIds) {
  702. if (typeof type.originIds[index] === 'string') {
  703. var element = $(type.originIds[index]);
  704. if (service.company.getValueField(element)) {
  705. user.push(service.company.getValueField(element));
  706. }
  707. } else {
  708. var element = $(type.originIds[index].id);
  709. var textValue = type.originIds[index].textValue;
  710. var value = service.company.getValueField(element, textValue);
  711. if (type.originIds[index].hasOwnProperty('type')) {
  712. typeField = type.originIds[index].type;
  713. switch (typeField) {
  714. case 'checkbox':
  715. if (value) {
  716. var valueTrue = type.originIds[index].valueTrue
  717. if (valueTrue != "") {
  718. user.push(valueTrue);
  719. }
  720. } else {
  721. valueFalse = type.originIds[index].valueFalse;
  722. if (valueFalse != "") {
  723. user.push(valueFalse);
  724. }
  725. }
  726.  
  727. break;
  728. case 'select':
  729. if (value == "A medida") {
  730. user.push('CUSTOM');
  731. } else if (value != "") {
  732. user.push(value);
  733. }
  734.  
  735. break;
  736.  
  737. }
  738.  
  739. } else {
  740.  
  741. }
  742.  
  743. }
  744. }
  745. var initOptions = service.company.genUserOptions;
  746. if (initOptions.hasOwnProperty('target') && initOptions.target != "") {
  747. var out = user.join(initOptions.separator);
  748. $(initOptions.target).val(out);
  749. }
  750. }
  751. }
  752. },
  753. /***********************************************************/
  754. tagsInput: function (origin) {
  755. $(origin).tagsinput();
  756. $(".bootstrap-tagsinput input").attr('placeholder', '999.999.999.999/99');
  757. $(origin).on('beforeItemAdd', function (event) {
  758. var regexIpLen = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$/;
  759. if (!regexIpLen.test(event.item)) {
  760. var error = {
  761. 'title': 'Atención.',
  762. 'error': 'Formato de prefijo incorrecto (IP/Len). ',
  763. };
  764. masmovil.form.fail(false, error.title, error.error);
  765. event.cancel = true;
  766.  
  767. }
  768. // event.item: contains the item
  769. // event.cancel: set to true to prevent the item getting added
  770. // event.cancel = true;
  771. });
  772. },
  773. /***********************************************************/
  774. getValueField: function (element, textValue) {
  775. textValue || (textValue = false);
  776. var tag = element.prop("tagName");
  777. switch (tag) {
  778. case 'SELECT':
  779. if (element.val() != "") {
  780. if (textValue) {
  781. return element.find("option:selected").text();
  782. } else {
  783. return element.val();
  784. }
  785. }
  786. return false;
  787. break;
  788. case 'INPUT':
  789. type = element.attr('type');
  790. switch (type) {
  791. case 'hidden':
  792. case 'text':
  793. case 'number':
  794. if (element.val() != "") {
  795. return element.val();
  796. }
  797. return false;
  798. break;
  799. case 'checkbox':
  800. return element.prop('checked');
  801. break;
  802. }
  803. break;
  804. }
  805. return null;
  806.  
  807. },
  808. isJSON: function (item) {
  809. item = typeof item !== "string"
  810. ? JSON.stringify(item)
  811. : item;
  812.  
  813. try {
  814. item = JSON.parse(item);
  815. } catch (e) {
  816. return false;
  817. }
  818.  
  819. if (typeof item === "object" && item !== null) {
  820. return true;
  821. }
  822.  
  823. return false;
  824. },
  825. hasJSONProperty: function (index, object) {
  826. for (item in object) {
  827. if (object[item].hasOwnProperty('regexIndex')) {
  828. var patt = new RegExp(object[item]['regexIndex']);
  829. if (patt.test(index)) {
  830. return object[item];
  831. } else {
  832. return false;
  833. }
  834. } else {
  835. if (object.hasOwnProperty(item)) {
  836. return object.hasOwnProperty(item);
  837. } else {
  838. return false;
  839. }
  840. }
  841. }
  842. },
  843. };
  844. })(jQuery);
Add Comment
Please, Sign In to add comment