-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Establish base Snowboard framework in Backend (#548)
This PR establishes a base Snowboard framework in the Backend. While we won't likely have any specific Snowboard widgets or functionality in the 1.1 branch, it will allow people to use Snowboard in the Backend should they wish. Fixes #541. Co-authored-by: Luke Towers <[email protected]>
- Loading branch information
1 parent
ac130c4
commit f79e672
Showing
33 changed files
with
1,251 additions
and
1,082 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Ignore build files | ||
**/node_modules/** | ||
build/*.js | ||
**/build/*.js | ||
**/mix.webpack.js | ||
|
||
# Ignore all JS except for Mix-based assets | ||
assets/js | ||
assets/vendor | ||
behaviors/**/*.js | ||
controllers/**/*.js | ||
formwidgets/**/*.js | ||
reportwidgets/**/*.js | ||
widgets/**/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"browser": true | ||
}, | ||
"globals": { | ||
"Snowboard": "writable" | ||
}, | ||
"extends": [ | ||
"airbnb-base", | ||
"plugin:vue/vue3-recommended" | ||
], | ||
"rules": { | ||
"class-methods-use-this": ["off"], | ||
"indent": ["error", 4, { | ||
"SwitchCase": 1 | ||
}], | ||
"max-len": ["off"], | ||
"new-cap": ["error", { "properties": false }], | ||
"no-alert": ["off"], | ||
"no-param-reassign": ["error", { | ||
"props": false | ||
}], | ||
"vue/html-indent": ["error", 4], | ||
"vue/html-self-closing": ["error", { | ||
"html": { | ||
"void": "never", | ||
"normal": "any", | ||
"component": "always" | ||
}, | ||
"svg": "always", | ||
"math": "always" | ||
}], | ||
"vue/multi-word-component-names": ["off"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Backend module ignores | ||
|
||
# Ignore Mix files | ||
node_modules | ||
package-lock.json | ||
mix.webpack.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Backend AJAX handler. | ||
* | ||
* This is a utility script that resolves some backwards-compatibility issues with the functionality | ||
* that relies on the old framework, and ensures that Snowboard works well within the Backend | ||
* environment. | ||
* | ||
* Functions: | ||
* - Adds the "render" jQuery event to Snowboard requests that widgets use to initialise. | ||
* - Ensures the CSRF token is included in requests. | ||
* | ||
* @copyright 2021 Winter. | ||
* @author Ben Thomson <[email protected]> | ||
*/ | ||
export default class Handler extends Snowboard.Singleton { | ||
/** | ||
* Event listeners. | ||
* | ||
* @returns {Object} | ||
*/ | ||
listens() { | ||
return { | ||
ready: 'ready', | ||
ajaxFetchOptions: 'ajaxFetchOptions', | ||
ajaxUpdateComplete: 'ajaxUpdateComplete', | ||
}; | ||
} | ||
|
||
/** | ||
* Ready handler. | ||
* | ||
* Adds the jQuery AJAX prefilter that the old framework uses to inject the CSRF token in AJAX | ||
* calls, and fires off a "render" event. | ||
*/ | ||
ready() { | ||
if (!window.jQuery) { | ||
return; | ||
} | ||
|
||
window.jQuery.ajaxPrefilter((options) => { | ||
if (this.hasToken()) { | ||
if (!options.headers) { | ||
options.headers = {}; | ||
} | ||
options.headers['X-CSRF-TOKEN'] = this.getToken(); | ||
} | ||
}); | ||
|
||
// Add "render" event for backwards compatibility | ||
window.jQuery(document).trigger('render'); | ||
} | ||
|
||
/** | ||
* Fetch options handler. | ||
* | ||
* Ensures that the CSRF token is included in Snowboard requests. | ||
* | ||
* @param {Object} options | ||
*/ | ||
ajaxFetchOptions(options) { | ||
if (this.hasToken()) { | ||
options.headers['X-CSRF-TOKEN'] = this.getToken(); | ||
} | ||
} | ||
|
||
/** | ||
* Update complete handler. | ||
* | ||
* Fires off a "render" event when partials are updated so that any widgets included in | ||
* responses are correctly initialised. | ||
*/ | ||
ajaxUpdateComplete() { | ||
if (!window.jQuery) { | ||
return; | ||
} | ||
|
||
// Add "render" event for backwards compatibility | ||
window.jQuery(document).trigger('render'); | ||
} | ||
|
||
/** | ||
* Determines if a CSRF token is available. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
hasToken() { | ||
const tokenElement = document.querySelector('meta[name="csrf-token"]'); | ||
|
||
if (!tokenElement) { | ||
return false; | ||
} | ||
if (!tokenElement.hasAttribute('content')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets the CSRF token. | ||
* | ||
* @returns {String} | ||
*/ | ||
getToken() { | ||
return document.querySelector('meta[name="csrf-token"]').getAttribute('content'); | ||
} | ||
} |
Oops, something went wrong.