PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.3.1
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.3.1
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
insert-php / admin / assets / js / view-opt.js

view-opt.js in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.3.1, at admin/assets/js/view-opt.js

1,196 lines 31.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 if( !window.winp ) {
2 window.winp = {};
3 }
4
5 (function($) {
6
7 /**
8 * Condition Editor
9 */
10 $.widget("winp.winpConditionEditor", {
11
12 options: {
13 filters: null
14 },
15
16 _create: function() {
17 var self = this;
18
19 this._counter = 0;
20
21 this._$editor = this.element;
22 this._$editor.data('winp-api', this);
23
24 this._$filters = this._$editor.find(".winp-filters");
25 this._$tmplFilter = this._$editor.find(".winp-filter.winp-template").clone().removeClass("winp-template");
26
27 this._$btnAdd = this._$editor.find(".winp-add-filter");
28
29 this._$btnAdd.click(function() {
30 self.addFilter();
31 return false;
32 });
33
34 this._$editor.on('winp.filters-changed', function() {
35 self._checkIsEmpty();
36 self._checkIsDeleted();
37 });
38
39 this._load();
40
41 this._checkIsEmpty();
42 },
43
44 _load: function() {
45
46 if( this.options.filters ) {
47 for( var index in this.options.filters ) {
48 this.addFilter(this.options.filters[index]);
49 }
50 }
51 },
52
53 _checkIsEmpty: function() {
54
55 if( this.getCount() === 0 ) {
56 this._$editor.addClass('winp-empty');
57 } else {
58 this._$editor.removeClass('winp-empty');
59 }
60 },
61
62 _checkIsDeleted: function() {
63
64 if( this.getCount() === 0 ) {
65 this.markChangeFilters();
66 }
67 },
68
69 markChangeFilters: function() {
70 this._$editor.find("#winp_changed_filters").val(1);
71 },
72
73 addFilter: function(data) {
74 if( !data ) {
75 data = {type: 'showif'};
76 }
77
78 var self = this;
79
80 var $filter = this._$tmplFilter.clone();
81 this._$filters.append($filter);
82
83 $filter.data('winp-editor', this._$editor);
84
85 this._counter = this._counter + 1;
86
87 $filter.winpConditionFilter({
88 index: self._counter,
89 type: data.type,
90 conditions: data.conditions
91 });
92
93 self._$editor.trigger('winp.filters-changed');
94 return $filter;
95 },
96
97 getData: function() {
98 var filters = [];
99
100 this._$filters.find(".winp-filter").each(function() {
101 var definition = $(this).winpConditionFilter('getData');
102 filters.push(definition);
103 });
104
105 return filters;
106 },
107
108 getCount: function() {
109 return this._$editor.find('.winp-filter:not(.winp-template)').length;
110 }
111 });
112
113 /**
114 * Condition Filter
115 */
116 $.widget("winp.winpConditionFilter", {
117
118 options: {
119 type: 'showif',
120 conditions: null,
121 index: null
122 },
123
124 _create: function() {
125 var self = this;
126
127 this._counter = 0;
128 this._index = this.options.index;
129
130 this._$filter = this.element;
131 this._$filter.data('winp-api', this);
132
133 this._$editor = this._$filter.data('winp-editor');
134 this._$conditions = this._$filter.find(".winp-conditions");
135
136 this._$tmplCondition = this._$editor.find(".winp-condition.winp-template").clone().removeClass("winp-template");
137 this._$tmplScope = this._$editor.find(".winp-scope.winp-template").clone().removeClass("winp-template");
138
139 this._load();
140
141 this._$filter.find(".winp-link-add").click(function() {
142 self.addCondition();
143 return false;
144 });
145
146 this._$filter.find(".btn-remove-filter").click(function() {
147 self._$filter.remove();
148 self._$editor.trigger('winp.filters-changed');
149 return false;
150 });
151
152 this._$filter.find(".winp-btn-apply-template").click(function() {
153 var templateName = $(".winp-select-template").val();
154
155 if( templateName ) {
156 var data = self.getTemplateData(templateName);
157 if( data ) {
158 self.setFilterData(data);
159 }
160 }
161
162 return false;
163 });
164
165 this._$filter.on('winp.conditions-changed', function() {
166 self._checkIsEmpty();
167 });
168 },
169
170 _load: function() {
171
172 if( !this.options.conditions ) {
173 this.addCondition();
174 } else {
175 this.setFilterData(this.options);
176 }
177 },
178
179 setFilterData: function(data) {
180
181 this._$filter.find('.winp-condition').remove();
182
183 if( data.conditions ) {
184 for( var index in data.conditions ) {
185 this.addCondition(data.conditions[index]);
186 }
187 }
188
189 this._$filter.find(".winp-filter-type").val(data.type);
190 this._checkIsEmpty();
191 },
192
193 _checkIsEmpty: function() {
194
195 if( this.getCount() === 0 ) {
196 this._$filter.addClass('winp-empty');
197 } else {
198 this._$filter.removeClass('winp-empty');
199 }
200
201 this._$conditions.find('.winp-scope').each(function() {
202 var count = $(this).find('.winp-condition').length;
203 if( count === 0 ) {
204 $(this).remove();
205 }
206 });
207 },
208
209 addCondition: function(data, $scope) {
210 if( !data ) {
211 data = {type: 'condition'};
212 }
213
214 if( data.type === 'scope' ) {
215 this.addScope(data);
216 } else if( data.type === 'condition' && !$scope ) {
217 var $scope = this.addScope();
218 this.addCondition(data, $scope);
219 } else {
220
221 var $condition = this._$tmplCondition.clone();
222 $scope.append($condition);
223
224 $condition.data('winp-scope', $scope);
225 $condition.data('winp-editor', this._$editor);
226 $condition.data('winp-filter', this._$filter);
227
228 this._counter = this._counter + 1;
229 data.index = this._index + '_' + this._counter;
230
231 $condition.winpCondition(data);
232 this._$filter.trigger('winp.conditions-changed');
233 }
234 },
235
236 addScope: function(data) {
237 if( !data ) {
238 data = {};
239 }
240
241 var $scope = this._$tmplScope.clone();
242 this._$conditions.append($scope);
243
244 if( data && data.conditions ) {
245 for( var index in data.conditions ) {
246 this.addCondition(data.conditions[index], $scope);
247 }
248 }
249
250 return $scope;
251 },
252
253 getData: function() {
254 var scopes = [];
255
256 this._$conditions.find('.winp-scope').each(function() {
257
258 var scope = {
259 type: 'scope',
260 conditions: []
261 };
262
263 scopes.push(scope);
264
265 $(this).find('.winp-condition').each(function() {
266 var condition = $(this).winpCondition('getData');
267 scope.conditions.push(condition);
268 });
269 });
270
271 var filterType = this._$filter.find(".winp-filter-type").val();
272
273 return {
274 conditions: scopes,
275 type: filterType
276 };
277 },
278
279 getCount: function() {
280 return this._$filter.find('.winp-condition').length;
281 },
282
283 getTemplateData: function(paramName) {
284 if( !window.winp ) {
285 return;
286 }
287 if( !window.winp.templates ) {
288 return;
289 }
290
291 for( var index in window.winp.templates ) {
292 var data = window.winp.templates[index];
293 if( data['id'] === paramName ) {
294 return data['filter'];
295 }
296 }
297
298 return false;
299 }
300 });
301
302 /**
303 * Condition
304 */
305 $.widget("winp.winpCondition", {
306
307 options: {
308 index: null,
309 operator: 'equals'
310 },
311
312 _create: function() {
313 this._index = this.options.index;
314
315 this._$condition = this.element;
316 this._$condition.data('winp-condition', this);
317
318 this._$editor = this._$condition.data('winp-editor');
319 this._$filter = this._$condition.data('winp-filter');
320 this._$scope = this._$condition.data('winp-scope');
321
322 this._editor = this._$editor.data('winp-api');
323 this._filter = this._$filter.data('winp-api');
324
325 this._$hint = this.element.find(".winp-hint");
326 this._$hintContent = this.element.find(".winp-hint-content");
327
328 this._$tmplDateControl = this._$editor.find(".winp-date-control.winp-template").clone().removeClass("winp-template");
329 },
330
331 _init: function() {
332 var self = this;
333
334 this._$condition.find(".winp-param-select").change(function() {
335 self.prepareFields();
336 });
337 self.prepareFields(true);
338
339 // buttons
340
341 this._$condition.find(".winp-btn-remove").click(function() {
342 self._editor.markChangeFilters();
343 self.remove();
344 return false;
345 });
346
347 this._$condition.find(".winp-btn-or").click(function() {
348 self._editor.markChangeFilters();
349 self._filter.addCondition(null, self._$scope);
350 return false;
351 });
352
353 this._$condition.find(".winp-btn-and").click(function() {
354 self._editor.markChangeFilters();
355 self._filter.addCondition();
356 return false;
357 });
358 },
359
360 remove: function() {
361 this._$condition.remove();
362 this._$filter.trigger('winp.conditions-changed');
363 },
364
365 getData: function() {
366
367 var currentParam = this._$condition.find(".winp-param-select").val();
368 var paramOptions = this.getParamOptions(currentParam);
369
370 var $operator = this._$condition.find(".winp-operator-select");
371 var currentOperator = $operator.val();
372
373 var value = null;
374
375 if( 'select' === paramOptions['type'] ) {
376 value = this.getSelectValue(paramOptions);
377 } else if( 'date' === paramOptions['type'] ) {
378 value = this.getDateValue(paramOptions);
379 } else if( 'date-between' === paramOptions['type'] ) {
380 value = this.getDateBetweenValue(paramOptions);
381 } else if( 'integer' === paramOptions['type'] ) {
382 value = this.getIntegerValue(paramOptions);
383 } else {
384 value = this.getTextValue(paramOptions);
385 }
386
387 return {
388 param: currentParam,
389 operator: currentOperator,
390 type: paramOptions['type'],
391 value: value
392 };
393 },
394
395 prepareFields: function(isInit) {
396 var self = this;
397
398 if( isInit && this.options.param ) {
399 this.selectParam(this.options.param);
400 }
401
402 var currentParam = this._$condition.find(".winp-param-select").val();
403 var paramOptions = this.getParamOptions(currentParam);
404
405 this.setParamHint(paramOptions.description);
406
407 var operators = [];
408
409 if( 'select' === paramOptions['type'] || paramOptions['onlyEquals'] ) {
410 operators = ['equals', 'notequal'];
411 } else if( 'date' === paramOptions['type'] ) {
412 operators = ['equals', 'notequal', 'younger', 'older', 'between'];
413 } else if( 'date-between' === paramOptions['type'] ) {
414 operators = ['between'];
415 } else if( 'integer' === paramOptions['type'] ) {
416 operators = ['equals', 'notequal', 'less', 'greater', 'between'];
417 } else {
418 operators = ['equals', 'notequal', 'contains', 'notcontain'];
419 }
420
421 this.setOperators(operators);
422
423 if( isInit && this.options.operator ) {
424 this.selectOperator(this.options.operator);
425 } else {
426 this.selectFirstOperator();
427 }
428
429 this.createValueControl(paramOptions, isInit);
430 },
431
432 /**
433 * Displays and configures the param hint.
434 */
435 setParamHint: function(description) {
436
437 if( description ) {
438 this._$hintContent.html(description);
439 this._$hint.show();
440 } else {
441 this._$hint.hide();
442 }
443 },
444
445 /**
446 * Creates control to specify value.
447 */
448 createValueControl: function(paramOptions, isInit) {
449
450 if( 'select' === paramOptions['type'] ) {
451 this.createValueAsSelect(paramOptions, isInit);
452 } else if( 'date' === paramOptions['type'] ) {
453 this.createValueAsDate(paramOptions, isInit);
454 } else if( 'date-between' === paramOptions['type'] ) {
455 this.createValueAsDateBetween(paramOptions, isInit);
456 } else if( 'integer' === paramOptions['type'] ) {
457 this.createValueAsInteger(paramOptions, isInit);
458 } else {
459 this.createValueAsText(paramOptions, isInit);
460 }
461 },
462
463 // -------------------
464 // Select Control
465 // -------------------
466
467 /**
468 * Creates the Select control.
469 */
470 createValueAsSelect: function(paramOptions, isInit) {
471 var self = this;
472
473 var createSelect = function(values) {
474 var $select = self.createSelect(values);
475 self.insertValueControl($select);
476 if( isInit && self.options.value ) {
477 self.setSelectValue(self.options.value);
478 }
479 self._$condition.find(".winp-value").trigger("insert.select");
480 };
481
482 if( !paramOptions['values'] ) {
483 return;
484 }
485 if( 'ajax' === paramOptions['values']['type'] ) {
486
487 var $fakeSelect = self.createSelect([
488 {
489 value: null,
490 title: '- loading -'
491 }
492 ]);
493 self.insertValueControl($fakeSelect);
494
495 $fakeSelect.attr('disabled', 'disabled');
496 $fakeSelect.addClass('winp-fake-select');
497
498 if( isInit && this.options.value ) {
499 $fakeSelect.data('value', this.options.value);
500 }
501
502 var req = $.ajax({
503 url: window.ajaxurl,
504 method: 'post',
505 data: {
506 action: paramOptions['values']['action'],
507 snippet_id: $('#post_ID').val(),
508 _wpnonce: $('#wbcr_inp_snippet_conditions_metabox_nonce').val()
509 },
510 dataType: 'json',
511 success: function(data) {
512
513 if( data.error ) {
514 self.advancedOptions.showError(data.error);
515 return;
516 } else if( !data.values ) {
517 self.advancedOptions.showError(req.responseText);
518 return;
519 }
520
521 createSelect(data.values);
522 },
523 error: function() {
524 self.advancedOptions.showError('Unexpected error during the ajax request.');
525 },
526 complete: function() {
527 if( $fakeSelect ) {
528 $fakeSelect.remove();
529 }
530 $fakeSelect = null;
531 }
532 });
533 } else {
534 createSelect(paramOptions['values']);
535 }
536 },
537
538 /**
539 * Returns a value for the select control.
540 */
541 getSelectValue: function() {
542 var $select = this._$condition.find(".winp-value select");
543
544 var value = $select.val();
545 if( !value ) {
546 value = $select.data('value');
547 }
548 return value;
549 },
550
551 /**
552 * Sets a select value.
553 */
554 setSelectValue: function(value) {
555 var $select = this._$condition.find(".winp-value select");
556
557 if( $select.hasClass('.winp-fake-select') ) {
558 $select.data('value', value);
559 } else {
560 $select.val(value);
561 }
562 },
563
564 // -------------------
565 // Date Control
566 // -------------------
567
568 /**
569 * Creates a control for the input linked with the date.
570 */
571 createValueAsDate: function(paramOptions, isInit) {
572
573 var $operator = this._$condition.find(".winp-operator-select");
574 var $control = this._$tmplDateControl.clone();
575
576 $operator.change(function() {
577 var currentOperator = $operator.val();
578
579 if( 'between' === currentOperator ) {
580 $control.addClass('winp-between');
581 $control.removeClass('winp-solo');
582 } else {
583 $control.addClass('winp-solo');
584 $control.removeClass('winp-between');
585 }
586
587 });
588
589 $operator.change();
590
591 var $radioes = $control.find(".winp-switcher input")
592 .attr('name', 'winp_switcher_' + this._index)
593 .click(function() {
594 var value = $control.find(".winp-switcher input:checked").val();
595 if( 'relative' === value ) {
596 $control.addClass('winp-relative');
597 $control.removeClass('winp-absolute');
598 } else {
599 $control.addClass('winp-absolute');
600 $control.removeClass('winp-relative');
601 }
602 });
603
604 $control.find(".winp-absolute-date input[type='text']").datepicker({
605 format: 'dd.mm.yyyy',
606 todayHighlight: true,
607 autoclose: true
608 });
609
610 this.insertValueControl($control);
611 if( isInit && this.options.value ) {
612 this.setDateValue(this.options.value);
613 }
614 },
615
616 /**
617 * Returns a value for the Date control.
618 * @returns {undefined}
619 */
620 getDateValue: function() {
621 var value = {};
622
623 var $operator = this._$condition.find(".winp-operator-select");
624 var currentOperator = $operator.val();
625
626 var $control = this._$condition.find(".winp-value > .winp-date-control");
627 var $holder = this._$condition.find(".winp-value > .winp-date-control");
628
629 if( 'between' === currentOperator ) {
630 $holder = $holder.find(".winp-between-date");
631 value.range = true;
632
633 value.start = {};
634 value.end = {};
635
636 if( $control.hasClass('winp-relative') ) {
637 $holder = $holder.find(".winp-relative-date");
638
639 value.start.unitsCount = $holder.find(".winp-date-value-start").val();
640 value.end.unitsCount = $holder.find(".winp-date-value-end").val();
641
642 value.start.units = $holder.find(".winp-date-start-units").val();
643 value.end.units = $holder.find(".winp-date-end-units").val();
644
645 value.start.type = 'relative';
646 value.end.type = 'relative';
647
648 } else {
649 $holder = $holder.find(".winp-absolute-date");
650
651 value.start = $holder.find(".winp-date-value-start").datepicker('getUTCDate').getTime();
652 value.end = $holder.find(".winp-date-value-end").datepicker('getUTCDate').getTime();
653 value.end = value.end + (((23 * 60 * 60) + (59 * 60) + 59) * 1000) + 999;
654 }
655
656 } else {
657 $holder = $holder.find(".winp-solo-date");
658 value.range = false;
659
660 if( $control.hasClass('winp-relative') ) {
661 $holder = $holder.find(".winp-relative-date");
662
663 value.type = 'relative';
664 value.unitsCount = $holder.find(".winp-date-value").val();
665 value.units = $holder.find(".winp-date-value-units").val();
666
667 } else {
668 $holder = $holder.find(".winp-absolute-date");
669 value = $holder.find("input[type='text']").datepicker('getUTCDate').getTime();
670
671 if( 'older' === currentOperator ) {
672 value = value + (((23 * 60 * 60) + (59 * 60) + 59) * 1000) + 999;
673 }
674 }
675 }
676
677 return value;
678 },
679
680 /**
681 * Sets a select value.
682 */
683 setDateValue: function(value) {
684 if( !value ) {
685 value = {};
686 }
687
688 var $holder = this._$condition.find(".winp-value > .winp-date-control");
689 var $control = this._$condition.find(".winp-value > .winp-date-control");
690
691 if( value.range ) {
692
693 if( 'relative' === value.start.type ) {
694 $holder = $holder.find(".winp-relative-date");
695
696 $holder.find(".winp-date-value-start").val(value.start.unitsCount);
697 $holder.find(".winp-date-value-end").val(value.end.unitsCount);
698 $holder.find(".winp-date-start-units").val(value.start.units);
699 $holder.find(".winp-date-end-units").val(value.end.units);
700
701 } else {
702 $holder = $holder.find(".winp-absolute-date");
703
704 var start = new Date(value.start);
705 var end = new Date(value.end);
706
707 $holder.find(".winp-date-value-start").datepicker('setUTCDate', start);
708 $holder.find(".winp-date-value-end").datepicker('setUTCDate', end);
709 }
710
711 } else {
712
713 if( 'relative' === value.type ) {
714 $holder = $holder.find(".winp-relative-date");
715
716 $holder.find(".winp-date-value").val(value.unitsCount);
717 $holder.find(".winp-date-value-units").val(value.units);
718
719 } else {
720 $holder = $holder.find(".winp-absolute-date");
721
722 var date = new Date(value);
723 $holder.find(".winp-date-value").datepicker('setUTCDate', date);
724 }
725 }
726
727 var $relative = $control.find(".winp-switcher input[value=relative]");
728 var $absolute = $control.find(".winp-switcher input[value=absolute]");
729
730 if( 'relative' === value.type || (value.start && 'relative' === value.start.type) ) {
731 $relative.attr('checked', 'checked');
732 $relative.click();
733 } else {
734 $absolute.attr('checked', 'checked');
735 $absolute.click();
736 }
737 },
738
739 // -------------------
740 // Date Between Control
741 // -------------------
742
743 /**
744 * Creates a control for the input linked with the date between.
745 */
746 createValueAsDateBetween: function(paramOptions, isInit) {
747 this._$condition.find('.winp-operator-select').hide();
748 var $control = this._$tmplDateControl.clone();
749 $control.addClass('winp-between');
750 $control.removeClass('winp-solo');
751 $control.addClass('winp-absolute');
752 $control.removeClass('winp-relative');
753
754 $control.find('.winp-switcher input').attr('name', 'winp_switcher_' + this._index);
755 $control.find('.winp-switcher').hide();
756
757 $control.find('.winp-absolute-date input[type=\'text\']').datepicker({
758 format: 'dd.mm.yyyy',
759 todayHighlight: true,
760 autoclose: true
761 }).attr('readonly', false);
762
763 this.insertValueControl($control);
764 if( isInit && this.options.value ) {
765 this.setDateBetweenValue(this.options.value);
766 }
767 },
768
769 /**
770 * Returns a value for the Date Between control.
771 * @returns {undefined}
772 */
773 getDateBetweenValue: function() {
774 var value = {};
775
776 var $holder = this._$condition.find(".winp-value > .winp-date-control");
777
778 $holder = $holder.find(".winp-between-date");
779 value.range = true;
780
781 value.start = {};
782 value.end = {};
783
784 $holder = $holder.find(".winp-absolute-date");
785
786 value.start = $holder.find(".winp-date-value-start").datepicker('getUTCDate').getTime();
787 value.end = $holder.find(".winp-date-value-end").datepicker('getUTCDate').getTime();
788 value.end = value.end + (((23 * 60 * 60) + (59 * 60) + 59) * 1000) + 999;
789
790 return value;
791 },
792
793 /**
794 * Sets a select value.
795 */
796 setDateBetweenValue: function(value) {
797 if( !value ) {
798 value = {};
799 }
800
801 var $holder = this._$condition.find(".winp-value > .winp-date-control");
802 var $control = this._$condition.find(".winp-value > .winp-date-control");
803
804 $holder = $holder.find(".winp-absolute-date");
805
806 var start = new Date(value.start);
807 var end = new Date(value.end);
808
809 $holder.find(".winp-date-value-start").datepicker('setUTCDate', start);
810 $holder.find(".winp-date-value-end").datepicker('setUTCDate', end);
811
812 var $absolute = $control.find(".winp-switcher input[value=absolute]");
813
814 $absolute.attr('checked', 'checked');
815 $absolute.click();
816 },
817
818 // -------------------
819 // Integer Control
820 // -------------------
821
822 /**
823 * Creates a control for the input linked with the integer.
824 */
825 createValueAsInteger: function(paramOptions, isInit) {
826 var self = this;
827
828 var $operator = this._$condition.find(".winp-operator-select");
829
830 $operator.on('change', function() {
831 var currentOperator = $operator.val();
832
833 var $control;
834 if( 'between' === currentOperator ) {
835 $control = $("<span><input type='text' class='winp-integer-start' /> and <input type='text' class='winp-integer-end' /></span>");
836 } else {
837 $control = $("<input type='text' class='winp-integer-solo' /></span>");
838 }
839
840 self.insertValueControl($control);
841 });
842
843 $operator.change();
844 if( isInit && this.options.value ) {
845 this.setIntegerValue(this.options.value);
846 }
847 },
848
849 /**
850 * Returns a value for the Integer control.
851 */
852 getIntegerValue: function() {
853 var value = {};
854
855 var $operator = this._$condition.find(".winp-operator-select");
856 var currentOperator = $operator.val();
857
858 if( 'between' === currentOperator ) {
859 value.range = true;
860 value.start = this._$condition.find(".winp-integer-start").val();
861 value.end = this._$condition.find(".winp-integer-end").val();
862
863 } else {
864 value = this._$condition.find(".winp-integer-solo").val();
865 }
866
867 return value;
868 },
869
870 /**
871 * Sets a value for the Integer control.
872 */
873 setIntegerValue: function(value) {
874 if( !value ) {
875 value = {};
876 }
877
878 if( value.range ) {
879 this._$condition.find(".winp-integer-start").val(value.start);
880 this._$condition.find(".winp-integer-end").val(value.end);
881 } else {
882 this._$condition.find(".winp-integer-solo").val(value);
883 }
884 },
885
886 // -------------------
887 // Text Control
888 // -------------------
889
890 /**
891 * Creates a control for the input linked with the integer.
892 */
893 createValueAsText: function(paramOptions, isInit) {
894
895 var $control = $("<input type='text' class='winp-text' /></span>");
896 this.insertValueControl($control);
897 if( isInit && this.options.value ) {
898 this.setTextValue(this.options.value);
899 }
900 },
901
902 /**
903 * Returns a value for the Text control.
904 * @returns {undefined}
905 */
906 getTextValue: function() {
907 return this._$condition.find(".winp-text").val();
908 },
909
910 /**
911 * Sets a value for the Text control.
912 */
913 setTextValue: function(value) {
914 this._$condition.find(".winp-text").val(value);
915 },
916
917 // -------------------
918 // Helper Methods
919 // -------------------
920
921 selectParam: function(value) {
922 this._$condition.find(".winp-param-select").val(value);
923 },
924
925 selectOperator: function(value) {
926 this._$condition.find(".winp-operator-select").val(value);
927 },
928
929 selectFirstOperator: function() {
930 this._$condition.find(".winp-operator-select").prop('selectedIndex', 0);
931 },
932
933 setOperators: function(values) {
934 var $operator = this._$condition.find(".winp-operator-select");
935 $operator.show().off('change');
936
937 $operator.find("option").hide();
938 for( var index in values ) {
939 $operator.find("option[value='" + values[index] + "']").show();
940 }
941 var value = $operator.find("option:not(:hidden):eq(0)").val();
942 $operator.val(value);
943 },
944
945 insertValueControl: function($control) {
946 this._$condition.find(".winp-value").html("").append($control);
947
948 },
949
950 getParamOptions: function(paramName) {
951 if( !window.winp ) {
952 return;
953 }
954 if( !window.winp.filtersParams ) {
955 return;
956 }
957
958 for( var index in window.winp.filtersParams ) {
959 var paramOptions = window.winp.filtersParams[index];
960 if( paramOptions['id'] === paramName ) {
961 return paramOptions;
962 }
963 }
964
965 return false;
966 },
967
968 createSelect: function(values, attrs) {
969
970 var $select = $("<select></select>");
971 if( attrs ) {
972 $select.attr(attrs);
973 }
974
975 for( var index in values ) {
976 var item = values[index];
977 var $option = '';
978
979 if( typeof index === "string" && isNaN(index) === true ) {
980 var $optgroup = $("<optgroup></optgroup>").attr('label', index);
981
982 for( var subindex in item ) {
983 var subvalue = item[subindex];
984 $option = $("<option></option>").attr('value', subvalue['value']).text(subvalue['title']);
985 $optgroup.append($option);
986 }
987 $select.append($optgroup);
988 } else {
989 $option = $("<option></option>").attr('value', item['value']).text(item['title']);
990 $select.append($option);
991 }
992 }
993
994 return $select;
995 },
996
997 createDataPircker: function() {
998
999 var $control = $('<div class="winp-date-control" data-date="today"></div>');
1000 var $input = $('<input size="16" type="text" readonly="readonly" />');
1001 var $icon = $('<i class="fa fa-calendar"></i>');
1002
1003 $control.append($input);
1004 $control.append($icon);
1005
1006 var $datepicker = $input.datepicker({
1007 autoclose: true,
1008 format: 'dd/mm/yyyy'
1009 });
1010
1011 $control.data('winp-datepicker', $datepicker);
1012
1013 $icon.click(function() {
1014 $input.datepicker('show');
1015 });
1016
1017 $control.on('changeDate', function(ev) {
1018 $input.datepicker('hide');
1019 });
1020
1021 return $control;
1022 }
1023 });
1024
1025 /**
1026 * Visability Options.
1027 */
1028 window.visibilityOptions = {
1029
1030 init: function() {
1031 this.initSwitcher();
1032 this.initSimpleOptions();
1033 this.initAdvancedOptions();
1034 this.initDefaultAction();
1035 },
1036
1037 initSwitcher: function() {
1038 var $buttons = $(".winp-options-switcher .btn");
1039
1040 var selectOptions = function(value) {
1041 if( !value ) {
1042 value = $("#winp_visibility_mode").val();
1043 }
1044
1045 $buttons.removeClass('active');
1046
1047 if( 'simple' === value ) {
1048 $(".winp-options-switcher .btn-btn-simple").addClass('active');
1049 $("#winp-advanced-visibility-options").hide();
1050 $("#winp-simple-visibility-options").fadeIn(300);
1051 } else {
1052 $(".winp-options-switcher .btn-btn-advanced").addClass('active');
1053 $("#winp-simple-visibility-options").hide();
1054 $("#winp-advanced-visibility-options").fadeIn(300);
1055 }
1056
1057 $("#winp_visibility_mode").val(value);
1058 };
1059
1060 $buttons = $(".winp-options-switcher .btn").click(function() {
1061 var value = $(this).data('value');
1062 selectOptions(value);
1063 return false;
1064 });
1065
1066 selectOptions();
1067 },
1068
1069 initSimpleOptions: function() {
1070 $("#winp_relock").change(function() {
1071 if( $(this).is(":checked") ) {
1072 $("#onp-sl-relock-options").hide().removeClass('hide').fadeIn();
1073 } else {
1074 $("#onp-sl-relock-options").hide();
1075 }
1076 });
1077 },
1078
1079 initAdvancedOptions: function() {
1080 var $formPost = $("form#post");
1081 var $hidden = $("#winp_visibility_filters");
1082 var $editor = $("#winp-advanced-visability-options");
1083
1084 // creating an editor
1085 var json_data = $.parseJSON($hidden.val());
1086 $editor.winpConditionEditor({
1087 filters: typeof json_data[0] === 'undefined' ? [] : json_data[0]
1088 });
1089
1090 // saves conditions on clicking the button Save
1091 $formPost.submit(function() {
1092 var data = $editor.winpConditionEditor("getData");
1093 var json = JSON.stringify(data);
1094 $hidden.val(json);
1095
1096 return true;
1097 });
1098 },
1099
1100 // По выбранному параметру "Insertion location" определяем параметры условия
1101 changeConditionValue: function() {
1102 var $editor = $("#winp-advanced-visability-options");
1103 var $condition = $editor.find('.winp-condition').eq(0);
1104 switch( $("#wbcr_inp_snippet_location").val() ) {
1105 case 'before_post':
1106 case 'before_content':
1107 case 'before_paragraph':
1108 case 'after_paragraph':
1109 case 'after_content':
1110 case 'after_post':
1111 $condition.find(".winp-value>select").val('base_sing');
1112 break;
1113 case 'before_excerpt':
1114 case 'after_excerpt':
1115 case 'between_posts':
1116 case 'before_posts':
1117 case 'after_posts':
1118 $condition.find(".winp-value>select").val('base_arch');
1119 break;
1120 default:
1121 $condition.find(".winp-value>select").val('base_web');
1122 }
1123 },
1124
1125 // "Вешаем" события на три select'a. Если юзер и�
1126 меняет,
1127 // то запоминаем это и больше автоматом параметры не меняем
1128 bindTrigger: function() {
1129 var $editor = $("#winp-advanced-visability-options");
1130 var $filter = $editor.find('.winp-filter').eq(0);
1131 $filter.find('select').change(function() {
1132 $editor.find("#winp_changed_filters").val(1);
1133 });
1134 },
1135
1136 // Устанавливаем первое условие и "навешиваем" события на элементы
1137 initDefaultAction: function() {
1138 var $editor = $("#winp-advanced-visability-options");
1139 var $condition = null;
1140 var $select = null;
1141 var self = this;
1142
1143 // Если ни одного условия ещё нет, то создаем его
1144 if( $editor.find("#winp_changed_filters").val() == 0 ) {
1145 if( $(".winp-filter:not(.winp-template)").length == 0 ) {
1146 // Генерируем событие нажатия кнопки Add new condition
1147 $("a.winp-add-filter").trigger('click');
1148 } else {
1149 $select = $("select.winp-param-select").eq(0);
1150 }
1151
1152 // "Вешаем" событие на последний select, который грузится по ajax'у
1153 $condition = $editor.find('.winp-condition').eq(0);
1154 $condition.find(".winp-value").on("insert.select", function() {
1155 if( $editor.find("#winp_changed_filters").val() == 0 ) {
1156 if( $select == null ) {
1157 $select = $("select.winp-param-select").eq(0);
1158 $select.val('location-some-page').trigger('change');
1159 }
1160
1161 self.bindTrigger();
1162 self.changeConditionValue();
1163 }
1164 });
1165 }
1166
1167 // Если изменили один из дву�
1168 параметров (scope или location),
1169 // то при необ�
1170 одимости параметры условия устанавливаем автоматом
1171 $("#wbcr_inp_snippet_scope, #wbcr_inp_snippet_location").change(function() {
1172 if( $editor.find("#winp_changed_filters").val() == 0 && $select != null ) {
1173 // Если первый параметр условия уже установлен
1174 if( 'location-some-page' == $select.val() ) {
1175 if( 'auto' == $("#wbcr_inp_snippet_scope").val() ) {
1176 self.changeConditionValue();
1177 } else {
1178 $condition.find(".winp-value>select").val('base_web');
1179 }
1180 } else {
1181 $select.val('location-some-page').trigger('change');
1182 }
1183 }
1184 });
1185
1186 $editor.find("select.winp-filter-type").change(function() {
1187 $editor.find("#winp_changed_filters").val(1);
1188 });
1189 }
1190 };
1191
1192 $(function() {
1193 window.visibilityOptions.init();
1194 });
1195
1196 })(jQuery);