Нет описания

admin-theme-editor.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*******************************************************************************
  2. * Copyright (c) 2019, Code Atlantic LLC
  3. ******************************************************************************/
  4. /* global PUM_Admin, jQuery, pum_google_fonts, pum_theme_settings_editor */
  5. (function ($) {
  6. "use strict";
  7. window.PUM_Admin = window.PUM_Admin || {};
  8. window.pum_theme_settings_editor = window.pum_theme_settings_editor || {
  9. form_args: {},
  10. current_values: {}
  11. };
  12. PUM_Admin.themeEditor = {
  13. preview_fixed_scroll: function () {
  14. var $preview = $('#pum_theme_preview'),
  15. $parent = $preview.parent(),
  16. startscroll = $preview.offset().top - 50;
  17. $(window).on('scroll', function () {
  18. if ($('> .postbox:visible', $parent).index($preview) === ($('> .postbox:visible', $parent).length - 1) && $(window).scrollTop() >= startscroll) {
  19. $preview.css({
  20. left: $preview.offset().left,
  21. width: $preview.width(),
  22. height: $preview.height(),
  23. position: 'fixed',
  24. top: 50
  25. });
  26. } else {
  27. $preview.removeAttr('style');
  28. }
  29. });
  30. },
  31. update_font_options: function (prefix) {
  32. var $font_family_select = $('select[id="' + prefix + '_font_family"]'),
  33. $font_weight_select = $('select[id="' + prefix + '_font_weight"]'),
  34. $font_style_select = $('select[id="' + prefix + '_font_style"]'),
  35. $font_weight_options = $font_weight_select.find('option'),
  36. $font_style_options = $font_style_select.find('option'),
  37. font_weights = [400, 300, 700, 100, 200, 500, 600, 800, 900],
  38. font_styles = ['', 'italic'],
  39. chosen_font = $font_family_select.val(),
  40. chosen_weight = $font_weight_select.val(),
  41. chosen_style = $font_style_select.val(),
  42. font,
  43. i;
  44. // Google Font Chosen
  45. if (pum_google_fonts[chosen_font] !== undefined) {
  46. font = pum_google_fonts[chosen_font];
  47. $font_weight_options.prop('disabled', true);
  48. $font_style_options.prop('disabled', true);
  49. if (font.variants.length) {
  50. for (i = 0; font.variants.length > i; i += 1) {
  51. if (font.variants[i] === 'regular') {
  52. $('option[value="400"]', $font_weight_select).prop('disabled', false);
  53. $('option[value=""]', $font_style_select).prop('disabled', false);
  54. } else {
  55. if (font.variants[i].indexOf('italic') >= 0) {
  56. $('option[value="italic"]', $font_style_select).prop('disabled', false);
  57. }
  58. $('option[value="' + parseInt(font.variants[i], 10) + '"]', $font_weight_select).prop('disabled', false);
  59. }
  60. }
  61. }
  62. // Standard Font Chosen
  63. } else {
  64. $font_weight_options.prop('disabled', false);
  65. $font_style_options.prop('disabled', false);
  66. }
  67. if (chosen_weight === '' || $font_weight_options.filter('[value="' + chosen_weight + '"]').is(':disabled')) {
  68. for (i = 0; font_weights.length > i; i += 1) {
  69. if (!$font_weight_options.filter('[value="' + font_weights[i] + '"]').is(':disabled')) {
  70. $font_weight_select.val(font_weights[i]);
  71. break;
  72. }
  73. }
  74. }
  75. if ($font_style_options.filter('[value="' + chosen_style + '"]').is(':disabled')) {
  76. for (i = 0; font_styles.length > i; i += 1) {
  77. if (!$font_style_options.filter('[value="' + font_styles[i] + '"]').is(':disabled')) {
  78. $font_style_select.val(font_styles[i]);
  79. break;
  80. }
  81. }
  82. }
  83. // Update Select2 if enabled.
  84. $font_family_select.trigger('change.select2');
  85. $font_weight_select.trigger('change.select2');
  86. $font_family_select.trigger('change.select2');
  87. $font_weight_select = $font_weight_select.parents('.pum-field');
  88. if ($font_weight_options.filter(':not(:disabled)').length > 1) {
  89. $font_weight_select.show();
  90. } else {
  91. $font_weight_select.hide();
  92. }
  93. $font_family_select = $font_style_select.parents('.pum-field');
  94. if ($font_style_options.filter(':not(:disabled)').length > 1) {
  95. $font_family_select.show();
  96. } else {
  97. $font_family_select.hide();
  98. }
  99. },
  100. update_font_selectboxes: function () {
  101. return $('select[id$="_font_family"]').each(function () {
  102. PUM_Admin.themeEditor.update_font_options($(this).attr('id').replace('_font_family', ''));
  103. });
  104. },
  105. update_loaded_font: function (prefix) {
  106. var $font_family_select = $('select[id="' + prefix + '_font_family"]'),
  107. $font_weight_select = $('select[id="' + prefix + '_font_weight"]'),
  108. $font_style_select = $('select[id="' + prefix + '_font_style"]'),
  109. chosen_font = $font_family_select.val(),
  110. chosen_weight = $font_weight_select.val(),
  111. chosen_style = $font_style_select.val(),
  112. $link = $('link#pum-' + prefix + 'google-font'),
  113. $new_link = $('<link id="pum-' + prefix + 'google-font" rel="stylesheet" type="text/css">'),
  114. url;
  115. if (typeof pum_google_fonts[chosen_font] !== 'undefined') {
  116. url = "//fonts.googleapis.com/css?family=" + chosen_font;
  117. if (chosen_weight !== 'normal') {
  118. url += ":" + chosen_weight;
  119. }
  120. if (chosen_style === 'italic') {
  121. if (url.indexOf(':') === -1) {
  122. url += ":";
  123. }
  124. url += "italic";
  125. }
  126. $new_link.attr('href', url);
  127. if ($link.length) {
  128. if ($link.attr('href') !== url) {
  129. $link.replaceWith($new_link);
  130. }
  131. } else {
  132. $('body').append($new_link);
  133. }
  134. } else {
  135. $link.remove();
  136. }
  137. },
  138. update_loaded_fonts: function() {
  139. return $('select[id$="_font_family"]').each(function () {
  140. PUM_Admin.themeEditor.update_loaded_font($(this).attr('id').replace('_font_family', ''));
  141. });
  142. },
  143. refresh_preview: function () {
  144. var form_values = $('#pum-theme-settings-container').pumSerializeObject(),
  145. theme_settings = form_values.theme_settings;
  146. // Remap deprecated settings so they work.
  147. // Remove this after ATB updated.
  148. if (typeof window.PUM_ATB !== 'undefined') {
  149. delete form_values.theme_settings;
  150. for (var old_setting in form_values) {
  151. if (form_values.hasOwnProperty(old_setting))
  152. theme_settings[old_setting.replace('popup_theme_','')] = form_values[old_setting];
  153. }
  154. }
  155. PUM_Admin.themeEditor.restyle_preview(theme_settings);
  156. },
  157. restyle_preview: function (theme) {
  158. var $overlay = $('.pum-popup-overlay'),
  159. $container = $('.pum-popup-container'),
  160. $title = $('.pum-popup-title', $container),
  161. $content = $('.pum-popup-content', $container),
  162. $close = $('.pum-popup-close', $container),
  163. container_inset = theme.container_boxshadow_inset === 'yes' ? 'inset ' : '',
  164. close_inset = theme.close_boxshadow_inset === 'yes' ? 'inset ' : '',
  165. top, left, right, bottom;
  166. PUM_Admin.themeEditor.update_loaded_fonts();
  167. $overlay.removeAttr('style').css({
  168. backgroundColor: window.PUM_Admin.utils.convert_hex(theme.overlay_background_color, theme.overlay_background_opacity)
  169. });
  170. $container.removeAttr('style').css({
  171. padding: theme.container_padding + 'px',
  172. backgroundColor: window.PUM_Admin.utils.convert_hex(theme.container_background_color, theme.container_background_opacity),
  173. borderStyle: theme.container_border_style,
  174. borderColor: theme.container_border_color,
  175. borderWidth: theme.container_border_width + 'px',
  176. borderRadius: theme.container_border_radius + 'px',
  177. boxShadow: container_inset + theme.container_boxshadow_horizontal + 'px ' + theme.container_boxshadow_vertical + 'px ' + theme.container_boxshadow_blur + 'px ' + theme.container_boxshadow_spread + 'px ' + window.PUM_Admin.utils.convert_hex(theme.container_boxshadow_color, theme.container_boxshadow_opacity)
  178. });
  179. $title.removeAttr('style').css({
  180. color: theme.title_font_color,
  181. lineHeight: theme.title_line_height + 'px',
  182. fontSize: theme.title_font_size + 'px',
  183. fontFamily: theme.title_font_family,
  184. fontStyle: theme.title_font_style,
  185. fontWeight: theme.title_font_weight,
  186. textAlign: theme.title_text_align,
  187. textShadow: theme.title_textshadow_horizontal + 'px ' + theme.title_textshadow_vertical + 'px ' + theme.title_textshadow_blur + 'px ' + window.PUM_Admin.utils.convert_hex(theme.title_textshadow_color, theme.title_textshadow_opacity)
  188. });
  189. $content.removeAttr('style').css({
  190. color: theme.content_font_color,
  191. //fontSize: theme.content_font_size+'px',
  192. fontFamily: theme.content_font_family,
  193. fontStyle: theme.content_font_style,
  194. fontWeight: theme.content_font_weight
  195. });
  196. $close.html(theme.close_text).removeAttr('style').css({
  197. position: theme.close_position_outside ? 'fixed' : 'absolute',
  198. padding: theme.close_padding + 'px',
  199. height: theme.close_height > 0 ? theme.close_height + 'px' : 'auto',
  200. width: theme.close_width > 0 ? theme.close_width + 'px' : 'auto',
  201. backgroundColor: window.PUM_Admin.utils.convert_hex(theme.close_background_color, theme.close_background_opacity),
  202. color: theme.close_font_color,
  203. lineHeight: theme.close_line_height + 'px',
  204. fontSize: theme.close_font_size + 'px',
  205. fontFamily: theme.close_font_family,
  206. fontWeight: theme.close_font_weight,
  207. fontStyle: theme.close_font_style,
  208. borderStyle: theme.close_border_style,
  209. borderColor: theme.close_border_color,
  210. borderWidth: theme.close_border_width + 'px',
  211. borderRadius: theme.close_border_radius + 'px',
  212. boxShadow: close_inset + theme.close_boxshadow_horizontal + 'px ' + theme.close_boxshadow_vertical + 'px ' + theme.close_boxshadow_blur + 'px ' + theme.close_boxshadow_spread + 'px ' + window.PUM_Admin.utils.convert_hex(theme.close_boxshadow_color, theme.close_boxshadow_opacity),
  213. textShadow: theme.close_textshadow_horizontal + 'px ' + theme.close_textshadow_vertical + 'px ' + theme.close_textshadow_blur + 'px ' + window.PUM_Admin.utils.convert_hex(theme.close_textshadow_color, theme.close_textshadow_opacity)
  214. });
  215. top = theme.close_position_top + (theme.close_position_outside ? $('#wpadminbar').outerHeight() : 0);
  216. left = theme.close_position_left + (theme.close_position_outside ? $('#adminmenuwrap').outerWidth() : 0);
  217. right = theme.close_position_right;
  218. bottom = theme.close_position_bottom;
  219. switch (theme.close_location) {
  220. case "topleft":
  221. $close.css({
  222. top: top + 'px',
  223. left: left + 'px'
  224. });
  225. break;
  226. case "topcenter":
  227. $close.css({
  228. top: top + 'px',
  229. left: '50%',
  230. transform: 'translateX(-50%)'
  231. // left: 0,
  232. // right: 0,
  233. // left: 0,
  234. // margin: 'auto'
  235. });
  236. break;
  237. case "topright":
  238. $close.css({
  239. top: top + 'px',
  240. right: right + 'px'
  241. });
  242. break;
  243. case 'middleleft':
  244. $close.css({
  245. top: '50%',
  246. left: left + 'px',
  247. transform: 'translateY(-50%)'
  248. });
  249. break;
  250. case 'middleright':
  251. $close.css({
  252. top: '50%',
  253. right: right + 'px',
  254. transform: 'translateY(-50%)'
  255. });
  256. break;
  257. case "bottomleft":
  258. $close.css({
  259. bottom: bottom + 'px',
  260. left: left + 'px'
  261. });
  262. break;
  263. case "bottomcenter":
  264. $close.css({
  265. bottom: bottom + 'px',
  266. left: '50%',
  267. transform: 'translateX(-50%)'
  268. // left: 0,
  269. // right: 0,
  270. // left: 0,
  271. // margin: 'auto'
  272. });
  273. break;
  274. case "bottomright":
  275. $close.css({
  276. bottom: bottom + 'px',
  277. right: right + 'px'
  278. });
  279. break;
  280. }
  281. /** @deprecated 1.8.0 */
  282. $(document).trigger('popmake-admin-retheme', [theme]);
  283. $(document).trigger('pumRestylePreview', [theme]);
  284. }
  285. };
  286. $(document)
  287. .on('change', 'select[id$="_font_family"]', function () {
  288. var prefix = $(this).attr('id').replace('_font_family', '');
  289. PUM_Admin.themeEditor.update_font_options(prefix);
  290. })
  291. /**
  292. * Change to the appropriate tab when an element is clicked. IE click the close button in preview and trigger the close tab.
  293. */
  294. .on('click', '.pum-popup-overlay, .pum-popup-container, .pum-popup-title, .pum-popup-content, .pum-popup-close', function (event) {
  295. var $this = $(this),
  296. clicked_class = $this.attr('class');
  297. event.preventDefault();
  298. event.stopPropagation();
  299. switch (clicked_class) {
  300. case 'pum-popup-overlay':
  301. $('a[href="#pum-theme-settings_overlay"]')[0].click();
  302. break;
  303. case 'pum-popup-container':
  304. $('a[href="#pum-theme-settings_container"]')[0].click();
  305. break;
  306. case 'pum-popup-title':
  307. $('a[href="#pum-theme-settings_title"]')[0].click();
  308. break;
  309. case 'pum-popup-content':
  310. $('a[href="#pum-theme-settings_content"]')[0].click();
  311. break;
  312. case 'pum-popup-close':
  313. $('a[href="#pum-theme-settings_close"]')[0].click();
  314. break;
  315. }
  316. $("html, body").animate({
  317. scrollTop: ($('#pum_theme_settings').offset().top - 40) + 'px'
  318. });
  319. })
  320. /**
  321. * Trigger preview update after any field change.
  322. */
  323. .on('change colorchange input focusout', '.pum-field select, .pum-field input', function () {
  324. PUM_Admin.themeEditor.refresh_preview();
  325. });
  326. // Initiate when ready.
  327. $(function () {
  328. $(this).trigger('pum_init');
  329. var $container = $('#pum-theme-settings-container'),
  330. args = pum_theme_settings_editor.form_args || {},
  331. values = pum_theme_settings_editor.current_values || {};
  332. if ($container.length) {
  333. $container.find('.pum-no-js').hide();
  334. PUM_Admin.forms.render(args, values, $container);
  335. }
  336. PUM_Admin.themeEditor.preview_fixed_scroll();
  337. PUM_Admin.themeEditor.refresh_preview();
  338. });
  339. }(jQuery));