lightbox.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. var W3tc_Lightbox = {
  2. window: jQuery(window),
  3. container: null,
  4. options: null,
  5. create: function() {
  6. var me = this;
  7. this.container = jQuery('<div class="' + this.options.id + '"><div class="lightbox-close">' + this.options.close + '</div><div id="w3tc_lightbox_content" class="lightbox-content"></div></div>').css({
  8. top: 0,
  9. left: 0,
  10. width: 0,
  11. height: 0,
  12. position: 'fixed',
  13. 'z-index': 9991,
  14. display: 'none'
  15. });
  16. jQuery('body').append(this.container);
  17. this.window.resize(function() {
  18. me.resize();
  19. });
  20. this.container.find('.lightbox-close').on( 'click', function() {
  21. me.close();
  22. });
  23. jQuery(document).keyup(function(e) {
  24. if (e.keyCode == 27) { me.close(); } // esc
  25. });
  26. },
  27. open: function(options) {
  28. this.options = jQuery.extend({
  29. id: 'lightbox',
  30. close: '',
  31. width: 0,
  32. height: 0,
  33. maxWidth: 0,
  34. maxHeight: 0,
  35. minWidth: 0,
  36. minHeight: 0,
  37. widthPercent: 0.6,
  38. heightPercent: 0.8,
  39. content: null,
  40. url: null,
  41. callback: null
  42. }, options);
  43. this.create();
  44. if (this.options.content) {
  45. this.content(this.options.content);
  46. } else if (this.options.url) {
  47. this.load(this.options.url, this.options.callback);
  48. if (window.w3tc_ga) {
  49. var w3tc_action = this.options.url.match(/w3tc_action=([^&]+)/);
  50. if (! w3tc_action || ! w3tc_action[1]) {
  51. w3tc_action = this.options.url.match(/&(w3tc_[^&]+)&/);
  52. }
  53. if (w3tc_action && w3tc_action[1]) {
  54. w3tc_ga(
  55. 'event',
  56. 'pageview',
  57. {
  58. eventLabel: 'overlays/' + w3tc_action[1]
  59. }
  60. );
  61. }
  62. }
  63. }
  64. W3tc_Overlay.show();
  65. this.container.show();
  66. },
  67. close: function() {
  68. if (this.options.onClose)
  69. this.options.onClose();
  70. this.container.remove();
  71. W3tc_Overlay.hide();
  72. },
  73. resize: function() {
  74. var width = (this.options.width ? this.options.width : this.window.width() * this.options.widthPercent);
  75. var height = (this.options.height ? this.options.height : this.window.height() * this.options.heightPercent);
  76. if (!this.options.maxWidth)
  77. this.options.maxWidth = this.window.width();
  78. if (!this.options.maxHeight)
  79. this.options.maxHeight = this.window.height();
  80. if (this.options.maxWidth && width > this.options.maxWidth) {
  81. width = this.options.maxWidth;
  82. } else if (width < this.options.minWidth) {
  83. width = this.options.minWidth;
  84. }
  85. if (this.options.maxHeight && height > this.options.maxHeight) {
  86. height = this.options.maxHeight;
  87. } else if (height < this.options.minHeight) {
  88. height = this.options.minHeight;
  89. }
  90. this.container.css({
  91. width: width,
  92. height: height
  93. });
  94. this.container.css({
  95. top: (this.window.height() / 2 - this.container.outerHeight() / 2)>=0 ? this.window.height() / 2 - this.container.outerHeight() / 2 : 0,
  96. left: (this.window.width() / 2 - this.container.outerWidth() / 2)>=0 ? this.window.width() / 2 - this.container.outerWidth() / 2 : 0
  97. });
  98. jQuery('.lightbox-content', this.container).css({
  99. width: width,
  100. height: height
  101. });
  102. },
  103. load: function(url, callback) {
  104. this.content('');
  105. this.loading(true);
  106. var me = this;
  107. jQuery.get(url, {}, function(content) {
  108. me.loading(false);
  109. if (content.substr(0, 9) === 'Location ') {
  110. w3tc_beforeupload_unbind();
  111. window.location = content.substr(9);
  112. return;
  113. }
  114. me.content(content);
  115. if (callback) {
  116. callback.call(this, me);
  117. }
  118. });
  119. },
  120. /**
  121. * adds all controls of the form to the url
  122. */
  123. load_form: function(url, form_selector, callback) {
  124. data = {};
  125. var v = jQuery(form_selector).find('input').each(function(i) {
  126. var name = jQuery(this).attr('name');
  127. var type = jQuery(this).attr('type');
  128. if (type == 'radio' || type == 'checkbox' ) {
  129. if (!jQuery(this).prop('checked'))
  130. return;
  131. }
  132. if (name)
  133. data[name] = jQuery(this).val();
  134. });
  135. this.content('');
  136. this.loading(true);
  137. var me = this;
  138. jQuery.post(url, data, function(content) {
  139. me.loading(false);
  140. if (content.substr(0, 9) === 'Location ') {
  141. w3tc_beforeupload_unbind();
  142. window.location = content.substr(9);
  143. return;
  144. }
  145. me.content(content);
  146. if (callback) {
  147. callback.call(this, me);
  148. }
  149. });
  150. },
  151. content: function(content) {
  152. return this.container.find('.lightbox-content').html(content);
  153. },
  154. width: function(width) {
  155. if (width === undefined) {
  156. return this.container.width();
  157. } else {
  158. this.container.css('width', width);
  159. return this.resize();
  160. }
  161. },
  162. height: function(height) {
  163. if (height === undefined) {
  164. return this.container.height();
  165. } else {
  166. this.container.css('height', height);
  167. return this.resize();
  168. }
  169. },
  170. loading: function(loading) {
  171. if (loading)
  172. this.container.find('.lightbox-content').addClass('lightbox-loader');
  173. else
  174. this.container.find('.lightbox-content').removeClass('lightbox-loader');
  175. }
  176. };
  177. var W3tc_Overlay = {
  178. window: jQuery(window),
  179. container: null,
  180. create: function() {
  181. var me = this;
  182. this.container = jQuery('<div id="overlay" />').css({
  183. top: 0,
  184. left: 0,
  185. width: 0,
  186. height: 0,
  187. position: 'fixed',
  188. 'z-index': 9990,
  189. display: 'none',
  190. opacity: 0.6
  191. });
  192. jQuery('#w3tc').append(this.container);
  193. this.window.resize(function() {
  194. me.resize();
  195. });
  196. this.window.scroll(function() {
  197. me.resize();
  198. });
  199. },
  200. show: function() {
  201. this.create();
  202. this.resize();
  203. this.container.show();
  204. },
  205. hide: function() {
  206. this.container.remove();
  207. },
  208. resize: function() {
  209. this.container.css({
  210. width: this.window.width(),
  211. height: this.window.height()
  212. });
  213. }
  214. };
  215. var w3tc_minify_recommendations_checked = {};
  216. function w3tc_lightbox_minify_recommendations(nonce) {
  217. W3tc_Lightbox.open({
  218. width: 1000,
  219. url: 'admin.php?page=w3tc_minify&w3tc_test_minify_recommendations&_wpnonce=' + nonce,
  220. callback: function(lightbox) {
  221. var theme = jQuery('#recom_theme').val();
  222. if (jQuery.ui && jQuery.ui.sortable) {
  223. jQuery("#recom_js_files,#recom_css_files").sortable({
  224. axis: 'y',
  225. stop: function() {
  226. jQuery(this).find('li').each(function(index) {
  227. jQuery(this).find('td:eq(1)').html((index + 1) + '.');
  228. });
  229. }
  230. });
  231. }
  232. if (w3tc_minify_recommendations_checked[theme] !== undefined) {
  233. jQuery('#recom_js_files :text,#recom_css_files :text').each(function() {
  234. var hash = jQuery(this).parents('li').find('[name=recom_js_template]').val() + ':' + jQuery(this).val();
  235. if (w3tc_minify_recommendations_checked[theme][hash] !== undefined) {
  236. var checkbox = jQuery(this).parents('li').find(':checkbox');
  237. if (w3tc_minify_recommendations_checked[theme][hash]) {
  238. checkbox.attr('checked', 'checked');
  239. } else {
  240. checkbox.removeAttr('checked');
  241. }
  242. }
  243. });
  244. }
  245. jQuery('#recom_theme').change(function() {
  246. jQuery('#recom_js_files :checkbox,#recom_css_files :checkbox').each(function() {
  247. var li = jQuery(this).parents('li');
  248. var hash = li.find('[name=recom_js_template]').val() + ':' + li.find(':text').val();
  249. if (w3tc_minify_recommendations_checked[theme] === undefined) {
  250. w3tc_minify_recommendations_checked[theme] = {};
  251. }
  252. w3tc_minify_recommendations_checked[theme][hash] = jQuery(this).is(':checked');
  253. });
  254. lightbox.load('admin.php?page=w3tc_minify&w3tc_test_minify_recommendations&theme_key=' + jQuery(this).val() + '&_wpnonce=' + nonce, lightbox.options.callback);
  255. });
  256. jQuery('#recom_js_check').on( 'click', function() {
  257. if (jQuery('#recom_js_files :checkbox:checked').length) {
  258. jQuery('#recom_js_files :checkbox').removeAttr('checked');
  259. } else {
  260. jQuery('#recom_js_files :checkbox').attr('checked', 'checked');
  261. }
  262. return false;
  263. });
  264. jQuery('#recom_css_check').on( 'click', function() {
  265. if (jQuery('#recom_css_files :checkbox:checked').length) {
  266. jQuery('#recom_css_files :checkbox').removeAttr('checked');
  267. } else {
  268. jQuery('#recom_css_files :checkbox').attr('checked', 'checked');
  269. }
  270. return false;
  271. });
  272. jQuery('.recom_apply', lightbox.container).on( 'click', function() {
  273. var theme = jQuery('#recom_theme').val();
  274. jQuery('#js_files li').each(function() {
  275. if (jQuery(this).find(':text').attr('name').indexOf('js_files[' + theme + ']') != -1) {
  276. jQuery(this).remove();
  277. }
  278. });
  279. jQuery('#css_files li').each(function() {
  280. if (jQuery(this).find(':text').attr('name').indexOf('css_files[' + theme + ']') != -1) {
  281. jQuery(this).remove();
  282. }
  283. });
  284. jQuery('#recom_js_files li').each(function() {
  285. if (jQuery(this).find(':checkbox:checked').length) {
  286. w3tc_minify_js_file_add(theme, jQuery(this).find('[name=recom_js_template]').val(), jQuery(this).find('[name=recom_js_location]').val(), jQuery(this).find('[name=recom_js_file]').val());
  287. }
  288. });
  289. jQuery('#recom_css_files li').each(function() {
  290. if (jQuery(this).find(':checkbox:checked').length) {
  291. w3tc_minify_css_file_add(theme, jQuery(this).find('[name=recom_css_template]').val(), jQuery(this).find('[name=recom_css_file]').val());
  292. }
  293. });
  294. w3tc_minify_js_theme(theme);
  295. w3tc_minify_css_theme(theme);
  296. w3tc_input_enable('.js_enabled', jQuery('#minify_js_enable:checked').length);
  297. w3tc_input_enable('.css_enabled', jQuery('#minify_css_enable:checked').length);
  298. lightbox.close();
  299. });
  300. lightbox.resize();
  301. }
  302. });
  303. }
  304. function w3tc_lightbox_self_test(nonce) {
  305. W3tc_Lightbox.open({
  306. width: 800,
  307. minHeight: 300,
  308. url: 'admin.php?page=w3tc_dashboard&w3tc_test_self&_wpnonce=' + w3tc_nonce,
  309. callback: function(lightbox) {
  310. jQuery('.button-primary', lightbox.container).on( 'click', function() {
  311. lightbox.close();
  312. });
  313. lightbox.resize();
  314. }
  315. });
  316. }
  317. function w3tc_lightbox_upgrade(nonce, data_src, renew_key) {
  318. var client_id = '';
  319. if (window.w3tc_ga) {
  320. client_id = w3tc_ga_cid;
  321. }
  322. var minWidth = jQuery(window).width() - 30;
  323. var minHeight = jQuery(window).height() - 30;
  324. W3tc_Lightbox.open({
  325. id: 'w3tc-overlay',
  326. close: '',
  327. maxWidth: 1000,
  328. minWidth: ( minWidth < 1000 ? minWidth : 1000 ),
  329. minHeight: ( minHeight < 500 ? minHeight : 500 ),
  330. url: 'admin.php?page=w3tc_dashboard&w3tc_licensing_upgrade&_wpnonce=' +
  331. encodeURIComponent(nonce) + '&data_src=' + encodeURIComponent(data_src) +
  332. (renew_key ? '&renew_key=' + encodeURIComponent(renew_key) : '') +
  333. (client_id ? '&client_id=' + encodeURIComponent(client_id) : ''),
  334. callback: function(lightbox) {
  335. lightbox.options.height = jQuery('#w3tc-upgrade').outerHeight();
  336. jQuery('.button-primary', lightbox.container).on( 'click', function() {
  337. lightbox.close();
  338. });
  339. jQuery('#w3tc-purchase', lightbox.container).on( 'click', function() {
  340. lightbox.close();
  341. w3tc_lightbox_buy_plugin(nonce, data_src, renew_key, client_id);
  342. });
  343. jQuery('#w3tc-purchase-link', lightbox.container).on( 'click', function() {
  344. lightbox.close();
  345. if ( jQuery('#licensing').length ) {
  346. jQuery([document.documentElement, document.body]).animate({
  347. scrollTop: jQuery('#licensing').offset().top
  348. }, 2000);
  349. }
  350. });
  351. // Allow for customizations of the "upgrade" overlay specifically.
  352. jQuery( '.w3tc-overlay' ).addClass( 'w3tc-overlay-upgrade' );
  353. lightbox.resize();
  354. }
  355. });
  356. }
  357. function w3tc_lightbox_buy_plugin(nonce, data_src, renew_key, client_id) {
  358. if (window.w3tc_ga) {
  359. client_id = w3tc_ga_cid;
  360. }
  361. var minWidth = jQuery(window).width() - 30;
  362. var minHeight = jQuery(window).height() - 30;
  363. W3tc_Lightbox.open({
  364. id: 'w3tc-overlay',
  365. maxWidth: 1000,
  366. minWidth: ( minWidth < 1000 ? minWidth : 1000 ),
  367. minHeight: ( minHeight < 700 ? minHeight : 700 ),
  368. url: 'admin.php?page=w3tc_dashboard&w3tc_licensing_buy_plugin' +
  369. '&_wpnonce=' + encodeURIComponent(nonce) +
  370. '&data_src=' + encodeURIComponent(data_src) +
  371. (renew_key ? '&renew_key=' + encodeURIComponent(renew_key) : '') +
  372. (client_id ? '&client_id=' + encodeURIComponent(client_id) : ''),
  373. callback: function(lightbox) {
  374. var w3tc_license_listener = function(event) {
  375. if (event.origin.substr(event.origin.length - 12) !== ".w3-edge.com")
  376. return;
  377. var data = event.data.split(' ');
  378. if (data[0] === 'license') {
  379. // legacy purchase
  380. w3tc_lightbox_save_license_key(function() {
  381. lightbox.close();
  382. });
  383. } else if (data[0] === 'v2_license') {
  384. // reset default timeout
  385. var iframe = document.getElementById('buy_frame');
  386. if (iframe.contentWindow && iframe.contentWindow.postMessage)
  387. iframe.contentWindow.postMessage('v2_license_accepted', '*');
  388. lightbox.options.onClose = function() {
  389. window.location = window.location + '&refresh';
  390. }
  391. w3tc_lightbox_save_license_key(data[1], nonce, function() {
  392. jQuery('#buy_frame').attr('src', data[3]);
  393. });
  394. }
  395. }
  396. if (window.addEventListener) {
  397. addEventListener("message", w3tc_license_listener, false)
  398. } else if (attachEvent) {
  399. attachEvent("onmessage", w3tc_license_listener);
  400. }
  401. jQuery('.button-primary', lightbox.container).on( 'click', function() {
  402. lightbox.close();
  403. });
  404. // Allow for customizations of the "upgrade" overlay specifically.
  405. jQuery( '.w3tc-overlay' ).addClass( 'w3tc-overlay-upgrade' );
  406. lightbox.resize();
  407. }
  408. });
  409. }
  410. function w3tc_lightbox_save_license_key(license_key, nonce, callback) {
  411. jQuery('#plugin_license_key').val(license_key);
  412. var params = {
  413. w3tc_default_save_license_key: 1,
  414. license_key: license_key,
  415. _wpnonce: ('array' === jQuery.type(nonce)) ? nonce[0] : nonce
  416. };
  417. jQuery.post('admin.php?page=w3tc_dashboard', params, function(data) {
  418. callback();
  419. }, 'json').fail(callback);
  420. }
  421. jQuery(function() {
  422. jQuery('.button-minify-recommendations').on( 'click', function() {
  423. var nonce = jQuery(this).metadata().nonce;
  424. w3tc_lightbox_minify_recommendations(nonce);
  425. return false;
  426. });
  427. jQuery('.button-self-test').on( 'click', function() {
  428. var nonce = jQuery(this).metadata().nonce;
  429. w3tc_lightbox_self_test(nonce);
  430. return false;
  431. });
  432. jQuery('.button-buy-plugin').on( 'click', function() {
  433. var data_src = jQuery(this).data('src');
  434. var nonce = jQuery(this).data('nonce');
  435. if (!nonce) {
  436. nonce = w3tc_nonce;
  437. }
  438. var renew_key = jQuery(this).data('renew-key');
  439. if (window.w3tc_ga) {
  440. w3tc_ga(
  441. 'event',
  442. 'button',
  443. {
  444. eventCategory: 'click',
  445. eventLabel: 'license_upgrade_' + data_src
  446. }
  447. );
  448. }
  449. w3tc_lightbox_upgrade(nonce, data_src, renew_key);
  450. jQuery('#w3tc-license-instruction').show();
  451. return false;
  452. });
  453. jQuery('.button-renew-plugin').on( 'click', function() {
  454. var data_src = jQuery(this).data('src');
  455. var nonce = jQuery(this).data('nonce');
  456. if (!nonce) {
  457. nonce = w3tc_nonce;
  458. }
  459. var renew_key = jQuery(this).data('renew-key');
  460. if (window.w3tc_ga) {
  461. w3tc_ga(
  462. 'event',
  463. 'button',
  464. {
  465. eventCategory: 'click',
  466. eventLabel: 'license_renew_' + data_src
  467. }
  468. );
  469. }
  470. w3tc_lightbox_buy_plugin(nonce, data_src, renew_key);
  471. return false;
  472. });
  473. jQuery('body').on('click', '.w3tc_lightbox_close', function() {
  474. W3tc_Lightbox.close();
  475. });
  476. });