Plato on Github
Report Home
node_modules\atropa-CreateHtmlDocumentsFromXmlhttp\src\atropa-CreateHtmlDocumentsFromXmlhttp.js
Maintainability
71.03
Lines of code
186
Difficulty
19.17
Estimated Errors
0.42
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.Requester = require('atropa-Requester').Requester; atropa.HTMLParser = require('atropa-HTMLParser').HTMLParser; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header atropa.requires( 'CreateHtmlDocumentsFromXmlhttp', function () { "use strict"; var supported = true; [ atropa.Requester, atropa.HTMLParser ].forEach(function (prerequisite) { if(prerequisite === undefined) { supported = false; } }); return supported; } ); /** * Creates HTML DOM Documents from an XMLHttpRequest object. * This was tested on Firefox, it doesn't work on google chrome. * Your mileage may vary. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130225 * @class Creates HTML DOM Documents from an XMLHttpRequest object. * @requires atropa.Requester * @requires atropa.HTMLParser * @requires atropa.data * @example * var method, url, callback, docs; * * // HTTP Request method * method = 'get'; * * // the page to fetch, this page must be accessible * // security restrictions may apply * url = 'docs/jsdoc/symbols/atropa.xpath.html'; * * // the callback funtion for when a new document is created * callback = function newDocumentHandler(docref) { * try { * if (false === docref) { * // if the document could not be created throw an error * throw new Error('atropa.CreateHtmlDocumentsFromXmlhttp ' + * 'Could not create hidden document'); * } else { * // if the document could be created we'll try to use it * if(docref.getElementById('index')) { * // if the document could be used then * // do something useful with it. * console.log('success!'); * } else { * // if the document can not be used throw an error * throw new Error('atropa.CreateHtmlDocumentsFromXmlhttp ' + * 'could not use the hidden document'); * } * } * } catch (e) { * // catching any errors thrown and handle them. * } * // At this point the work with the document is currently finished * // the document will live in the documentQueue in case you need it * // later. This is when you will trigger any function which depends * // on this hidden document having been created. * showDocumentQueue(); * }; * * function showDocumentQueue() { * console.dir(docs.documentQueue); * } * * // create an instance of the class * docs = new atropa.CreateHtmlDocumentsFromXmlhttp(); * // try to create a new hidden document * docs.newDocument(method, url, null, callback); */ atropa.CreateHtmlDocumentsFromXmlhttp = function CreateHtmlDocumentsFromXmlhttp( ) { "use strict"; var requester, htmldocument, that; that = this; /** * Queue of documents created by this instance. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @type Array * @fieldOf atropa.CreateHtmlDocumentsFromXmlhttp# */ this.documentQueue = []; /** * Creates an HTML DOM Document and puts it in the document * queue, then executes the callback given. Note, this does * not work on google chrome. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CreateHtmlDocumentsFromXmlhttp# * @param {String} method Any valid method to be used in * an XMLHttpRequest. * @param {String} url The location of the document's source. * @param {String} messageBody null, or a message body. * @param {Function} callback The function to execute upon * request completion. This function will be given either * an HTML DOM Document or false. * @returns {HTML DOM Document, false} The return value is * given to the callback function. */ this.newDocument = function newDocument( method, url, messageBody, callback ) { var cb; /* * Internal callback function to process data from XMLHttpRequest * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CreateHtmlDocumentsFromXmlhttp#newDocument- * @private * @property {true,false} boolStatus This tells whether or not the * XMLHttpRequest was successful. * @property {XMLHttp Response Object} responseObject This is the * response object from the XMLHttp Request object. */ cb = function (boolStatus, responseObject) { var result = false; if (boolStatus === true) { if (false !== htmldocument.loadString( responseObject.responseText)) { result = htmldocument.doc; that.documentQueue.push(result); } } else { result = boolStatus; } callback(result); }; requester.makeRequest(method, url, messageBody, cb); }; function init () { try { atropa.supportCheck('CreateHtmlDocumentsFromXmlhttp'); requester = new atropa.Requester(); htmldocument = new atropa.HTMLParser(); } catch (e) { atropa.data.CreateHtmlDocumentsFromXmlhttp.support = 'unsupported'; throw new Error(atropa.data.CreateHtmlDocumentsFromXmlhttp.error); } } init(); }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;