toggledesigner.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*jslint browser: true, white: true */
  2. /*global console,jQuery,megamenu,window,navigator*/
  3. /**
  4. * Max Mega Menu jQuery Plugin
  5. */
  6. jQuery(function($) {
  7. // Make block areas sortable
  8. $( ".mega-blocks .mega-left" ).sortable({
  9. forcePlaceholderSize: false,
  10. items : '.block',
  11. stop: function() {
  12. reindex_blocks();
  13. },
  14. connectWith: ".mega-blocks .mega-right, .mega-blocks .mega-center"
  15. });
  16. $( ".mega-blocks .mega-right" ).sortable({
  17. forcePlaceholderSize: false,
  18. items : '.block',
  19. stop: function() {
  20. reindex_blocks();
  21. },
  22. connectWith: ".mega-blocks .mega-left, .mega-blocks .mega-center"
  23. });
  24. $( ".mega-blocks .mega-center" ).sortable({
  25. forcePlaceholderSize: false,
  26. items : '.block',
  27. stop: function() {
  28. reindex_blocks();
  29. },
  30. connectWith: ".mega-blocks .mega-left, .mega-blocks .mega-right"
  31. });
  32. // Reindex blocks based on position
  33. var reindex_blocks = function() {
  34. var i = 0;
  35. $(".mega-blocks .block").each(function() {
  36. i++;
  37. var block = $(this);
  38. block.find('input, select, textarea').each(function() {
  39. // account for inputs created by select2
  40. if (typeof $(this).attr('name') !== 'undefined') {
  41. $(this).attr('name', $(this).attr('name').replace(/\[\d+\]/g, "[" + i + "]"));
  42. }
  43. });
  44. // update the align value based on block position
  45. block.find('input.align').each(function() {
  46. if (block.parent().hasClass('mega-right')) {
  47. $(this).attr('value', 'right');
  48. } else if (block.parent().hasClass('mega-center')) {
  49. $(this).attr('value', 'center');
  50. } else {
  51. $(this).attr('value', 'left');
  52. }
  53. });
  54. });
  55. };
  56. // Delete block
  57. $( ".mega-toggle_blocks").on('click', 'a.mega-delete', function(e) {
  58. e.preventDefault();
  59. $(this).parent(".block-settings").parent(".block").remove();
  60. reindex_blocks();
  61. });
  62. // Show/hide block settings
  63. $( '.mega-toggle_blocks').on('click', '.block-title', function(e) {
  64. e.preventDefault();
  65. e.stopPropagation();
  66. var settings = $(this).parent().find(".block-settings");
  67. $(".block").removeClass('mega-open');
  68. if ( settings.is(":visible") ) {
  69. settings.parent().removeClass('mega-open');
  70. settings.hide();
  71. } else {
  72. settings.parent().addClass('mega-open');
  73. $(".block-settings").hide();
  74. settings.show();
  75. }
  76. });
  77. // Add block to designer
  78. $( "#toggle-block-selector").on('change', function() {
  79. var selected_option = $("#toggle-block-selector").find(":selected");
  80. var val = selected_option.attr('value');
  81. if (val == 'title') {
  82. return;
  83. }
  84. $.ajax({
  85. type: 'POST',
  86. url: ajaxurl,
  87. data: {
  88. action: "mm_get_toggle_block_" + val,
  89. _wpnonce: megamenu.nonce
  90. },
  91. cache: false,
  92. success: function(response) {
  93. var $response = $(response);
  94. // initiate color picker fields
  95. $(".mm_colorpicker", $response).spectrum({
  96. preferredFormat: "rgb",
  97. showInput: true,
  98. showAlpha: true,
  99. clickoutFiresChange: true,
  100. change: function(color) {
  101. if (color.getAlpha() === 0) {
  102. $(this).siblings('div.chosen-color').html('transparent');
  103. } else {
  104. $(this).siblings('div.chosen-color').html(color.toRgbString());
  105. }
  106. }
  107. });
  108. // initiate icon selector dropdowns
  109. $('.icon_dropdown', $response).select2({
  110. containerCssClass: 'tpx-select2-container select2-container-sm',
  111. dropdownCssClass: 'tpx-select2-drop',
  112. minimumResultsForSearch: -1,
  113. formatResult: function(icon) {
  114. return '<i class="dashicons ' + $(icon.element).attr('data-class') + '"></i>';
  115. },
  116. formatSelection: function (icon) {
  117. return '<i class="dashicons ' + $(icon.element).attr('data-class') + '"></i>';
  118. }
  119. });
  120. // add the block
  121. $(".mega-blocks .mega-left").append($response);
  122. // reinded blocks
  123. reindex_blocks();
  124. // reset the select dropdown
  125. $("#toggle-block-selector").val("title");
  126. $('body').trigger('toggle_block_content_loaded');
  127. }
  128. });
  129. });
  130. });