OTRS API Reference JavaScript

Source: Core.Agent.Admin.DynamicFieldMultiselect.js

  1. // --
  2. // Copyright (C) 2001-2018 OTRS AG, https://otrs.com/
  3. // --
  4. // This software comes with ABSOLUTELY NO WARRANTY. For details, see
  5. // the enclosed file COPYING for license information (GPL). If you
  6. // did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
  7. // --
  8. "use strict";
  9. var Core = Core || {};
  10. Core.Agent = Core.Agent || {};
  11. Core.Agent.Admin = Core.Agent.Admin || {};
  12. /**
  13. * @namespace Core.Agent.Admin.DynamicFieldMultiselect
  14. * @memberof Core.Agent.Admin
  15. * @author OTRS AG
  16. * @description
  17. * This namespace contains the special module functions for the DynamicFieldMultiselect module.
  18. */
  19. Core.Agent.Admin.DynamicFieldMultiselect = (function (TargetNS) {
  20. /**
  21. * @name RemoveValue
  22. * @memberof Core.Agent.Admin.DynamicFieldMultiselect
  23. * @function
  24. * @returns {Boolean} Returns false.
  25. * @param {String} IDSelector - ID of the pressed remove value button.
  26. * @description
  27. * This function removes a value from possible values list and creates a stub input so
  28. * the server can identify if a value is empty or deleted (useful for server validation)
  29. * It also deletes the Value from the DefaultValues list
  30. */
  31. TargetNS.RemoveValue = function (IDSelector){
  32. // copy HTML code for an input replacement for the deleted value
  33. var $Clone = $('.DeletedValue').clone(),
  34. // get the index of the value to delete (its always the second element (1) in this RegEx
  35. $ObjectIndex = IDSelector.match(/.+_(\d+)/)[1],
  36. // get the key name to remove it from the defaults select
  37. $Key = $('#Key_' + $ObjectIndex).val();
  38. // set the input replacement attributes to match the deleted original value
  39. // new value and other controls are not needed anymore
  40. $Clone.attr('id', 'Key' + '_' + $ObjectIndex);
  41. $Clone.attr('name', 'Key' + '_' + $ObjectIndex);
  42. $Clone.removeClass('DeletedValue');
  43. // add the input replacement to the mapping type so it can be parsed and distinguish from
  44. // empty values by the server
  45. $('#' + IDSelector).closest('fieldset').append($Clone);
  46. // remove the value from default list
  47. if ($Key !== ''){
  48. $('#DefaultValue').find("option[value='" + $Key + "']").remove();
  49. $('#DefaultValue').trigger('redraw.InputField');
  50. }
  51. // remove possible value
  52. $('#' + IDSelector).parent().remove();
  53. return false;
  54. };
  55. /**
  56. * @name AddValue
  57. * @memberof Core.Agent.Admin.DynamicFieldMultiselect
  58. * @function
  59. * @returns {Boolean} Returns false
  60. * @param {Object} ValueInsert - HTML container of the value mapping row.
  61. * @description
  62. * This function adds a new value to the possible values list
  63. */
  64. TargetNS.AddValue = function (ValueInsert) {
  65. // clone key dialog
  66. var $Clone = $('.ValueTemplate').clone(),
  67. ValueCounter = $('#ValueCounter').val();
  68. // increment key counter
  69. ValueCounter++;
  70. // remove unnecessary classes
  71. $Clone.removeClass('Hidden ValueTemplate');
  72. // add needed class
  73. $Clone.addClass('ValueRow');
  74. // copy values and change ids and names
  75. $Clone.find(':input, a.RemoveButton').each(function(){
  76. var ID = $(this).attr('id');
  77. $(this).attr('id', ID + '_' + ValueCounter);
  78. $(this).attr('name', ID + '_' + ValueCounter);
  79. $(this).addClass('Validate_Required');
  80. // set error controls
  81. $(this).parent().find('#' + ID + 'Error').attr('id', ID + '_' + ValueCounter + 'Error');
  82. $(this).parent().find('#' + ID + 'Error').attr('name', ID + '_' + ValueCounter + 'Error');
  83. $(this).parent().find('#' + ID + 'ServerError').attr('id', ID + '_' + ValueCounter + 'ServerError');
  84. $(this).parent().find('#' + ID + 'ServerError').attr('name', ID + '_' + ValueCounter + 'ServerError');
  85. // add event handler to remove button
  86. if($(this).hasClass('RemoveButton')) {
  87. // bind click function to remove button
  88. $(this).bind('click', function () {
  89. TargetNS.RemoveValue($(this).attr('id'));
  90. return false;
  91. });
  92. }
  93. });
  94. $Clone.find('label').each(function(){
  95. var FOR = $(this).attr('for');
  96. $(this).attr('for', FOR + '_' + ValueCounter);
  97. });
  98. // append to container
  99. ValueInsert.append($Clone);
  100. // set new value for KeyName
  101. $('#ValueCounter').val(ValueCounter);
  102. $('.DefaultValueKeyItem,.DefaultValueItem').bind('keyup', function () {
  103. Core.Agent.Admin.DynamicFieldMultiselect.RecreateDefaultValueList();
  104. });
  105. return false;
  106. };
  107. /**
  108. * @name RecreateDefaultValueList
  109. * @memberof Core.Agent.Admin.DynamicFieldMultiselect
  110. * @function
  111. * @returns {Boolean} Returns false
  112. * @description
  113. * This function re-creates and sort the Default Values list taking the Possible Values
  114. * as source, all deleted values will not be part of the re-created value list
  115. */
  116. TargetNS.RecreateDefaultValueList = function() {
  117. // get the selected default value
  118. var SelectedValue = $("#DefaultValue option:selected").val(),
  119. // define other variables
  120. ValueIndex, Key, Value, KeyID, SelectOptions;
  121. // delete all elements
  122. $('#DefaultValue').empty();
  123. // add the default "possible none" element
  124. $('#DefaultValue').append($('<option>', { value: '' }).text('-'));
  125. // find all active possible values keys (this will omit all previously deleted keys)
  126. $('.ValueRow > .DefaultValueKeyItem').each(function(){
  127. // for each key:
  128. // Get the ID
  129. KeyID = $(this).attr('id');
  130. // extract the index
  131. ValueIndex = KeyID.match(/.+_(\d+)/)[1];
  132. // get the Key and Value
  133. Key = $(this).val();
  134. Value = $('#Value_' + ValueIndex).val();
  135. // check if both are none empty and add them to the default values list
  136. if (Key !== '' && Value !== '') {
  137. $('#DefaultValue').append($('<option>', { value: Key }).text(Value));
  138. }
  139. });
  140. // extract the new value list into an array
  141. SelectOptions = $("#DefaultValue option");
  142. // sort the array by the text (this means the Value)
  143. SelectOptions.sort(function(a, b) {
  144. if (a.text > b.text) {
  145. return 1;
  146. }
  147. else if (a.text < b.text) {
  148. return -1;
  149. }
  150. else {
  151. return 0;
  152. }
  153. });
  154. // clear the list again and re-populate it with the sorted list
  155. $("#DefaultValue").empty().append(SelectOptions);
  156. // set the selected value as it was before, this will not apply if the key name was
  157. // changed
  158. $('#DefaultValue').val(SelectedValue);
  159. $('#DefaultValue').trigger('redraw.InputField');
  160. return false;
  161. };
  162. return TargetNS;
  163. }(Core.Agent.Admin.DynamicFieldMultiselect || {}));

^ Use Elevator