Skip to content

Commit

Permalink
Minor changes to the loading of canvas.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-worsfold committed Jul 19, 2017
1 parent 91ec7d6 commit 2d530af
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 179 deletions.
94 changes: 44 additions & 50 deletions assets/js/dist/canvas.js
Expand Up @@ -167,37 +167,46 @@
};
} );

app.on( 'start', function() {

this.channel.on( 'sidebar:initialize', function() {
app.channel.on('sidebar:initialize', function() {

// Load modules
app.module( 'module:elements', require( './canvas/modules/elements/elements' ) );
app.module( 'module:templates', require( './canvas/modules/templates/templates' ) );
app.module( 'module:canvas', require( './canvas/modules/canvas/canvas' ) );
app.module( 'module:tools', require( './canvas/modules/tools/tools' ) );
app.module( 'module:css', require( './canvas/modules/css/css' ) );

// Load modules
app.module( 'module:elements', require( './canvas/modules/elements/elements' ) );
app.module( 'module:templates', require( './canvas/modules/templates/templates' ) );
app.module( 'module:canvas', require( './canvas/modules/canvas/canvas' ) );
app.module( 'module:tools', require( './canvas/modules/tools/tools' ) );
app.module( 'module:css', require( './canvas/modules/css/css' ) );
} );
/**
* Fires when the canvas is initialized.
*
* @since 1.5.0
*
* @param app
*/
app.channel.trigger( 'canvas:initialize', app );
} );

$( function() {

// Start the app
function start() {
app.start( {
elements : window._elements || [],
nonces : window._nonces || [],
mediaQueries : window._media_queries || {},
cssRules : window._css_rules || {}
} );
}

/**
* Fires when the canvas is initialized.
*
* @since 1.5.0
*
* @param app
*/
app.channel.trigger( 'canvas:initialize', app );
$( function() {
var sidebarApp = window.parent.app;

// Wait until the sidebar is initialized before starting the canvas
if ( sidebarApp._initialized ) {
start();
}
else {
sidebarApp.on('start', function() {
start();
} );
}
} );

} ( window, Backbone.$ ) );
Expand All @@ -219,38 +228,20 @@ CanvasApplication = Marionette.Application.extend( {
* @since 1.0.0
*/
onBeforeStart : function() {
if ( window.location.origin !== window.parent.location.origin ) {
console.error( 'The Canvas has a different origin than the Sidebar' );
return;
}

if ( ! window.parent.app ) {
$( window.parent.document ).on( "DOMContentLoaded readystatechange load", this.registerRemoteChannel.bind( this ) );
}
else {
this.registerRemoteChannel();
}
},

/**
* Initializes the application.
*
* @since 1.0.0
*/
onStart : function() {

// White listed events from the remote channel
this.allowableEvents = [
'sidebar:initialize',

// Triggered when elements or templates are dragged from the sidebar
'canvas:dragstart', 'canvas:drag', 'canvas:dragend',
'canvas:dragstart', 'canvas:drag', 'canvas:dragend',

// Triggered when a template is loaded
'template:load',

// Triggered when an element is edited
'modal:apply',

// Triggered when a sidebar setting changes
'sidebar:setting:change',

Expand All @@ -260,9 +251,16 @@ CanvasApplication = Marionette.Application.extend( {
// Triggered when the element collection is reset (used by the History module)
'elements:reset'
];

this.addEventListeners();
},

if ( window.location.origin !== window.parent.location.origin ) {
console.error( 'The Canvas has a different origin than the Sidebar' );
return;
}

this.registerRemoteChannel();
},

/**
* Adds the required event listeners.
Expand Down Expand Up @@ -363,7 +361,7 @@ CanvasApplication = Marionette.Application.extend( {

// Forward allowable events from the remote channel
this.listenTo( remoteChannel, 'all', this.forwardRemoteEvent );

/**
* Fires when the remote channel is registered.
*
Expand All @@ -372,10 +370,6 @@ CanvasApplication = Marionette.Application.extend( {
* @param this
*/
remoteChannel.trigger( 'canvas:handshake', this );

this.listenTo( remoteChannel, 'sidebar:initialize', function() {
this.channel.trigger( 'sidebar:initialize' );
} );
},

/**
Expand Down
12 changes: 7 additions & 5 deletions assets/js/dist/canvas.min.js

Large diffs are not rendered by default.

71 changes: 37 additions & 34 deletions assets/js/dist/sidebar.js
Expand Up @@ -781,26 +781,23 @@ module.exports = notify;
return Tailor[ object ].Default;
};

app.on( 'before:start', function() {
app.module( 'module:library', require( './sidebar/modules/library/library' ) );
} );
// Load modules
app.module( 'module:library', require( './sidebar/modules/library/library' ) );
app.module( 'module:templates', require( './sidebar/modules/templates/templates' ) );
app.module( 'module:settings', require( './sidebar/modules/settings/settings' ) );
app.module( 'module:history', require( './sidebar/modules/history/history' ) );
app.module( 'module:sections', require( './sidebar/modules/sections/sections' ) );
app.module( 'module:panels', require( './sidebar/modules/panels/panels' ) );
app.module( 'module:modal', require( './sidebar/modules/modal/modal' ) );
app.module( 'module:dialog', require( './sidebar/modules/dialog/dialog' ) );
app.module( 'module:notification', require( './sidebar/modules/notifications/notifications' ) );
app.module( 'module:devicePreview', require( './sidebar/modules/device-preview/device-preview' ) );

// Initialize preview
require( './sidebar/preview' );

app.on( 'before:start', function( options ) {

app.on( 'start', function( options ) {

// Load modules
app.module( 'module:templates', require( './sidebar/modules/templates/templates' ) );
app.module( 'module:settings', require( './sidebar/modules/settings/settings' ) );
app.module( 'module:history', require( './sidebar/modules/history/history' ) );
app.module( 'module:sections', require( './sidebar/modules/sections/sections' ) );
app.module( 'module:panels', require( './sidebar/modules/panels/panels' ) );
app.module( 'module:modal', require( './sidebar/modules/modal/modal' ) );
app.module( 'module:dialog', require( './sidebar/modules/dialog/dialog' ) );
app.module( 'module:notification', require( './sidebar/modules/notifications/notifications' ) );
app.module( 'module:devicePreview', require( './sidebar/modules/device-preview/device-preview' ) );

// Initialize preview
require( './sidebar/preview' );

$doc.on( 'heartbeat-send', function( e, data ) {
data['tailor_post_id'] = window.post.id;
} );
Expand All @@ -810,11 +807,11 @@ module.exports = notify;

$win

/**
* Warns the user if they attempt to navigate away from the page without saving changes.
*
* @since 1.0.0
*/
/**
* Warns the user if they attempt to navigate away from the page without saving changes.
*
* @since 1.0.0
*/
.on( 'beforeunload.tailor', function( e ) {
if ( app.hasUnsavedChanges() ) {
return window._l10n.confirmPage;
Expand Down Expand Up @@ -850,15 +847,6 @@ module.exports = notify;
settings : window._settings || [],
controls : window._controls || []
} );

/**
* Fires when the sidebar is initialized.
*
* @since 1.0.0
*
* @param app
*/
app.channel.trigger( 'sidebar:initialize', app );
} );

} ( window, Backbone.$ ) );
Expand All @@ -872,14 +860,16 @@ var $ = Backbone.$,

SidebarApplication = Marionette.Application.extend( {

_initialized: false,

el : document.querySelector( '#tailor' ),

/**
* Initializes the application.
*
* @since 1.0.0
*/
initialize : function() {
onBeforeStart : function() {
this._collapsed = false;
this._unsavedChanges = false;
this.saveButton = document.querySelector( '#tailor-save' );
Expand Down Expand Up @@ -912,6 +902,10 @@ SidebarApplication = Marionette.Application.extend( {

this.addEventListeners();
},

onStart: function() {
this._initialized = true;
},

/**
* Returns true if there are unsaved changes.
Expand Down Expand Up @@ -1125,6 +1119,15 @@ SidebarApplication = Marionette.Application.extend( {

app.el.classList.add( 'is-initialized' );
app.el.querySelector( '.tailor-preview__viewport' ).classList.add( 'is-loaded' );

/**
* Fires when the sidebar is initialized.
*
* @since 1.0.0
*
* @param app
*/
app.channel.trigger( 'sidebar:initialize', app );
}
},

Expand Down
8 changes: 4 additions & 4 deletions assets/js/dist/sidebar.min.js

Large diffs are not rendered by default.

51 changes: 30 additions & 21 deletions assets/js/src/canvas.js
Expand Up @@ -166,37 +166,46 @@
};
} );

app.on( 'start', function() {
app.channel.on('sidebar:initialize', function() {

// Load modules
app.module( 'module:elements', require( './canvas/modules/elements/elements' ) );
app.module( 'module:templates', require( './canvas/modules/templates/templates' ) );
app.module( 'module:canvas', require( './canvas/modules/canvas/canvas' ) );
app.module( 'module:tools', require( './canvas/modules/tools/tools' ) );
app.module( 'module:css', require( './canvas/modules/css/css' ) );

this.channel.on( 'sidebar:initialize', function() {

// Load modules
app.module( 'module:elements', require( './canvas/modules/elements/elements' ) );
app.module( 'module:templates', require( './canvas/modules/templates/templates' ) );
app.module( 'module:canvas', require( './canvas/modules/canvas/canvas' ) );
app.module( 'module:tools', require( './canvas/modules/tools/tools' ) );
app.module( 'module:css', require( './canvas/modules/css/css' ) );
} );
/**
* Fires when the canvas is initialized.
*
* @since 1.5.0
*
* @param app
*/
app.channel.trigger( 'canvas:initialize', app );
} );

$( function() {

// Start the app
function start() {
app.start( {
elements : window._elements || [],
nonces : window._nonces || [],
mediaQueries : window._media_queries || {},
cssRules : window._css_rules || {}
} );
}

/**
* Fires when the canvas is initialized.
*
* @since 1.5.0
*
* @param app
*/
app.channel.trigger( 'canvas:initialize', app );
$( function() {
var sidebarApp = window.parent.app;

// Wait until the sidebar is initialized before starting the canvas
if ( sidebarApp._initialized ) {
start();
}
else {
sidebarApp.on('start', function() {
start();
} );
}
} );

} ( window, Backbone.$ ) );
Expand Down

0 comments on commit 2d530af

Please sign in to comment.