이미지 편집 수정 중
@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* 전달받은 이미지를 base64로 인코딩한다
|
||||||
|
* @param file - 이미지 파일 또는 이미지 URL
|
||||||
|
* @param maxWidth - 인코딩 시의 이미지 max width 사이즈
|
||||||
|
* @returns {Promise<any>}
|
||||||
|
*/
|
||||||
|
var readImage = () => {
|
||||||
|
let image = this.$refs.image.src
|
||||||
|
this.toBlob(image)
|
||||||
|
.then(res => {
|
||||||
|
console.log(res)
|
||||||
|
this.image = {
|
||||||
|
filename: res.name,
|
||||||
|
size: res.size,
|
||||||
|
type: res.type,
|
||||||
|
lastModified: res.lastModified
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 이미지 url을 blob 파일로 변환하여 전달한다
|
||||||
|
* @param url
|
||||||
|
* @returns {Promise<any>}
|
||||||
|
*/
|
||||||
|
var toBlob = (url) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.base64Encode(url)
|
||||||
|
.then(res => {
|
||||||
|
let byteString = atob(res.dataUrl)
|
||||||
|
let ab = new ArrayBuffer(byteString.length)
|
||||||
|
let ia = new Uint8Array(ab)
|
||||||
|
|
||||||
|
for (let i = 0; i < byteString.length; i++) {
|
||||||
|
ia[i] = byteString.charCodeAt(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the array buffer to blob
|
||||||
|
let blob = new Blob([ab], {type: 'image/' + res.type})
|
||||||
|
|
||||||
|
let formData = new FormData()
|
||||||
|
formData.append('file', blob, res.name)
|
||||||
|
resolve(formData.get('file'))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 전달받은 이미지를 base64로 인코딩한다
|
||||||
|
*/
|
||||||
|
var base64Encode = (url) => {
|
||||||
|
const re = new RegExp('.(gif|jpg|jpeg|tiff|png|ico)$', 'i')
|
||||||
|
let name = (/[^(/|\\)]*$/).exec(url)[0]
|
||||||
|
let type = re.test(name) ? re.exec(name)[0].replace('.', '') : 'jpg'
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let image = new Image()
|
||||||
|
|
||||||
|
image.onload = function (event) {
|
||||||
|
let canvas = document.createElement('canvas')
|
||||||
|
// draw canvas
|
||||||
|
canvas.width = image.naturalWidth
|
||||||
|
canvas.height = image.naturalHeight
|
||||||
|
canvas.getContext('2d').drawImage(image, 0, 0)
|
||||||
|
|
||||||
|
let dataUrl = canvas.toDataURL('image/' + type)
|
||||||
|
resolve({
|
||||||
|
name: name,
|
||||||
|
type: type,
|
||||||
|
dataUrl: dataUrl.split(',')[1]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
image.onerror = function () {
|
||||||
|
let msg = `"${file}"을 로딩하는 데 오류가 발생하였습니다. 이미지 파일을 확인해주세요.`
|
||||||
|
alert(msg)
|
||||||
|
console.error(msg)
|
||||||
|
}
|
||||||
|
image.crossOrigin = 'anonymous';
|
||||||
|
image.src = url
|
||||||
|
})
|
||||||
|
}
|
@ -1,363 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2009 Mihai Şucan
|
|
||||||
*
|
|
||||||
* This file is part of PaintWeb.
|
|
||||||
*
|
|
||||||
* PaintWeb is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* PaintWeb is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* $URL: http://code.google.com/p/paintweb $
|
|
||||||
* $Date: 2009-10-29 19:05:49 +0200 $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a lang="ro" href="http://www.robodesign.ro/mihai">Mihai Şucan</a>
|
|
||||||
* @fileOverview Holds the integration code for PaintWeb inside <a
|
|
||||||
* href="http://www.moodle.org">Moodle</a>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @class The Moodle extension for PaintWeb. This extension handles the Moodle
|
|
||||||
* integration inside the PaintWeb code.
|
|
||||||
*
|
|
||||||
* <p><strong>Note:</strong> This extension is supposed to work with Moodle 1.9
|
|
||||||
* and Moodle 2.0.
|
|
||||||
*
|
|
||||||
* @param {PaintWeb} app Reference to the main paint application object.
|
|
||||||
*/
|
|
||||||
pwlib.extensions.moodle = function (app) {
|
|
||||||
var _self = this,
|
|
||||||
appEvent = pwlib.appEvent,
|
|
||||||
config = app.config,
|
|
||||||
gui = app.gui,
|
|
||||||
lang = app.lang.moodle,
|
|
||||||
moodleServer = config.moodleServer,
|
|
||||||
tinymceEditor = null,
|
|
||||||
qfErrorShown = false;
|
|
||||||
|
|
||||||
// Holds information related to Moodle.
|
|
||||||
var moodleInfo = {
|
|
||||||
// Holds the URL of the image the user is saving.
|
|
||||||
imageURL: null,
|
|
||||||
|
|
||||||
// The class name for the element which holds the textarea buttons (toggle
|
|
||||||
// on/off).
|
|
||||||
// This element exists only in Moodle 1.9.
|
|
||||||
textareaButtons: 'textareaicons',
|
|
||||||
|
|
||||||
// The image save handler script on the server-side. The path is relative to
|
|
||||||
// the PaintWeb base folder.
|
|
||||||
// This used with Moodle 2.0.
|
|
||||||
imageSaveHandler20: '../ext/moodle/imagesave20.php',
|
|
||||||
|
|
||||||
// The image save handler script for Moodle 1.9.
|
|
||||||
imageSaveHandler19: '../ext/moodle/imagesave19.php',
|
|
||||||
|
|
||||||
// This holds the release version of Moodle being used. This should be 1.9
|
|
||||||
// or 2.0.
|
|
||||||
release: 0,
|
|
||||||
|
|
||||||
// Moodle 2.0 draft item ID used for file storage.
|
|
||||||
draftitemid: null
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>extensionRegister</code> event handler. Setup event listeners,
|
|
||||||
* determine Moodle version, and more.
|
|
||||||
*
|
|
||||||
* @returns {Boolean} True if the extension initialized successfully, or false
|
|
||||||
* if not.
|
|
||||||
*/
|
|
||||||
this.extensionRegister = function () {
|
|
||||||
// Register application events.
|
|
||||||
app.events.add('guiShow', this.guiShow);
|
|
||||||
app.events.add('guiHide', this.guiHide);
|
|
||||||
app.events.add('imageSave', this.imageSave);
|
|
||||||
|
|
||||||
if (moodleServer && moodleServer.release) {
|
|
||||||
var matches = moodleServer.release.match(/^\s*(\d+\.\d+)/);
|
|
||||||
if (matches && matches[1]) {
|
|
||||||
moodleInfo.release = parseFloat(matches[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof window.qf_errorHandler === 'function' && config.tinymce &&
|
|
||||||
!config.tinymce.onSubmitUnsaved) {
|
|
||||||
config.tinymce.onSubmitUnsaved = this.onSubmitUnsaved;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>submit</code> event handler for the form to which the PaintWeb
|
|
||||||
* instance is attached to. This method is invoked by the TinyMCE plugin when
|
|
||||||
* the form is submitted while the user edits an image with unsaved changes.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this.onSubmitUnsaved = function () {
|
|
||||||
var tmce = config.tinymceEditor,
|
|
||||||
textarea = tmce ? tmce.getElement() : null,
|
|
||||||
guiPlaceholder = config.guiPlaceholder,
|
|
||||||
prevSibling = guiPlaceholder.previousSibling;
|
|
||||||
|
|
||||||
if (tmce && textarea && window.qf_errorHandler) {
|
|
||||||
try {
|
|
||||||
qf_errorHandler(textarea, "\n - " + lang.errorSubmitUnsaved);
|
|
||||||
} catch (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
qfErrorShown = true;
|
|
||||||
|
|
||||||
// Due to the styling of the error shown by Moodle, PaintWeb must have
|
|
||||||
// clear:right.
|
|
||||||
if (prevSibling && prevSibling.className &&
|
|
||||||
prevSibling.className.indexOf('paintweb_tinymce_status') !== -1) {
|
|
||||||
prevSibling.style.clear = 'right';
|
|
||||||
} else {
|
|
||||||
guiPlaceholder.style.clear = 'right';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>imageSave</code> application event handler. When the user
|
|
||||||
* attempts to save an image, this extension handles the event by sending the
|
|
||||||
* image data to the Moodle server, to perform the actual save operation.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {pwlib.appEvent.imageSave} ev The application event object.
|
|
||||||
*/
|
|
||||||
this.imageSave = function (ev) {
|
|
||||||
if (!ev.dataURL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ev.preventDefault();
|
|
||||||
|
|
||||||
moodleInfo.imageURL = config.imageLoad.src;
|
|
||||||
if (!moodleInfo.imageURL || moodleInfo.imageURL.substr(0, 5) === 'data:') {
|
|
||||||
moodleInfo.imageURL = '-';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.moodleSaveMethod === 'dataURL') {
|
|
||||||
app.events.dispatch(new appEvent.imageSaveResult(true,
|
|
||||||
moodleInfo.imageURL, ev.dataURL));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
var handlerURL = PaintWeb.baseFolder,
|
|
||||||
send = 'url=' + encodeURIComponent(moodleInfo.imageURL) +
|
|
||||||
'&dataURL=' + encodeURIComponent(ev.dataURL),
|
|
||||||
headers = {'Content-Type': 'application/x-www-form-urlencoded'};
|
|
||||||
|
|
||||||
// In Moodle 2.0 we include the context ID and the draft item ID, such
|
|
||||||
// that the image save script can properly save the new image inside the
|
|
||||||
// current draft area of the current textarea element.
|
|
||||||
if (moodleInfo.release >= 2) {
|
|
||||||
handlerURL += moodleInfo.imageSaveHandler20;
|
|
||||||
if (moodleServer.contextid) {
|
|
||||||
send += '&contextid=' + encodeURIComponent(moodleServer.contextid);
|
|
||||||
}
|
|
||||||
if (moodleInfo.draftitemid) {
|
|
||||||
send += '&draftitemid=' + encodeURIComponent(moodleInfo.draftitemid);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
handlerURL += moodleInfo.imageSaveHandler19;
|
|
||||||
}
|
|
||||||
|
|
||||||
pwlib.xhrLoad(handlerURL, imageSaveReady, 'POST', send, headers);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The image save <code>onreadystatechange</code> event handler for the
|
|
||||||
* <code>XMLHttpRequest</code> which performs the image save. This function
|
|
||||||
* uses the reply to determine if the image save operation is successful or
|
|
||||||
* not.
|
|
||||||
*
|
|
||||||
* <p>The {@link pwlib.appEvent.imageSaveResult} application event is
|
|
||||||
* dispatched.
|
|
||||||
*
|
|
||||||
* <p>The server-side script must reply with a JSON object with the following
|
|
||||||
* properties:
|
|
||||||
*
|
|
||||||
* <ul>
|
|
||||||
* <li><var>successful</var> which tells if the image save operation was
|
|
||||||
* successful or not;
|
|
||||||
*
|
|
||||||
* <li><var>url</var> which must tell the same URL as the image we just
|
|
||||||
* saved (sanity/security check);
|
|
||||||
*
|
|
||||||
* <li><var>urlNew</var> is optional. This allows the server-side script to
|
|
||||||
* change the image URL;
|
|
||||||
*
|
|
||||||
* <li><var>errorMessage</var> is optional. When the image save was not
|
|
||||||
* successful, an error message can be displayed.
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {XMLHttpRequest} xhr The XMLHttpRequest object.
|
|
||||||
*/
|
|
||||||
function imageSaveReady (xhr) {
|
|
||||||
if (!xhr || xhr.readyState !== 4) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = {successful: false, url: moodleInfo.imageURL};
|
|
||||||
|
|
||||||
if ((xhr.status !== 304 && xhr.status !== 200) || !xhr.responseText) {
|
|
||||||
alert(lang.xhrRequestFailed);
|
|
||||||
|
|
||||||
app.events.dispatch(new appEvent.imageSaveResult(false, result.url, null,
|
|
||||||
lang.xhrRequestFailed));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
result = JSON.parse(xhr.responseText);
|
|
||||||
} catch (err) {
|
|
||||||
result.errorMessage = lang.jsonParseFailed + "\n" + err;
|
|
||||||
alert(result.errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.successful) {
|
|
||||||
if (result.url !== moodleInfo.imageURL) {
|
|
||||||
alert(pwlib.strf(lang.urlMismatch, {
|
|
||||||
url: moodleInfo.imageURL,
|
|
||||||
urlServer: result.url || 'null'}));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (result.errorMessage) {
|
|
||||||
alert(lang.imageSaveFailed + "\n" + result.errorMessage);
|
|
||||||
} else {
|
|
||||||
alert(lang.imageSaveFailed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.events.dispatch(new appEvent.imageSaveResult(result.successful,
|
|
||||||
result.url, result.urlNew, result.errorMessage));
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>guiShow</code> application event handler. When the PaintWeb GUI
|
|
||||||
* is shown, we must hide the textarea icons for the current textarea element,
|
|
||||||
* inside a Moodle page.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this.guiShow = function () {
|
|
||||||
var pNode = config.guiPlaceholder.parentNode,
|
|
||||||
textareaButtons
|
|
||||||
= pNode.getElementsByClassName(moodleInfo.textareaButtons)[0];
|
|
||||||
|
|
||||||
// These show in Moodle 1.9.
|
|
||||||
if (textareaButtons) {
|
|
||||||
textareaButtons.style.display = 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
qfErrorShown = false;
|
|
||||||
|
|
||||||
// For Moodle 2.0 we must determine the draft item ID in order to properly
|
|
||||||
// perform the image save operation into the current draft area.
|
|
||||||
if (moodleInfo.release < 2) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Typically the TinyMCE editor instance is attached to a textarea element
|
|
||||||
// which has a name=whatever[text] or similar form. In the same form as the
|
|
||||||
// textarea, there must be a hidden input element with the
|
|
||||||
// name=whatever[itemid]. The value of that input holds the draft item ID.
|
|
||||||
var tmce = config.tinymceEditor,
|
|
||||||
textarea = tmce ? tmce.getElement() : null,
|
|
||||||
frm = textarea ? textarea.form : null;
|
|
||||||
|
|
||||||
if (!tmce || !textarea || !textarea.name || !frm) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fieldname = textarea.name.replace(/\[text\]$/, '');
|
|
||||||
if (!fieldname) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var draftitemid = frm.elements.namedItem(fieldname + '[itemid]'),
|
|
||||||
format = frm.elements.namedItem(fieldname + '[format]');
|
|
||||||
|
|
||||||
if (draftitemid) {
|
|
||||||
moodleInfo.draftitemid = draftitemid.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format) {
|
|
||||||
format.style.display = 'none';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>guiHide</code> application event handler. When the PaintWeb GUI
|
|
||||||
* is hidden, we must show again the textarea icons for the current textarea
|
|
||||||
* element, inside a Moodle page.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this.guiHide = function () {
|
|
||||||
var guiPlaceholder = config.guiPlaceholder,
|
|
||||||
prevSibling = guiPlaceholder.previousSibling;
|
|
||||||
pNode = guiPlaceholder.parentNode,
|
|
||||||
textareaButtons
|
|
||||||
= pNode.getElementsByClassName(moodleInfo.textareaButtons)[0];
|
|
||||||
|
|
||||||
// These show in Moodle 1.9.
|
|
||||||
if (textareaButtons) {
|
|
||||||
textareaButtons.style.display = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmce = config.tinymceEditor,
|
|
||||||
textarea = tmce ? tmce.getElement() : null,
|
|
||||||
frm = textarea ? textarea.form : null;
|
|
||||||
|
|
||||||
if (!tmce || !textarea || !textarea.name || !frm) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (qfErrorShown) {
|
|
||||||
if (window.qf_errorHandler) {
|
|
||||||
qf_errorHandler(textarea, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prevSibling && prevSibling.className &&
|
|
||||||
prevSibling.className.indexOf('paintweb_tinymce_status') !== -1) {
|
|
||||||
prevSibling.style.clear = '';
|
|
||||||
} else {
|
|
||||||
guiPlaceholder.style.clear = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The format input element only shows in Moodle 2.0.
|
|
||||||
if (moodleInfo.release >= 2) {
|
|
||||||
var fieldname = textarea.name.replace(/\[text\]$/, '');
|
|
||||||
if (!fieldname) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var format = frm.elements.namedItem(fieldname + '[format]');
|
|
||||||
|
|
||||||
if (format) {
|
|
||||||
format.style.display = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// vim:set spell spl=en fo=wan1croqlt tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix:
|
|
||||||
|
|
@ -1,451 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2008, 2009 Mihai Şucan
|
|
||||||
*
|
|
||||||
* This file is part of PaintWeb.
|
|
||||||
*
|
|
||||||
* PaintWeb is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* PaintWeb is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* $URL: http://code.google.com/p/paintweb $
|
|
||||||
* $Date: 2009-05-11 19:37:56 +0300 $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a lang="ro" href="http://www.robodesign.ro/mihai">Mihai Şucan</a>
|
|
||||||
* @fileOverview Minimal code used for aiding debugging PaintWeb.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Opera compatibility
|
|
||||||
if (!window.console) {
|
|
||||||
/**
|
|
||||||
* @namespace Holds a simple method used for debugging. Opera doesn't have the
|
|
||||||
* window.console API like Firefox+Firebug has.
|
|
||||||
*/
|
|
||||||
window.console = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!window.console.log) {
|
|
||||||
/**
|
|
||||||
* Display a message in the debugger. If available, opera.postError is used,
|
|
||||||
* otherwise no message is displayed.
|
|
||||||
*
|
|
||||||
* @param {mixed} mixed Any number of arguments, each one is displayed in the
|
|
||||||
* debugger.
|
|
||||||
*/
|
|
||||||
window.console.log = function () {
|
|
||||||
var msg = '';
|
|
||||||
|
|
||||||
for (var i = 0, n = arguments.length; i < n; i++) {
|
|
||||||
msg += arguments[i] + ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (window.opera && window.opera.postError) {
|
|
||||||
opera.postError(msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!window.console.warn) {
|
|
||||||
/**
|
|
||||||
* Display a message in the debugger. If available, opera.postError is used,
|
|
||||||
* otherwise no warning is displayed.
|
|
||||||
*
|
|
||||||
* @param {mixed} mixed Any number of arguments, each one is displayed in the
|
|
||||||
* debugger.
|
|
||||||
*/
|
|
||||||
window.console.warn = function () {
|
|
||||||
console.log.apply(null, arguments);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JavaScript code performance profiler.
|
|
||||||
* <p>Nested timers are accounted for - see the example below.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* <code>// To profile your code just do:
|
|
||||||
* var profiler = new $.profiler();
|
|
||||||
*
|
|
||||||
* profiler.start('long test');
|
|
||||||
* // ... more code ...
|
|
||||||
* profiler.start('function 1');
|
|
||||||
* // ... more code ...
|
|
||||||
* profiler.stop('function 1');
|
|
||||||
* // ... more code ...
|
|
||||||
* profiler.start('function 2');
|
|
||||||
* // ... more code ...
|
|
||||||
* profiler.stop('function 2');
|
|
||||||
* // ... more code ...
|
|
||||||
* profiler.stop('long test');
|
|
||||||
*
|
|
||||||
* // To see the results do:
|
|
||||||
* profiler.reportText();
|
|
||||||
* // or ..
|
|
||||||
* profiler.reportData();</code>
|
|
||||||
*
|
|
||||||
* @class JavaScript code performance profiler.
|
|
||||||
*/
|
|
||||||
var libProfiler = function () {
|
|
||||||
/**
|
|
||||||
* @ignore
|
|
||||||
* @class Function timer. This is the constructor used for instancing a single
|
|
||||||
* timer object created by the profiler.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {String} name_ The timer name.
|
|
||||||
* @param {Boolean} [independent_=false] Tells if the timer is independent.
|
|
||||||
* Means this timer will not affect the timers execution stack.
|
|
||||||
*/
|
|
||||||
function fnTimer (name_, independent_) {
|
|
||||||
this.avgOwnTimePerCall = 0;
|
|
||||||
this.avgRunTimePerCall = 0;
|
|
||||||
this.calls = 0;
|
|
||||||
this.maxOwnTimePerCall = 0;
|
|
||||||
this.maxRunTimePerCall = 0;
|
|
||||||
this.minOwnTimePerCall = 0;
|
|
||||||
this.minRunTimePerCall = 0;
|
|
||||||
this.name = name_;
|
|
||||||
this.ownTimeTotal = 0;
|
|
||||||
this.runTimeTotal = 0;
|
|
||||||
this.state = fnTimer.STATE_NONE;
|
|
||||||
this.independent = independent_;
|
|
||||||
|
|
||||||
var startTime_ = 0,
|
|
||||||
subTimerStart_ = 0,
|
|
||||||
subTime_ = 0,
|
|
||||||
stack_ = 0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Start timing function execution.
|
|
||||||
*/
|
|
||||||
this.start = function () {
|
|
||||||
if (this.state == fnTimer.STATE_START ||
|
|
||||||
this.state == fnTimer.STATE_SUB) {
|
|
||||||
stack_++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime_ = (new Date ()).getTime();
|
|
||||||
this.state = fnTimer.STATE_START;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Stop timing function execution.
|
|
||||||
*/
|
|
||||||
this.stop = function () {
|
|
||||||
if (this.state == fnTimer.STATE_SUB) {
|
|
||||||
this.subTimerEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.state != fnTimer.STATE_START) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.calls++;
|
|
||||||
|
|
||||||
if (stack_) {
|
|
||||||
stack_--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var runTime = (new Date ()).getTime() - startTime_;
|
|
||||||
var ownTime = runTime - subTime_;
|
|
||||||
subTime_ = 0;
|
|
||||||
|
|
||||||
this.runTimeTotal += runTime;
|
|
||||||
this.ownTimeTotal += ownTime;
|
|
||||||
|
|
||||||
this.avgRunTimePerCall = this.runTimeTotal / this.calls;
|
|
||||||
this.avgOwnTimePerCall = this.ownTimeTotal / this.calls;
|
|
||||||
|
|
||||||
if (runTime < this.minRunTimePerCall) {
|
|
||||||
this.minRunTimePerCall = runTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (runTime > this.maxRunTimePerCall) {
|
|
||||||
this.maxRunTimePerCall = runTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ownTime < this.minOwnTimePerCall) {
|
|
||||||
this.minOwnTimePerCall = ownTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ownTime > this.maxOwnTimePerCall) {
|
|
||||||
this.maxOwnTimePerCall = ownTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.state = fnTimer.STATE_STOP;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Start timing sub-function execution. The sub-function execution timer is
|
|
||||||
* used for calculating the ownTime (runTime - sub-function execution time).
|
|
||||||
*/
|
|
||||||
this.subTimerStart = function () {
|
|
||||||
if (this.independent || this.state != fnTimer.STATE_START) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
subTimerStart_ = (new Date()).getTime();
|
|
||||||
|
|
||||||
this.state = fnTimer.STATE_SUB;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Stop timing sub-function execution.
|
|
||||||
*/
|
|
||||||
this.subTimerEnd = function () {
|
|
||||||
if (this.independent || this.state != fnTimer.STATE_SUB) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
subTime_ += (new Date()).getTime() - subTimerStart_;
|
|
||||||
this.state = fnTimer.STATE_START;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
fnTimer.STATE_NONE = 0;
|
|
||||||
fnTimer.STATE_START = 1;
|
|
||||||
fnTimer.STATE_SUB = 2;
|
|
||||||
fnTimer.STATE_STOP = 3;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Holds the timer objects.
|
|
||||||
*/
|
|
||||||
this.timers = {};
|
|
||||||
|
|
||||||
var activeTimer_ = null,
|
|
||||||
timersStack_ = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start/create a function timer.
|
|
||||||
*
|
|
||||||
* @param {String} name The timer name.
|
|
||||||
* @param {Boolean} [independent=false] Tells if the timer should be
|
|
||||||
* independent or not. This means that this new function timer is not be
|
|
||||||
* considered affecting the execution time of existing function timers in the
|
|
||||||
* call stack.
|
|
||||||
*/
|
|
||||||
this.start = function (name, independent) {
|
|
||||||
var timer = this.timers[name];
|
|
||||||
if (!timer) {
|
|
||||||
timer = this.timers[name] = new fnTimer(name, independent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!timer.independent && activeTimer_ != name) {
|
|
||||||
var activeTimer = activeTimer_ ? this.timers[activeTimer_] : null;
|
|
||||||
|
|
||||||
if (activeTimer && activeTimer.state == fnTimer.STATE_START) {
|
|
||||||
timersStack_.push(activeTimer_);
|
|
||||||
activeTimer.subTimerStart();
|
|
||||||
}
|
|
||||||
|
|
||||||
activeTimer_ = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer.start();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop a function timer.
|
|
||||||
*/
|
|
||||||
this.stop = function (name) {
|
|
||||||
var timer = this.timers[name];
|
|
||||||
if (!timer) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer.stop();
|
|
||||||
|
|
||||||
if (timer.independent || name != activeTimer_ ||
|
|
||||||
name == activeTimer_ && timer.state == fnTimer.STATE_START) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timersStack_.length > 0) {
|
|
||||||
activeTimer_ = timersStack_.pop();
|
|
||||||
|
|
||||||
var activeTimer = this.timers[activeTimer_];
|
|
||||||
activeTimer.subTimerEnd();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
activeTimer_ = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate timers report data.
|
|
||||||
*
|
|
||||||
* @returns {Object} Holds all the information gathered by the timers.
|
|
||||||
*/
|
|
||||||
this.reportData = function () {
|
|
||||||
var name, timer, timerDetails,
|
|
||||||
data = {
|
|
||||||
avgCallsPerTimer: 0,
|
|
||||||
avgOwnTimePerCall: 0,
|
|
||||||
avgOwnTimePerTimer: 0,
|
|
||||||
avgRunTimePerCall: 0,
|
|
||||||
avgRunTimePerTimer: 0,
|
|
||||||
callsTotal: 0,
|
|
||||||
maxCallsPerTimer: 0,
|
|
||||||
maxCallsPerTimerName: '',
|
|
||||||
maxOwnTimePerCall: 0,
|
|
||||||
maxOwnTimePerCallName: '',
|
|
||||||
maxRunTimePerCall: 0,
|
|
||||||
maxRunTimePerCallName: '',
|
|
||||||
minCallsPerTimer: 0,
|
|
||||||
minCallsPerTimerName: '',
|
|
||||||
minOwnTimePerCall: 0,
|
|
||||||
minOwnTimePerCallName: '',
|
|
||||||
minRunTimePerCall: 0,
|
|
||||||
minRunTimePerCallName: '',
|
|
||||||
ownTimeTotal: 0,
|
|
||||||
runTimeTotal: 0,
|
|
||||||
timers: 0,
|
|
||||||
timerDetails: []
|
|
||||||
};
|
|
||||||
|
|
||||||
for (name in this.timers) {
|
|
||||||
timer = this.timers[name];
|
|
||||||
if (timer.state != fnTimer.STATE_STOP) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
timerDetails = {
|
|
||||||
name: name,
|
|
||||||
avgOwnTimePerCall: timer.avgOwnTimePerCall,
|
|
||||||
avgRunTimePerCall: timer.avgRunTimePerCall,
|
|
||||||
calls: timer.calls,
|
|
||||||
maxOwnTimePerCall: timer.maxOwnTimePerCall,
|
|
||||||
maxRunTimePerCall: timer.maxRunTimePerCall,
|
|
||||||
minOwnTimePerCall: timer.minOwnTimePerCall,
|
|
||||||
minRunTimePerCall: timer.minRunTimePerCall,
|
|
||||||
runTimeTotal: timer.runTimeTotal,
|
|
||||||
ownTimeTotal: timer.ownTimeTotal
|
|
||||||
};
|
|
||||||
data.timerDetails.push(timerDetails);
|
|
||||||
|
|
||||||
if (timer.calls > data.maxCallsPerTimer || !data.timers) {
|
|
||||||
data.maxCallsPerTimer = timer.calls;
|
|
||||||
data.maxCallsPerTimerName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer.maxOwnTimePerCall > data.maxOwnTimePerCall || !data.timers) {
|
|
||||||
data.maxOwnTimePerCall = timer.maxOwnTimePerCall;
|
|
||||||
data.maxOwnTimePerCallName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer.maxRunTimePerCall > data.maxRunTimePerCall || !data.timers) {
|
|
||||||
data.maxRunTimePerCall = timer.maxRunTimePerCall;
|
|
||||||
data.maxRunTimePerCallName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer.calls < data.minCallsPerTimer || !data.timers) {
|
|
||||||
data.minCallsPerTimer = timer.calls;
|
|
||||||
data.minCallsPerTimerName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer.minOwnTimePerCall < data.minOwnTimePerCall || !data.timers) {
|
|
||||||
data.minOwnTimePerCall = timer.minOwnTimePerCall;
|
|
||||||
data.minOwnTimePerCallName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer.minRunTimePerCall < data.minRunTimePerCall || !data.timers) {
|
|
||||||
data.minRunTimePerCall = timer.minRunTimePerCall;
|
|
||||||
data.minRunTimePerCallName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.runTimeTotal += timer.runTimeTotal;
|
|
||||||
data.ownTimeTotal += timer.ownTimeTotal;
|
|
||||||
data.callsTotal += timer.calls;
|
|
||||||
data.timers++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.timers == 0) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.avgCallsPerTimer = data.callsTotal / data.timers;
|
|
||||||
data.avgOwnTimePerCall = data.ownTimeTotal / data.callsTotal;
|
|
||||||
data.avgOwnTimePerTimer = data.ownTimeTotal / data.timers;
|
|
||||||
data.avgRunTimePerCall = data.runTimeTotal / data.callsTotal;
|
|
||||||
data.avgRunTimePerTimer = data.runTimeTotal / data.timers;
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a report in text format.
|
|
||||||
*
|
|
||||||
* @returns {String} All the information gathered by the timers, as text.
|
|
||||||
*/
|
|
||||||
this.reportText = function () {
|
|
||||||
var data = this.reportData(),
|
|
||||||
timer, result = '';
|
|
||||||
|
|
||||||
if (!data.timers) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < data.timers; i++) {
|
|
||||||
timer = data.timerDetails[i];
|
|
||||||
result += timer.name + ":\n" +
|
|
||||||
' Avg ownTime / call: ' + timer.avgOwnTimePerCall + " ms\n" +
|
|
||||||
' Avg runTime / call: ' + timer.avgRunTimePerCall + " ms\n" +
|
|
||||||
' Calls: ' + timer.calls + "\n"+
|
|
||||||
' Max ownTime / call: ' + timer.maxOwnTimePerCall + " ms\n" +
|
|
||||||
' Max runTime / call: ' + timer.maxRunTimePerCall + " ms\n" +
|
|
||||||
' Min ownTime / call: ' + timer.minOwnTimePerCall + " ms\n" +
|
|
||||||
' Min runTime / call: ' + timer.minRunTimePerCall + " ms\n" +
|
|
||||||
' runTime: ' + timer.runTimeTotal + " ms\n" +
|
|
||||||
' ownTime: ' + timer.ownTimeTotal + " ms\n\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
result += "Overview info:\n" +
|
|
||||||
' Avg calls / timer: ' + data.avgCallsPerTimer + "\n" +
|
|
||||||
' Avg ownTime / call: ' + data.avgOwnTimePerCall + " ms\n" +
|
|
||||||
' Avg ownTime / timer: ' + data.avgOwnTimePerTimer + " ms\n" +
|
|
||||||
' Avg runTime / call: ' + data.avgRunTimePerCall + " ms\n" +
|
|
||||||
' Avg runTime / timer: ' + data.avgRunTimePerTimer + " ms\n" +
|
|
||||||
' Calls total: ' + data.callsTotal + "\n" +
|
|
||||||
' Max calls / timer: ' + data.maxCallsPerTimer + ' (' +
|
|
||||||
data.maxCallsPerTimerName + ")\n" +
|
|
||||||
' Max ownTime / call: ' + data.maxOwnTimePerCall + ' ms (' +
|
|
||||||
data.maxOwnTimePerCallName + ")\n" +
|
|
||||||
' Max runTime / call: ' + data.maxRunTimePerCall + ' ms (' +
|
|
||||||
data.maxRunTimePerCallName + ")\n" +
|
|
||||||
' Min calls / timer: ' + data.minCallsPerTimer + ' (' +
|
|
||||||
data.minCallsPerTimerName + ")\n" +
|
|
||||||
' Min ownTime / call: ' + data.minOwnTimePerCall + ' ms (' +
|
|
||||||
data.minOwnTimePerCallName + ")\n" +
|
|
||||||
' Min runTime / call: ' + data.minRunTimePerCall + ' ms (' +
|
|
||||||
data.minRunTimePerCallName + ")\n" +
|
|
||||||
' Accumulated ownTime: ' + data.ownTimeTotal + " ms\n" +
|
|
||||||
' Accumulated runTime: ' + data.runTimeTotal + " ms\n" +
|
|
||||||
' Timers: ' + data.timers;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reset/clear all the timers.
|
|
||||||
*/
|
|
||||||
this.reset = function () {
|
|
||||||
this.timers = {};
|
|
||||||
activeTimer_ = null;
|
|
||||||
timersStack_ = [];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// vim:set spell spl=en fo=wan1croqlt tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix:
|
|
||||||
|
|
@ -1,478 +0,0 @@
|
|||||||
/*
|
|
||||||
http://www.JSON.org/json2.js
|
|
||||||
2009-04-16
|
|
||||||
|
|
||||||
Public Domain.
|
|
||||||
|
|
||||||
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
|
||||||
|
|
||||||
See http://www.JSON.org/js.html
|
|
||||||
|
|
||||||
This file creates a global JSON object containing two methods: stringify
|
|
||||||
and parse.
|
|
||||||
|
|
||||||
JSON.stringify(value, replacer, space)
|
|
||||||
value any JavaScript value, usually an object or array.
|
|
||||||
|
|
||||||
replacer an optional parameter that determines how object
|
|
||||||
values are stringified for objects. It can be a
|
|
||||||
function or an array of strings.
|
|
||||||
|
|
||||||
space an optional parameter that specifies the indentation
|
|
||||||
of nested structures. If it is omitted, the text will
|
|
||||||
be packed without extra whitespace. If it is a number,
|
|
||||||
it will specify the number of spaces to indent at each
|
|
||||||
level. If it is a string (such as '\t' or ' '),
|
|
||||||
it contains the characters used to indent at each level.
|
|
||||||
|
|
||||||
This method produces a JSON text from a JavaScript value.
|
|
||||||
|
|
||||||
When an object value is found, if the object contains a toJSON
|
|
||||||
method, its toJSON method will be called and the result will be
|
|
||||||
stringified. A toJSON method does not serialize: it returns the
|
|
||||||
value represented by the name/value pair that should be serialized,
|
|
||||||
or undefined if nothing should be serialized. The toJSON method
|
|
||||||
will be passed the key associated with the value, and this will be
|
|
||||||
bound to the object holding the key.
|
|
||||||
|
|
||||||
For example, this would serialize Dates as ISO strings.
|
|
||||||
|
|
||||||
Date.prototype.toJSON = function (key) {
|
|
||||||
function f(n) {
|
|
||||||
// Format integers to have at least two digits.
|
|
||||||
return n < 10 ? '0' + n : n;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.getUTCFullYear() + '-' +
|
|
||||||
f(this.getUTCMonth() + 1) + '-' +
|
|
||||||
f(this.getUTCDate()) + 'T' +
|
|
||||||
f(this.getUTCHours()) + ':' +
|
|
||||||
f(this.getUTCMinutes()) + ':' +
|
|
||||||
f(this.getUTCSeconds()) + 'Z';
|
|
||||||
};
|
|
||||||
|
|
||||||
You can provide an optional replacer method. It will be passed the
|
|
||||||
key and value of each member, with this bound to the containing
|
|
||||||
object. The value that is returned from your method will be
|
|
||||||
serialized. If your method returns undefined, then the member will
|
|
||||||
be excluded from the serialization.
|
|
||||||
|
|
||||||
If the replacer parameter is an array of strings, then it will be
|
|
||||||
used to select the members to be serialized. It filters the results
|
|
||||||
such that only members with keys listed in the replacer array are
|
|
||||||
stringified.
|
|
||||||
|
|
||||||
Values that do not have JSON representations, such as undefined or
|
|
||||||
functions, will not be serialized. Such values in objects will be
|
|
||||||
dropped; in arrays they will be replaced with null. You can use
|
|
||||||
a replacer function to replace those with JSON values.
|
|
||||||
JSON.stringify(undefined) returns undefined.
|
|
||||||
|
|
||||||
The optional space parameter produces a stringification of the
|
|
||||||
value that is filled with line breaks and indentation to make it
|
|
||||||
easier to read.
|
|
||||||
|
|
||||||
If the space parameter is a non-empty string, then that string will
|
|
||||||
be used for indentation. If the space parameter is a number, then
|
|
||||||
the indentation will be that many spaces.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
text = JSON.stringify(['e', {pluribus: 'unum'}]);
|
|
||||||
// text is '["e",{"pluribus":"unum"}]'
|
|
||||||
|
|
||||||
|
|
||||||
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
|
|
||||||
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
|
||||||
|
|
||||||
text = JSON.stringify([new Date()], function (key, value) {
|
|
||||||
return this[key] instanceof Date ?
|
|
||||||
'Date(' + this[key] + ')' : value;
|
|
||||||
});
|
|
||||||
// text is '["Date(---current time---)"]'
|
|
||||||
|
|
||||||
|
|
||||||
JSON.parse(text, reviver)
|
|
||||||
This method parses a JSON text to produce an object or array.
|
|
||||||
It can throw a SyntaxError exception.
|
|
||||||
|
|
||||||
The optional reviver parameter is a function that can filter and
|
|
||||||
transform the results. It receives each of the keys and values,
|
|
||||||
and its return value is used instead of the original value.
|
|
||||||
If it returns what it received, then the structure is not modified.
|
|
||||||
If it returns undefined then the member is deleted.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
// Parse the text. Values that look like ISO date strings will
|
|
||||||
// be converted to Date objects.
|
|
||||||
|
|
||||||
myData = JSON.parse(text, function (key, value) {
|
|
||||||
var a;
|
|
||||||
if (typeof value === 'string') {
|
|
||||||
a =
|
|
||||||
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
|
|
||||||
if (a) {
|
|
||||||
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
|
|
||||||
+a[5], +a[6]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
|
|
||||||
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
|
|
||||||
var d;
|
|
||||||
if (typeof value === 'string' &&
|
|
||||||
value.slice(0, 5) === 'Date(' &&
|
|
||||||
value.slice(-1) === ')') {
|
|
||||||
d = new Date(value.slice(5, -1));
|
|
||||||
if (d) {
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
This is a reference implementation. You are free to copy, modify, or
|
|
||||||
redistribute.
|
|
||||||
|
|
||||||
This code should be minified before deployment.
|
|
||||||
See http://javascript.crockford.com/jsmin.html
|
|
||||||
|
|
||||||
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
|
|
||||||
NOT CONTROL.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*jslint evil: true */
|
|
||||||
|
|
||||||
/*global JSON */
|
|
||||||
|
|
||||||
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
|
|
||||||
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
|
|
||||||
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
|
|
||||||
lastIndex, length, parse, prototype, push, replace, slice, stringify,
|
|
||||||
test, toJSON, toString, valueOf
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Create a JSON object only if one does not already exist. We create the
|
|
||||||
// methods in a closure to avoid creating global variables.
|
|
||||||
|
|
||||||
if (!this.JSON) {
|
|
||||||
JSON = {};
|
|
||||||
}
|
|
||||||
(function () {
|
|
||||||
|
|
||||||
function f(n) {
|
|
||||||
// Format integers to have at least two digits.
|
|
||||||
return n < 10 ? '0' + n : n;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof Date.prototype.toJSON !== 'function') {
|
|
||||||
|
|
||||||
Date.prototype.toJSON = function (key) {
|
|
||||||
|
|
||||||
return this.getUTCFullYear() + '-' +
|
|
||||||
f(this.getUTCMonth() + 1) + '-' +
|
|
||||||
f(this.getUTCDate()) + 'T' +
|
|
||||||
f(this.getUTCHours()) + ':' +
|
|
||||||
f(this.getUTCMinutes()) + ':' +
|
|
||||||
f(this.getUTCSeconds()) + 'Z';
|
|
||||||
};
|
|
||||||
|
|
||||||
String.prototype.toJSON =
|
|
||||||
Number.prototype.toJSON =
|
|
||||||
Boolean.prototype.toJSON = function (key) {
|
|
||||||
return this.valueOf();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
|
||||||
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
|
||||||
gap,
|
|
||||||
indent,
|
|
||||||
meta = { // table of character substitutions
|
|
||||||
'\b': '\\b',
|
|
||||||
'\t': '\\t',
|
|
||||||
'\n': '\\n',
|
|
||||||
'\f': '\\f',
|
|
||||||
'\r': '\\r',
|
|
||||||
'"' : '\\"',
|
|
||||||
'\\': '\\\\'
|
|
||||||
},
|
|
||||||
rep;
|
|
||||||
|
|
||||||
|
|
||||||
function quote(string) {
|
|
||||||
|
|
||||||
// If the string contains no control characters, no quote characters, and no
|
|
||||||
// backslash characters, then we can safely slap some quotes around it.
|
|
||||||
// Otherwise we must also replace the offending characters with safe escape
|
|
||||||
// sequences.
|
|
||||||
|
|
||||||
escapable.lastIndex = 0;
|
|
||||||
return escapable.test(string) ?
|
|
||||||
'"' + string.replace(escapable, function (a) {
|
|
||||||
var c = meta[a];
|
|
||||||
return typeof c === 'string' ? c :
|
|
||||||
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
|
||||||
}) + '"' :
|
|
||||||
'"' + string + '"';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function str(key, holder) {
|
|
||||||
|
|
||||||
// Produce a string from holder[key].
|
|
||||||
|
|
||||||
var i, // The loop counter.
|
|
||||||
k, // The member key.
|
|
||||||
v, // The member value.
|
|
||||||
length,
|
|
||||||
mind = gap,
|
|
||||||
partial,
|
|
||||||
value = holder[key];
|
|
||||||
|
|
||||||
// If the value has a toJSON method, call it to obtain a replacement value.
|
|
||||||
|
|
||||||
if (value && typeof value === 'object' &&
|
|
||||||
typeof value.toJSON === 'function') {
|
|
||||||
value = value.toJSON(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we were called with a replacer function, then call the replacer to
|
|
||||||
// obtain a replacement value.
|
|
||||||
|
|
||||||
if (typeof rep === 'function') {
|
|
||||||
value = rep.call(holder, key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// What happens next depends on the value's type.
|
|
||||||
|
|
||||||
switch (typeof value) {
|
|
||||||
case 'string':
|
|
||||||
return quote(value);
|
|
||||||
|
|
||||||
case 'number':
|
|
||||||
|
|
||||||
// JSON numbers must be finite. Encode non-finite numbers as null.
|
|
||||||
|
|
||||||
return isFinite(value) ? String(value) : 'null';
|
|
||||||
|
|
||||||
case 'boolean':
|
|
||||||
case 'null':
|
|
||||||
|
|
||||||
// If the value is a boolean or null, convert it to a string. Note:
|
|
||||||
// typeof null does not produce 'null'. The case is included here in
|
|
||||||
// the remote chance that this gets fixed someday.
|
|
||||||
|
|
||||||
return String(value);
|
|
||||||
|
|
||||||
// If the type is 'object', we might be dealing with an object or an array or
|
|
||||||
// null.
|
|
||||||
|
|
||||||
case 'object':
|
|
||||||
|
|
||||||
// Due to a specification blunder in ECMAScript, typeof null is 'object',
|
|
||||||
// so watch out for that case.
|
|
||||||
|
|
||||||
if (!value) {
|
|
||||||
return 'null';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make an array to hold the partial results of stringifying this object value.
|
|
||||||
|
|
||||||
gap += indent;
|
|
||||||
partial = [];
|
|
||||||
|
|
||||||
// Is the value an array?
|
|
||||||
|
|
||||||
if (Object.prototype.toString.apply(value) === '[object Array]') {
|
|
||||||
|
|
||||||
// The value is an array. Stringify every element. Use null as a placeholder
|
|
||||||
// for non-JSON values.
|
|
||||||
|
|
||||||
length = value.length;
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
partial[i] = str(i, value) || 'null';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Join all of the elements together, separated with commas, and wrap them in
|
|
||||||
// brackets.
|
|
||||||
|
|
||||||
v = partial.length === 0 ? '[]' :
|
|
||||||
gap ? '[\n' + gap +
|
|
||||||
partial.join(',\n' + gap) + '\n' +
|
|
||||||
mind + ']' :
|
|
||||||
'[' + partial.join(',') + ']';
|
|
||||||
gap = mind;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the replacer is an array, use it to select the members to be stringified.
|
|
||||||
|
|
||||||
if (rep && typeof rep === 'object') {
|
|
||||||
length = rep.length;
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
k = rep[i];
|
|
||||||
if (typeof k === 'string') {
|
|
||||||
v = str(k, value);
|
|
||||||
if (v) {
|
|
||||||
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Otherwise, iterate through all of the keys in the object.
|
|
||||||
|
|
||||||
for (k in value) {
|
|
||||||
if (Object.hasOwnProperty.call(value, k)) {
|
|
||||||
v = str(k, value);
|
|
||||||
if (v) {
|
|
||||||
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Join all of the member texts together, separated with commas,
|
|
||||||
// and wrap them in braces.
|
|
||||||
|
|
||||||
v = partial.length === 0 ? '{}' :
|
|
||||||
gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
|
|
||||||
mind + '}' : '{' + partial.join(',') + '}';
|
|
||||||
gap = mind;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the JSON object does not yet have a stringify method, give it one.
|
|
||||||
|
|
||||||
if (typeof JSON.stringify !== 'function') {
|
|
||||||
JSON.stringify = function (value, replacer, space) {
|
|
||||||
|
|
||||||
// The stringify method takes a value and an optional replacer, and an optional
|
|
||||||
// space parameter, and returns a JSON text. The replacer can be a function
|
|
||||||
// that can replace values, or an array of strings that will select the keys.
|
|
||||||
// A default replacer method can be provided. Use of the space parameter can
|
|
||||||
// produce text that is more easily readable.
|
|
||||||
|
|
||||||
var i;
|
|
||||||
gap = '';
|
|
||||||
indent = '';
|
|
||||||
|
|
||||||
// If the space parameter is a number, make an indent string containing that
|
|
||||||
// many spaces.
|
|
||||||
|
|
||||||
if (typeof space === 'number') {
|
|
||||||
for (i = 0; i < space; i += 1) {
|
|
||||||
indent += ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the space parameter is a string, it will be used as the indent string.
|
|
||||||
|
|
||||||
} else if (typeof space === 'string') {
|
|
||||||
indent = space;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is a replacer, it must be a function or an array.
|
|
||||||
// Otherwise, throw an error.
|
|
||||||
|
|
||||||
rep = replacer;
|
|
||||||
if (replacer && typeof replacer !== 'function' &&
|
|
||||||
(typeof replacer !== 'object' ||
|
|
||||||
typeof replacer.length !== 'number')) {
|
|
||||||
throw new Error('JSON.stringify');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make a fake root object containing our value under the key of ''.
|
|
||||||
// Return the result of stringifying the value.
|
|
||||||
|
|
||||||
return str('', {'': value});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// If the JSON object does not yet have a parse method, give it one.
|
|
||||||
|
|
||||||
if (typeof JSON.parse !== 'function') {
|
|
||||||
JSON.parse = function (text, reviver) {
|
|
||||||
|
|
||||||
// The parse method takes a text and an optional reviver function, and returns
|
|
||||||
// a JavaScript value if the text is a valid JSON text.
|
|
||||||
|
|
||||||
var j;
|
|
||||||
|
|
||||||
function walk(holder, key) {
|
|
||||||
|
|
||||||
// The walk method is used to recursively walk the resulting structure so
|
|
||||||
// that modifications can be made.
|
|
||||||
|
|
||||||
var k, v, value = holder[key];
|
|
||||||
if (value && typeof value === 'object') {
|
|
||||||
for (k in value) {
|
|
||||||
if (Object.hasOwnProperty.call(value, k)) {
|
|
||||||
v = walk(value, k);
|
|
||||||
if (v !== undefined) {
|
|
||||||
value[k] = v;
|
|
||||||
} else {
|
|
||||||
delete value[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return reviver.call(holder, key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Parsing happens in four stages. In the first stage, we replace certain
|
|
||||||
// Unicode characters with escape sequences. JavaScript handles many characters
|
|
||||||
// incorrectly, either silently deleting them, or treating them as line endings.
|
|
||||||
|
|
||||||
cx.lastIndex = 0;
|
|
||||||
if (cx.test(text)) {
|
|
||||||
text = text.replace(cx, function (a) {
|
|
||||||
return '\\u' +
|
|
||||||
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// In the second stage, we run the text against regular expressions that look
|
|
||||||
// for non-JSON patterns. We are especially concerned with '()' and 'new'
|
|
||||||
// because they can cause invocation, and '=' because it can cause mutation.
|
|
||||||
// But just to be safe, we want to reject all unexpected forms.
|
|
||||||
|
|
||||||
// We split the second stage into 4 regexp operations in order to work around
|
|
||||||
// crippling inefficiencies in IE's and Safari's regexp engines. First we
|
|
||||||
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
|
|
||||||
// replace all simple value tokens with ']' characters. Third, we delete all
|
|
||||||
// open brackets that follow a colon or comma or that begin the text. Finally,
|
|
||||||
// we look to see that the remaining characters are only whitespace or ']' or
|
|
||||||
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
|
|
||||||
|
|
||||||
if (/^[\],:{}\s]*$/.
|
|
||||||
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
|
|
||||||
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
|
|
||||||
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
|
|
||||||
|
|
||||||
// In the third stage we use the eval function to compile the text into a
|
|
||||||
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
|
|
||||||
// in JavaScript: it can begin a block or an object literal. We wrap the text
|
|
||||||
// in parens to eliminate the ambiguity.
|
|
||||||
|
|
||||||
j = eval('(' + text + ')');
|
|
||||||
|
|
||||||
// In the optional fourth stage, we recursively walk the new structure, passing
|
|
||||||
// each name/value pair to a reviver function for possible transformation.
|
|
||||||
|
|
||||||
return typeof reviver === 'function' ?
|
|
||||||
walk({'': j}, '') : j;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the text is not JSON parseable, then a SyntaxError is thrown.
|
|
||||||
|
|
||||||
throw new SyntaxError('JSON.parse');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}());
|
|
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 625 B After Width: | Height: | Size: 625 B |
Before Width: | Height: | Size: 557 B After Width: | Height: | Size: 557 B |
Before Width: | Height: | Size: 784 B After Width: | Height: | Size: 784 B |
After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 260 B After Width: | Height: | Size: 260 B |
Before Width: | Height: | Size: 656 B After Width: | Height: | Size: 656 B |
Before Width: | Height: | Size: 634 B After Width: | Height: | Size: 634 B |
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 406 B |
Before Width: | Height: | Size: 827 B After Width: | Height: | Size: 827 B |
Before Width: | Height: | Size: 476 B After Width: | Height: | Size: 476 B |
Before Width: | Height: | Size: 546 B After Width: | Height: | Size: 546 B |
Before Width: | Height: | Size: 423 B After Width: | Height: | Size: 423 B |
Before Width: | Height: | Size: 453 B After Width: | Height: | Size: 453 B |
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 273 B |
Before Width: | Height: | Size: 577 B After Width: | Height: | Size: 577 B |
Before Width: | Height: | Size: 785 B After Width: | Height: | Size: 785 B |
Before Width: | Height: | Size: 791 B After Width: | Height: | Size: 791 B |
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 273 B |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 569 B After Width: | Height: | Size: 569 B |
Before Width: | Height: | Size: 558 B After Width: | Height: | Size: 558 B |
Before Width: | Height: | Size: 322 B After Width: | Height: | Size: 322 B |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 575 B After Width: | Height: | Size: 575 B |
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 4.8 KiB After Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 136 B After Width: | Height: | Size: 136 B |
Before Width: | Height: | Size: 116 B After Width: | Height: | Size: 116 B |
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 136 B After Width: | Height: | Size: 136 B |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |