-
Notifications
You must be signed in to change notification settings - Fork 9
/
gk-nsp-admin.js
288 lines (256 loc) · 10.9 KB
/
gk-nsp-admin.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* GK News Show Pro
*
* @version: 1.0.0
* @date: 22-03-2014
* @desc: Complex widget for displaying WordPress posts, WooCommerce products, XML/JSON file content and RSS feeds.
* @author: GavickPro
* @email: [email protected]
*
*/
function GK_NSP_UI() {
// properties
var wrapper = null;
// private methods
var initUI = function() {
// togglers
var togglers = wrapper.find('.gk-toggler');
togglers.each(function(i, toggler) {
toggler = jQuery(toggler);
toggler.click(function() {
if (!toggler.hasClass('active')) {
togglers.each(function(j, el) {
el = jQuery(el);
if (i !== j) {
el.removeClass('active');
}
});
toggler.addClass('active');
} else {
toggler.removeClass('active');
}
});
});
// data source config
var data_source_type = wrapper.find('.gk-data-source-type');
var depends = {};
var selected = data_source_type.val();
jQuery.each(
[
wrapper.find('.gk-data-source'),
wrapper.find('.gk-one-per-category'),
wrapper.find('.gk-order-by'),
wrapper.find('.gk-order'),
wrapper.find('.gk-offset'),
wrapper.find('.gk-xml-filelist'),
wrapper.find('.gk-json-filelist'),
wrapper.find('.gk-wp-category-list'),
wrapper.find('.gk-woocommerce-category-list'),
wrapper.find('.gk-post-types-list')
],
function(i, item) {
depends[item.attr('class')] = item.attr('data-depends').split(',');
if (depends[item.attr('class')].indexOf(data_source_type.val()) === -1) {
if (item.attr('class') === 'gk-order') {
item.css('display', 'none');
} else {
item.parent().css('display', 'none');
}
}
if (selected.indexOf('-') !== -1) {
selected = selected.substr(0, selected.indexOf('-'));
}
}
);
//
wrapper.find('.gk-article-wrapper-selector option').each(function(i, item) {
item = jQuery(item);
var support = item.attr('data-support') === 'all' ? false : item.attr('data-support').split(',');
if (support) {
if (support.indexOf(selected) === -1) {
item.attr('disabled', 'disabled');
} else {
item.removeAttr('disabled');
}
}
});
data_source_type.change(function() {
jQuery.each(
[
wrapper.find('.gk-data-source'),
wrapper.find('.gk-one-per-category'),
wrapper.find('.gk-order-by'),
wrapper.find('.gk-order'),
wrapper.find('.gk-offset'),
wrapper.find('.gk-json-filelist'),
wrapper.find('.gk-xml-filelist'),
wrapper.find('.gk-wp-category-list'),
wrapper.find('.gk-woocommerce-category-list'),
wrapper.find('.gk-post-types-list')
],
function(i, item) {
var state = depends[item.attr('class')].indexOf(data_source_type.val()) === -1 ? 'none' : 'block';
if (item.attr('class') === 'gk-order') {
item.css('display', state === 'block' ? 'inline' : 'none');
} else {
item.parent().css('display', state);
}
}
);
var selected = data_source_type.val();
if (selected.indexOf('-') !== -1) {
selected = selected.substr(0, selected.indexOf('-'));
}
wrapper.find('.gk-article-wrapper-selector option').each(function(i, item) {
item = jQuery(item);
var support = item.attr('data-support') === 'all' ? false : item.attr('data-support').split(',');
if (support) {
if (support.indexOf(selected) === -1) {
item.attr('disabled', 'disabled');
} else {
item.removeAttr('disabled');
}
}
});
});
// reordering
jQuery.each(['title', 'image', 'text', 'info', 'readmore'], function(iter, item) {
var el = wrapper.find('.gk-article-' + item + '-order');
el.change(function() {
changeOrder(el);
});
el.blur(function() {
changeOrder(el);
});
});
// hiding article wrapper unnecessary elements
if (wrapper.find('.gk-article-wrapper-selector').val() !== 'default') {
wrapper.find('.gk-article-wrapper-hide').css('display', 'none');
wrapper.find('*[data-aw]').css('display', 'none');
wrapper.find('*[data-aw="' + wrapper.find('.gk-article-wrapper-selector').val() + '"]').css('display', 'block');
} else {
wrapper.find('.gk-article-wrapper-hide').css('display', 'block');
wrapper.find('*[data-aw]').css('display', 'none');
}
wrapper.find('.gk-article-wrapper-selector').change(function() {
if (wrapper.find('.gk-article-wrapper-selector').val() !== 'default') {
wrapper.find('.gk-article-wrapper-hide').css('display', 'none');
wrapper.find('*[data-aw]').css('display', 'none');
wrapper.find('*[data-aw="' + wrapper.find('.gk-article-wrapper-selector').val() + '"]').css('display', 'block');
} else {
wrapper.find('.gk-article-wrapper-hide').css('display', 'block');
wrapper.find('*[data-aw]').css('display', 'none');
}
});
// reorder elements on start
var elements_list = wrapper.find('.gk-article-elements');
var elements_list_items = [
elements_list.find('.gk-article-element[data-sort-pos="1"]'),
elements_list.find('.gk-article-element[data-sort-pos="2"]'),
elements_list.find('.gk-article-element[data-sort-pos="3"]'),
elements_list.find('.gk-article-element[data-sort-pos="4"]'),
elements_list.find('.gk-article-element[data-sort-pos="5"]')
];
for(var num = 1; num <= 5; num++) {
var el = jQuery(elements_list_items[num]);
var x = el.attr('data-sort-pos');
if(x === 1) {
el.insertBefore(elements_list.find('.gk-article-element[data-sort-pos="2"]'));
} else {
el.insertAfter(elements_list.find('.gk-article-element[data-sort-pos="'+(x - 1)+'"]'));
}
}
// adding sortable UI to article elements
elements_list.sortable({
update: function(event, ui) {
wrapper.find('.gk-article-element').each(function(i, el) {
wrapper.find('.gk-article-'+jQuery(el).attr('data-element-name')+'-order').val(i+1);
});
}
});
};
var changeOrder = function(current) {
var unexisting = [false, false, false, false, false];
var searched = 0;
var elms = ['title', 'image', 'text', 'info', 'readmore'].map(function(item) {
return wrapper.find('.gk-article-' + item + '-order');
});
jQuery.each(elms, function(iter, item) {
unexisting[item.val() - 1] = true;
});
for (var i = 0; i < 5; i++) {
if (unexisting[i] === false) {
searched = i + 1;
}
}
jQuery.each(elms, function(iter, item) {
if (item.attr('class') !== current.attr('class') && item.val() === current.val()) {
item.val(searched);
}
});
};
// id of the clicked field
var uploadID = '';
var gkMediaInit = function() {
// image uploaders
jQuery('.gk-nsp-ui .gk-media-input').each(
function (i, el) {
el = jQuery(el);
var btnid = el.attr('id') + '_button';
jQuery('#' + btnid).click(function (event) {
event.preventDefault();
var btn = jQuery(event.target);
var options, attachment;
// if the frame already exists, open it
if ( wp.media.frames.gkNSPImageFrame ) {
wp.media.frames.gkNSPImageFrame.open();
return;
}
// set our settings
wp.media.frames.gkNSPImageFrame = wp.media({
title: 'Choose Image',
multiple: false,
library: {
type: 'image'
},
button: {
text: 'Use This Image'
}
});
wp.media.frames.gkNSPImageFrame.on('close',function() {
// get selections and save to hidden input plus other AJAX stuff etc.
var selection = wp.media.frames.gkNSPImageFrame.state().get('selection');
// loop through the selected files
selection.each( function( attachment ) {
var url = attachment.attributes.url;
jQuery(btn).prev('input').val(url);
});
});
// set up our select handler
wp.media.frames.gkNSPImageFrame.on( 'select', function() {
var selection = wp.media.frames.gkNSPImageFrame.state().get('selection');
if ( ! selection ) {
return;
}
// loop through the selected files
selection.each( function( attachment ) {
var url = attachment.attributes.url;
jQuery(btn).prev('input').val(url);
});
});
// open the frame
wp.media.frames.gkNSPImageFrame.open();
});
}
);
};
// public API
var API = {
init: function(wrap) {
wrapper = wrap;
initUI();
gkMediaInit();
}
};
return API;
}