Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AJAX context saving #184

Merged
merged 3 commits into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -57,6 +57,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 @@ -224,6 +227,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 @@ -240,6 +258,14 @@
text : 'Пишите нам на team@ifmo.su'
}
},
{
type : 'personality',
data : {
name : 'Красюк Светлана Ивановна',
cite : 'Заместитель директора по учебно-воспитательной работе (начальная школа)',
url : 'http://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
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "codex.editor",
"version": "1.6.2",
"version": "1.6.3",
"description": "Codex Editor. Native JS, based on API and Open Source",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"author": "Codex Team",
"license": "ISC",
"dependencies": {
"whatwg-fetch": "^2.0.1"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
Expand Down
3 changes: 1 addition & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
module.exports = {

entry: {
"whatwg-fetch": "whatwg-fetch",
"codex-editor": "./codex"
},
output: {
Expand Down Expand Up @@ -87,7 +86,7 @@ module.exports = {
},
{
test : /\.js$/,
loader: 'eslint-loader',
loader: 'eslint-loader?fix=true',
exclude: /(node_modules)/
},
{
Expand Down
2 changes: 0 additions & 2 deletions whatwg-fetch.js

This file was deleted.

1 change: 0 additions & 1 deletion whatwg-fetch.js.map

This file was deleted.