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