Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. <script>
  2. var classLegendariesDropdownId = '#<% =classLegendariesUi.ClientID.ToString() %>';
  3.  
  4. //Initiate classLegendaries
  5. $(document).ready(function () {
  6. getClassLegendaries();
  7. });
  8.  
  9.  
  10. function getClassLegendaries() {
  11. $(classLegendariesDropdownId).select2({
  12. width: '100%', // need to override the changed default
  13. closeOnSelect: false,
  14. ajax: {
  15. url: '/API/classlegendariesapi.aspx?classId=' + $('#<% =New_Application_Class_DropDownList.ClientID.ToString() %>').val(),
  16. dataType: 'json'
  17. },
  18. placeholder: "Select your legendaries",
  19. dropdownAdapter: $.fn.select2.amd.require('select2/selectAllAdapter'),
  20. templateResult: formatLegendary,
  21. templateSelection: formatLegendary
  22. });
  23. }
  24.  
  25. function resetClassLegendariesSelect() {
  26. $(classLegendariesDropdownId).val('').trigger('change');
  27. }
  28.  
  29. function forceRefreshClassLegendariesSelect() {
  30. $(classLegendariesDropdownId).trigger('change');
  31. }
  32.  
  33. function formatLegendary(legendary) {
  34. if (!legendary.id) {
  35. return legendary.text;
  36. }
  37. var $legendary = $(
  38. '<a rel="item=' + legendary.id + '&bonus=3570">' + legendary.text + '</a>'
  39. );
  40. return $legendary;
  41. };
  42.  
  43. $(function () {
  44. $(document).ready(function () {
  45. Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  46.  
  47. function EndRequestHandler(sender, args) {
  48. getClassLegendaries();
  49. }
  50. });
  51. });
  52.  
  53. /*
  54. Add options to select/unselect all options in select2 multiple select elements
  55. &
  56. Define the adapter so that it's reusable
  57. */
  58. $.fn.select2.amd.define('select2/selectAllAdapter', [
  59. 'select2/utils',
  60. 'select2/dropdown',
  61. 'select2/dropdown/attachBody'
  62. ], function (Utils, Dropdown, AttachBody) {
  63.  
  64. function SelectAll() { }
  65. SelectAll.prototype.render = function (decorated) {
  66. var self = this,
  67. $rendered = decorated.call(this),
  68. $selectAll = $(
  69. '<button class="btn btn-xs btn-success" type="button" style="margin-left:6px;"><i class="fa fa-check-square-o"></i> Select All</button>'
  70. ),
  71. $unselectAll = $(
  72. '<button class="btn btn-xs btn-warning" type="button" style="margin-left:6px;"><i class="fa fa-square-o"></i> Unselect All</button>'
  73. ),
  74. $btnContainer = $('<div style="margin-top:3px;background-color:#000;">').append($selectAll).append($unselectAll);
  75. if (!this.$element.prop("multiple")) {
  76. // this isn't a multi-select -> don't add the buttons!
  77. return $rendered;
  78. }
  79. $rendered.find('.select2-dropdown').prepend($btnContainer);
  80. $selectAll.on('click', function (e) {
  81. var $results = $rendered.find('.select2-results__option[aria-selected=false]');
  82. $results.each(function () {
  83. self.trigger('select', {
  84. data: $(this).data('data')
  85. });
  86. });
  87. self.trigger('close');
  88. });
  89. $unselectAll.on('click', function (e) {
  90. var $results = $rendered.find('.select2-results__option[aria-selected=true]');
  91. $results.each(function () {
  92. self.trigger('unselect', {
  93. data: $(this).data('data')
  94. });
  95. });
  96. self.trigger('close');
  97. });
  98. return $rendered;
  99. };
  100.  
  101. return Utils.Decorate(
  102. Utils.Decorate(
  103. Dropdown,
  104. AttachBody
  105. ),
  106. SelectAll
  107. );
  108.  
  109. });
  110.  
  111. /*
  112. adapter could have been defined elsewhere in a global includes script or whereever
  113. we can now use it elsewhere
  114. this can also be applied to non-multi-selects (the buttons won't be added)
  115. */
  116. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement