Improve AJAX context saving (#184)

* Improve AJAX context saving

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

* resolve conflict
This commit is contained in:
Peter Savchenko 2017-04-23 17:11:03 +03:00 committed by GitHub
parent 79dfa8cbe7
commit da4d8153d7
8 changed files with 121 additions and 19 deletions

64
.jshintrc Normal file
View file

@ -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
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -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",
@ -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 : {
@ -234,6 +252,14 @@
text : 'Пишите нам на team@ifmo.su'
}
},
{
type : 'personality',
data : {
name : 'Красюк Светлана Ивановна',
cite : 'Заместитель директора по учебно-воспитательной работе (начальная школа)',
url : 'http://new.school332.ru/user/2'
}
},
{
type : 'list',
data : {

View file

@ -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) {
@ -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 (settings.beforeSend() === false) {
if (typeof settings.beforeSend === 'function') {
beforeSendResult = settings.beforeSend.call();
if (beforeSendResult === false) {
return;
@ -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']);
@ -219,29 +232,31 @@ module.exports = (function (core) {
XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
if (typeof settings.progress == 'function') {
responseContext = beforeSendResult || XMLHTTP;
XMLHTTP.upload.onprogress = settings.progress;
if (typeof settings.progress === 'function') {
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);
}

View file

@ -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);

View file

@ -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",

View file

@ -86,7 +86,7 @@ module.exports = {
},
{
test : /\.js$/,
loader: 'eslint-loader',
loader: 'eslint-loader?fix=true',
exclude: /(node_modules)/
},
{