Skip to content

Commit

Permalink
Improve AJAX context saving (codex-team#184)
Browse files Browse the repository at this point in the history
* Improve AJAX context saving

Value returned in beforeSend method now will be passed as context to
the Error, Success and Progress events

* resolve conflict
  • Loading branch information
neSpecc authored Apr 23, 2017
1 parent 79dfa8c commit da4d815
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 19 deletions.
64 changes: 64 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
/*
* ENVIRONMENTS
* =================
*/

// Define globals exposed by modern browsers.
"browser": true,

// Define globals exposed by Node.js.
"node": true,

// Define globals exposed by CodeX Team
"predef": [
"codex"
],

// Allow ES6.
"esversion": 6,

/*
* ENFORCING OPTIONS
* =================
*/

// Force all variable names to use either camelCase style or UPPER_CASE
// with underscores.
"camelcase": true,

// Prohibit use of == and != in favor of === and !==.
"eqeqeq": true,

// Enforce tab width of 2 spaces.
"indent": 2,

// Prohibit use of a variable before it is defined.
"latedef": true,

// Enforce line length to 100 characters
"maxlen": 120,

// Require capitalized names for constructor functions.
"newcap": true,

// Enforce use of single quotation marks for strings.
"quotmark": "single",

// Enforce placing 'use strict' at the top function scope
"strict": true,

// Prohibit use of explicitly undeclared variables.
"undef": true,

// Warn when variables are defined but never used.
"unused": true,

/*
* RELAXING OPTIONS
* =================
*/

// Suppress warnings about == null comparisons.
"eqnull": true
}
4 changes: 2 additions & 2 deletions codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion codex-editor.js.map

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<script src="plugins/attaches/attaches.js"></script>
<link rel="stylesheet" href="plugins/attaches/attaches.css">

<script src="plugins/personality/personality.js"></script>
<link rel="stylesheet" href="plugins/personality/personality.css">

<script>
codex.editor.start({
holderId : "codex-editor",
Expand Down Expand Up @@ -218,6 +221,21 @@
fetchUrl: '/test',
maxSize: 50000,
}
},
personality: {
type : 'personality',
displayInToolbox : true,
iconClassname : 'cdx-personality-icon',
prepare : cdxEditorPersonality.prepare,
render : cdxEditorPersonality.render,
save : cdxEditorPersonality.save,
validate : cdxEditorPersonality.validate,
destroy : cdxEditorPersonality.destroy,
enableLineBreaks : true,
showInlineToolbar: true,
config: {
uploadURL: '/uploadPhoto',
}
}
},
data : {
Expand All @@ -234,6 +252,14 @@
text : 'Пишите нам на [email protected]'
}
},
{
type : 'personality',
data : {
name : 'Красюк Светлана Ивановна',
cite : 'Заместитель директора по учебно-воспитательной работе (начальная школа)',
url : 'https://new.school332.ru/user/2'
}
},
{
type : 'list',
data : {
Expand Down
37 changes: 26 additions & 11 deletions modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ module.exports = (function (core) {

/**
* Native Ajax
* @param {String} settings.url - request URL
* @param {function} settings.beforeSend - returned value will be passed as context to the Success, Error and Progress callbacks
* @param {function} settings.success
* @param {function} settings.progress
*/
core.ajax = function (settings) {

Expand Down Expand Up @@ -186,9 +190,18 @@ module.exports = (function (core) {

}

if (settings.beforeSend && typeof settings.beforeSend == 'function') {
/**
* Value returned in beforeSend funtion will be passed as context to the other response callbacks
* If beforeSend returns false, AJAX will be blocked
*/
let responseContext,
beforeSendResult;

if (typeof settings.beforeSend === 'function') {

if (settings.beforeSend() === false) {
beforeSendResult = settings.beforeSend.call();

if (beforeSendResult === false) {

return;

Expand All @@ -205,7 +218,7 @@ module.exports = (function (core) {

if (!isFormData) {

if (settings.type != 'POST') {
if (settings.type !== 'POST') {

XMLHTTP.setRequestHeader('Content-type', settings['content-type']);

Expand All @@ -219,29 +232,31 @@ module.exports = (function (core) {

XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

if (typeof settings.progress == 'function') {
responseContext = beforeSendResult || XMLHTTP;

if (typeof settings.progress === 'function') {

XMLHTTP.upload.onprogress = settings.progress;
XMLHTTP.upload.onprogress = settings.progress.bind(responseContext);

}

XMLHTTP.onreadystatechange = function () {

if (XMLHTTP.readyState == 4) {
if (XMLHTTP.readyState === 4) {

if (XMLHTTP.status == 200) {
if (XMLHTTP.status === 200) {

if (typeof settings.success == 'function') {
if (typeof settings.success === 'function') {

settings.success(XMLHTTP.responseText);
settings.success.call(responseContext, XMLHTTP.responseText);

}

} else {

if (typeof settings.error == 'function') {
if (typeof settings.error === 'function') {

settings.error(XMLHTTP.responseText);
settings.error.call(responseContext, XMLHTTP.responseText, XMLHTTP.status);

}

Expand Down
2 changes: 1 addition & 1 deletion modules/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = (function (sanitizer) {
* @param {String} dirtyString - taint string
* @param {Object} customConfig - allowed tags
*/
sanitizer.clean = function(dirtyString, customConfig) {
sanitizer.clean = function (dirtyString, customConfig) {

let janitorInstance = init_(customConfig);

Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
},
"author": "Codex Team",
"license": "ISC",
"dependencies": {
"whatwg-fetch": "^2.0.1"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
},
{
test : /\.js$/,
loader: 'eslint-loader',
loader: 'eslint-loader?fix=true',
exclude: /(node_modules)/
},
{
Expand Down

0 comments on commit da4d815

Please sign in to comment.