vendor
5 years ago
adblock.js
5 years ago
admin.debug.js
5 years ago
admin.min.js
5 years ago
front.debug.js
5 years ago
front.min.js
5 years ago
preview.debug.js
5 years ago
preview.min.js
5 years ago
shared-ui.min.js
5 years ago
wp-dashboard.debug.js
5 years ago
wp-dashboard.min.js
5 years ago
preview.debug.js
149 lines
| 1 | /* global hustleVars */ |
| 2 | (function ($) { |
| 3 | 'use strict'; |
| 4 | |
| 5 | var hustlePreview = { |
| 6 | moduleId: null, |
| 7 | moduleData: null, |
| 8 | $module: null, |
| 9 | open: function open(id, type) { |
| 10 | var _this = this; |
| 11 | |
| 12 | var previewData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; |
| 13 | |
| 14 | // Abort if we don't have an ID. |
| 15 | if ('undefined' === typeof id) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | var isInline = 'embedded' === type || 'social_sharing' === type, |
| 20 | $previewContainer = isInline ? $('#module-preview-inline-container') : $('#module-preview-container'); |
| 21 | $.ajax({ |
| 22 | url: hustleVars.ajaxurl, |
| 23 | type: 'POST', |
| 24 | data: { |
| 25 | action: 'hustle_preview_module', |
| 26 | id: id, |
| 27 | previewData: previewData |
| 28 | } |
| 29 | }).then(function (res) { |
| 30 | if (res.success) { |
| 31 | $previewContainer.html(res.data.html); |
| 32 | _this.$module = $previewContainer.find('.hustle-ui'); // Load select2 if this module has select fields. |
| 33 | |
| 34 | if (_this.$module.find('.hustle-select2').length) { |
| 35 | HUI.select2(); |
| 36 | } // If there's a timepicker. |
| 37 | |
| 38 | |
| 39 | if (_this.$module.find('.hustle-time').length) { |
| 40 | HUI.timepicker('.hustle-time'); |
| 41 | } // If there's a datepicker. |
| 42 | |
| 43 | |
| 44 | if (_this.$module.find('.hustle-date').length) { |
| 45 | var _hustleVars = hustleVars, |
| 46 | strings = _hustleVars.days_and_months; |
| 47 | HUI.datepicker('.hustle-date', strings.days_full, strings.days_short, strings.days_min, strings.months_full, strings.months_short); |
| 48 | } |
| 49 | |
| 50 | HUI.nonSharingSimulation(_this.$module); |
| 51 | HUI.inputFilled(); |
| 52 | |
| 53 | if (res.data.style) { |
| 54 | $previewContainer.append(res.data.style); |
| 55 | } |
| 56 | |
| 57 | if (res.data.script) { |
| 58 | $previewContainer.append(res.data.script); |
| 59 | } |
| 60 | |
| 61 | setTimeout(function () { |
| 62 | return HUI.maybeRenderRecaptcha(_this.$module); |
| 63 | }, 1000); |
| 64 | } |
| 65 | |
| 66 | return { |
| 67 | id: id, |
| 68 | data: res.data.module |
| 69 | }; |
| 70 | }, function () {// TODO: handle errors |
| 71 | }).then(function (_ref) { |
| 72 | var moduleId = _ref.id, |
| 73 | data = _ref.data; |
| 74 | _this.moduleId = moduleId; |
| 75 | _this.moduleData = data; // Display the module. |
| 76 | |
| 77 | _this.showModule(); |
| 78 | }); |
| 79 | }, |
| 80 | showModule: function showModule() { |
| 81 | var autohideDelay = '0' === String(this.$module.data('close-delay')) ? false : this.$module.data('close-delay'); |
| 82 | |
| 83 | if ('popup' === this.moduleData.module_type) { |
| 84 | HUI.popupLoad(this.$module[0], autohideDelay); |
| 85 | } else if ('slidein' === this.moduleData.module_type) { |
| 86 | HUI.slideinLayouts(this.$module[0]); |
| 87 | HUI.slideinLoad(this.$module[0], autohideDelay); |
| 88 | $(window).on('resize', function () { |
| 89 | if (this.$module) { |
| 90 | HUI.slideinLayouts(this.$module[0]); |
| 91 | } |
| 92 | }); |
| 93 | } else { |
| 94 | HUI.inlineResize(this.$module[0]); |
| 95 | HUI.inlineLoad(this.$module[0]); |
| 96 | } |
| 97 | }, |
| 98 | reloadModule: function reloadModule() { |
| 99 | var _this2 = this; |
| 100 | |
| 101 | if (this.$module.is(':visible')) { |
| 102 | var delay = 0; |
| 103 | |
| 104 | if ('popup' === this.moduleData.module_type) { |
| 105 | HUI.popupClose(this.$module[0], 0); |
| 106 | delay = 2000; |
| 107 | } else if ('slidein' === this.moduleData.module_type) { |
| 108 | HUI.slideinClose(this.$module[0], 0); |
| 109 | delay = 500; |
| 110 | } // TODO: replace this timeout by an event for |
| 111 | // when after the module is hidden when introduced into HUI. |
| 112 | |
| 113 | |
| 114 | setTimeout(function () { |
| 115 | return _this2.showModule(); |
| 116 | }, delay); |
| 117 | } else { |
| 118 | this.showModule(); |
| 119 | } |
| 120 | }, |
| 121 | previewClosed: function previewClosed() { |
| 122 | this.$module = null; |
| 123 | $('#module-preview-container').empty(); |
| 124 | $('#module-preview-inline-container').empty(); |
| 125 | } |
| 126 | }; |
| 127 | window.addEventListener('message', hustleReceiveMessage, false); |
| 128 | |
| 129 | function hustleReceiveMessage(event) { |
| 130 | if (event.origin !== window.location.origin || event.source !== window.parent) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | var action = event.data.action; |
| 135 | |
| 136 | if ('open' === action) { |
| 137 | hustlePreview.open(event.data.moduleId, event.data.moduleType, event.data.previewData); |
| 138 | } else if ('close' === action) { |
| 139 | hustlePreview.previewClosed(); |
| 140 | } else if ('reload' === action) { |
| 141 | hustlePreview.reloadModule(); |
| 142 | } |
| 143 | } // Prevent links from doing anything on the preview page. |
| 144 | |
| 145 | |
| 146 | $('a:not(.hustle-button)').on('click', function (e) { |
| 147 | e.preventDefault(); |
| 148 | }); |
| 149 | })(jQuery); |