| 1 |
|
| 2 |
var mbResponsive = function ($) |
| 3 |
{ |
| 4 |
this.jquery = $; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
mbResponsive.prototype = { |
| 9 |
jquery : null, |
| 10 |
mbAdmin: null, |
| 11 |
responsiveMap: null, |
| 12 |
} |
| 13 |
|
| 14 |
mbResponsive.prototype.init = function (mbAdmin) |
| 15 |
{ |
| 16 |
$ = this.jquery; |
| 17 |
this.mbAdmin = mbAdmin; |
| 18 |
|
| 19 |
if ($('#new-button-form').length == 0) // Don't load outside button editor |
| 20 |
return; |
| 21 |
|
| 22 |
this.checkAutoQuery(); |
| 23 |
|
| 24 |
$('input[name="auto_responsive"]').on('click', $.proxy(this.checkAutoQuery,this)); |
| 25 |
$('.add_media_query').on('click', $.proxy(this.addMediaQuery, this)); |
| 26 |
|
| 27 |
this.responsiveMap = JSON.parse(responsiveMap); |
| 28 |
|
| 29 |
$(document).on('click', '.removebutton', $.proxy(this.removeMediaQuery, this)); |
| 30 |
//$(document).on('click', '.responsive_preview', $.proxy(this.toggleResponsivePreview, this)); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
mbResponsive.prototype.checkAutoQuery = function() |
| 35 |
{ |
| 36 |
$ = this.jquery; |
| 37 |
|
| 38 |
if ( $('input[name="auto_responsive"]').is(':checked') ) |
| 39 |
{ |
| 40 |
|
| 41 |
$('.media_queries_options').hide(); |
| 42 |
$('.option-design.new-query').hide(); |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
$('.media_queries_options').show(); |
| 47 |
$('.option-design.new-query').show(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
mbResponsive.prototype.addMediaQuery = function() |
| 52 |
{ |
| 53 |
$ = this.jquery; |
| 54 |
|
| 55 |
this.mbAdmin.saveIndicator(true); |
| 56 |
var new_option = $('.media_option_prot').children().clone(); |
| 57 |
|
| 58 |
var new_query = $("#new_query").val(); |
| 59 |
var new_title = $("#new_query :selected").text(); |
| 60 |
var new_desc = $("#media_desc").children('#' + new_query).text(); |
| 61 |
|
| 62 |
$(new_option).data('query', new_query); |
| 63 |
$(new_option).children('input[name="media_query[]"]').val(new_query); |
| 64 |
$(new_option).children('.title').text(new_title); |
| 65 |
$(new_option).children('.description').text(new_desc); |
| 66 |
|
| 67 |
if (new_query == 'custom') |
| 68 |
{ |
| 69 |
|
| 70 |
$(new_option).find('.custom').removeClass('hidden'); |
| 71 |
} |
| 72 |
|
| 73 |
var new_index = $('input[name="next_media_index"]').val(); |
| 74 |
|
| 75 |
// rename new field to the proper ID |
| 76 |
$(new_option).find('label, select, input').each(function () { |
| 77 |
|
| 78 |
var name = $(this).attr('name'); |
| 79 |
var id = $(this).attr('id'); |
| 80 |
var data = $(this).data('field'); |
| 81 |
var tagname = $(this).prop('tagName').toLowerCase(); |
| 82 |
|
| 83 |
if (typeof id !== 'undefined') |
| 84 |
$(this).attr('id', id.replace('[]','[' + new_index + ']')); |
| 85 |
if (typeof name !== 'undefined') |
| 86 |
$(this).attr('name', name.replace('[]','[' + new_index + ']')); |
| 87 |
if (typeof data !== 'undefined') |
| 88 |
$(this).data('field', data.replace('[]','[' + new_index + ']')); |
| 89 |
if (tagname == 'label') |
| 90 |
{ |
| 91 |
var attrfor = $(this).attr('for'); |
| 92 |
if ( typeof attrfor != 'undefined') |
| 93 |
$(this).attr('for', attrfor.replace('[]','[' + new_index + ']')); |
| 94 |
} |
| 95 |
|
| 96 |
}) |
| 97 |
|
| 98 |
$(document).trigger('reinitConditionals'); |
| 99 |
|
| 100 |
new_index = parseInt(new_index); |
| 101 |
|
| 102 |
$('input[name="next_media_index"]').val( (new_index+1) ); |
| 103 |
|
| 104 |
if (new_query !== 'custom') |
| 105 |
{ |
| 106 |
$('#new_query :selected').prop('disabled', true); |
| 107 |
$('#new_query :selected').prop('selected', false); |
| 108 |
} |
| 109 |
|
| 110 |
$('.new_query_space').append(new_option); |
| 111 |
|
| 112 |
var pos = $('.new_query_space').offset().top; |
| 113 |
$(window).scrollTop( (pos-100) ); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
mbResponsive.prototype.removeMediaQuery = function(e) |
| 118 |
{ |
| 119 |
$ = this.jquery; |
| 120 |
var target = e.target; |
| 121 |
|
| 122 |
var query = $(target).parents('.media_query').data('query'); |
| 123 |
$(target).parents('.media_query').fadeOut(function() { $(this).remove() } ); |
| 124 |
|
| 125 |
$('#new_query option[value="' + query + '"]').prop('disabled', false); |
| 126 |
} |
| 127 |
|
| 128 |
mbResponsive.prototype.toggleResponsivePreview = function(e) |
| 129 |
{ |
| 130 |
var target = $(e.target); |
| 131 |
var id = target.attr('id') |
| 132 |
id = id.replace('mq_preview[',''); |
| 133 |
id = id.replace(']', ''); |
| 134 |
this.renderPreview(id); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
mbResponsive.prototype.renderPreview = function (id) |
| 139 |
{ |
| 140 |
var excluded = ['mq_container_width', 'mq_container_float','mq_custom_minwidth','mq_custom_minheight', 'mq_hide']; |
| 141 |
var responsiveMap = this.responsiveMap; |
| 142 |
var self = this; |
| 143 |
|
| 144 |
$.each(responsiveMap, function (index) { |
| 145 |
|
| 146 |
var css = this.css; |
| 147 |
var field_pattern = '[id^="' + index + '["]'; |
| 148 |
var unit_pattern = '[id^="' + index + '_unit["]'; |
| 149 |
var $field = $(field_pattern).eq(id); |
| 150 |
|
| 151 |
var field_id = $field.attr('id'); |
| 152 |
field_id = field_id.replace(/\[[0-9]\]/gi, ''); |
| 153 |
|
| 154 |
if ($.inArray(field_id, excluded) >= 0 ) |
| 155 |
{ |
| 156 |
return true; // continue |
| 157 |
} |
| 158 |
if (field_id.indexOf('_unit') >= 0 ) |
| 159 |
{ |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
var value= $field.val(); |
| 164 |
var unit_val = ''; |
| 165 |
|
| 166 |
if (typeof responsiveMap[index + '_unit'] !== 'undefined') |
| 167 |
{ |
| 168 |
var unit_val = $(unit_pattern).eq(id).val(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
var data = { css: css }; |
| 173 |
if (css == 'font-size') |
| 174 |
{ |
| 175 |
data.csspart = 'mb-text,mb-text2'; |
| 176 |
} |
| 177 |
|
| 178 |
self.mbAdmin.putCSS(data, value + unit_val) |
| 179 |
|
| 180 |
|
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|