123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- (function($, FLBuilder) {
- /**
- * Save Manager Object
- */
- var SaveManager = {
- /**
- * Indicates whether or not the current layout is clean or has changes that require
- * publish actions.
- *
- * @var bool
- */
- layoutNeedsPublish: false,
- /**
- * The message that is displayed whenever `resetStatusMessage()` is called.
- *
- * @var string
- */
- defaultIndicatorMessage: "",
- /**
- * The tooltip that is displayed whenever 'resetStatusMessage()' is called.
- *
- * @var string
- */
- defaultTooltipMessage: "",
- /**
- * Local reference to strings pertaining to saving states.
- *
- * @var object
- */
- messages: null,
- /**
- * Setup the Save Manager object.
- *
- * @return void
- */
- init: function() {
- this.messages = FLBuilderStrings.savedStatus;
- this.$savingIndicator = $('.fl-builder--saving-indicator', window.parent.document);
- FLBuilder.addHook('didBeginAJAX', this.onLayoutSaving.bind(this));
- FLBuilder.addHook('didCompleteAJAX', this.onLayoutSaved.bind(this));
- FLBuilder.addHook('didPublishLayout', this.onLayoutPublished.bind(this));
- FLBuilder.addHook('publishAndRemain', this.onPublishAndRemain.bind(this));
- // We have to set the layout as needing publish when settings are opened because
- // we can't yet reliably set it to needing publish when settings are changed.
- FLBuilder.addHook('didShowLightbox', this.setLayoutNeedsPublish.bind(this));
- if ( FLBuilderConfig.layoutHasDraftedChanges || ! FLBuilderConfig.builderEnabled ) {
- this.setLayoutNeedsPublish();
- this.resetStatusMessage();
- }
- },
- /**
- * Flag the layout as needing to be published.
- *
- * @return void
- */
- setLayoutNeedsPublish: function() {
- if ( !this.layoutNeedsPublish ) {
- this.layoutNeedsPublish = true;
- $('body').add( 'body', window.parent.document ).addClass('fl-builder--layout-has-drafted-changes');
- }
- },
- /**
- * Fires when layout begins saving.
- *
- * @return void
- */
- onLayoutSaving: function(e, data) {;
- if ( this.isPublishingLayout( data.action ) ) {
- this.showStatusMessage( this.messages.publishing, this.messages.publishingTooltip );
- }
- else if ( this.isUpdatingLayout( data.action ) ) {
- this.setLayoutNeedsPublish();
- this.showStatusMessage( this.messages.saving, this.messages.savingTooltip );
- }
- },
- /**
- * Check if the current ajax action is publishing the layout
- *
- * @var String action
- * @return bool
- */
- isPublishingLayout: function( action ) {
- if ( 'save_layout' == action ) {
- return true;
- }
- return false;
- },
- /**
- * Checks if the current ajax action is updating the layout or some other part of the system.
- *
- * @var String action
- * @return bool
- */
- isUpdatingLayout: function( action ) {
- if ( this.isPublishingLayout() ) return false;
- if ( action.startsWith('render') ) {
- if ( action.startsWith('render_new') ) return true;
- return false;
- }
- if ( action.startsWith('duplicate') ) return false;
- if ( action.startsWith('refresh') ) return false; // Like refresh_revision_items
- if ( 'save_ui_skin' == action ) return false;
- if ( 'save_lightbox_position' == action ) return false;
- if ( 'save_pinned_ui_position' == action ) return false;
- if ( 'fl_builder_notifications' == action ) return false;
- if ( action.indexOf( 'history' ) > -1 ) return false; // HistoryManager
- return true;
- },
- /**
- * Fires after layout has been successfully saved.
- * Display the "Saved" message, wait a bit and reset to the default message.
- *
- * @return void
- */
- onLayoutSaved: function( e, data ) {
- if ( this.isUpdatingLayout( data.fl_builder_data.action ) ) {
- this.showStatusMessage(this.messages.saved, this.messages.savedTooltip);
- var obj = this;
- setTimeout(function() {
- obj.resetStatusMessage();
- }, 2000);
- }
- },
- /**
- * Handle layout published
- *
- * @return void
- */
- onLayoutPublished: function() {
- this.layoutNeedsPublish = false;
- $('body').add( 'body', window.parent.document ).removeClass('fl-builder--layout-has-drafted-changes');
- this.resetStatusMessage();
- },
- /**
- * Set the status message area to a string of text.
- *
- * @var string Message to display
- * @return void
- */
- showStatusMessage: function(message, toolTip) {
- this.$savingIndicator.html(message);
- if (! FLBuilder.isUndefined(toolTip)) {
- this.$savingIndicator.attr('title', toolTip);
- $('.fl-builder--saving-indicator', window.parent.document).tipTip({
- defaultPosition: 'bottom',
- edgeOffset: 14
- });
- }
- },
- /**
- * Set the status message back to it's default state.
- *
- * @return void
- */
- resetStatusMessage: function() {
- if(this.layoutNeedsPublish) {
- this.defaultIndicatorMessage = this.messages.edited + '<i class="fas fa-question-circle"></i>';
- this.defaultTooltipMessage = this.messages.editedTooltip;
- } else {
- this.defaultIndicatorMessage = "";
- this.defaultTooltipMessage = "";
- }
- this.showStatusMessage(this.defaultIndicatorMessage, this.defaultTooltipMessage );
- },
- /**
- * Handle publish key command
- *
- * @return void
- */
- onPublishAndRemain: function() {
- FLBuilder.MainMenu.hide();
- if (this.layoutNeedsPublish || FLBuilderSettingsForms.settingsHaveChanged()) {
- FLBuilder._publishLayout(false, true);
- } else {
- this.showStatusMessage(this.messages.noChanges);
- var manager = this;
- setTimeout(function() {
- manager.resetStatusMessage();
- }, 2000);
- }
- }
- };
- /**
- * Pubic Interface
- */
- FLBuilder.SaveManager = {
- /**
- * Check if the current layout has unpublished changes
- *
- * @return bool
- */
- layoutNeedsPublish: function() {
- return SaveManager.layoutNeedsPublish;
- },
- /**
- * Show a status message
- *
- * @var String message
- * @var String toolTip
- * @return void
- */
- showStatusMessage: function(message, toolTip) {
- SaveManager.showStatusMessage(message, toolTip);
- },
- /**
- * Reset status message to contextual default
- *
- * @return void
- */
- resetStatusMessage: function() {
- SaveManager.resetStatusMessage();
- }
- }
- /**
- * Kick off init.
- */
- $(function() {
- SaveManager.init();
- });
- })(jQuery, FLBuilder);
|