mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-21 22:12:32 -05:00
Update & Add: Refactored Loading of UI & Stylesheets/Scripts
This commit is contained in:
parent
97a75e92ac
commit
e0fe00bea6
11 changed files with 1300 additions and 180 deletions
80
Demo.html
80
Demo.html
|
@ -4,89 +4,13 @@
|
|||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||
<meta charset="UTF-8">
|
||||
<title>HTML Editor Test</title>
|
||||
<style type="text/css" media="screen">
|
||||
body {
|
||||
position:relative;
|
||||
margin:0 auto;
|
||||
padding:50px;
|
||||
width:540px;
|
||||
font:400 14px/1.24 helvetica,arial,sans-serif;
|
||||
text-shadow:0 1px 0 #fff;
|
||||
}
|
||||
|
||||
iframe {
|
||||
border: none;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size:1.95em
|
||||
}
|
||||
|
||||
span {
|
||||
cursor:pointer;
|
||||
text-decoration:underline
|
||||
}
|
||||
|
||||
p {
|
||||
margin:5px 0
|
||||
}
|
||||
</style>
|
||||
<!--[if IE 8]>
|
||||
<script type="text/javascript" src="build/ie8.js"></script>
|
||||
<![endif]-->
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var editor;
|
||||
document.addEventListener( 'click', function ( e ) {
|
||||
var id = e.target.id,
|
||||
value;
|
||||
if ( id && editor && editor[ id ] ) {
|
||||
if ( e.target.className === 'prompt' ) {
|
||||
value = prompt( 'Value:' );
|
||||
}
|
||||
editor[ id ]( value );
|
||||
}
|
||||
}, false );
|
||||
</script>
|
||||
<script src="build/Squire-UI.js" type="text/javascript"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>HTML Editor Test</h1>
|
||||
<header>
|
||||
<p>This is a really simple demo, with the most trivial of UI integrations</p>
|
||||
<p>
|
||||
<span id="bold2">Bold</span>
|
||||
<span id="removeBold">Unbold</span>
|
||||
<span id="italic">Italic</span>
|
||||
|
||||
<span id="removeItalic">Unitalic</span>
|
||||
<span id="underline">Underline</span>
|
||||
<span id="removeUnderline">Deunderline</span>
|
||||
|
||||
<span id="setFontSize" class="prompt">Font size</span>
|
||||
<span id="setFontFace" class="prompt">Font face</span>
|
||||
</p>
|
||||
<p>
|
||||
<span id="setTextColour" class="prompt">Text colour</span>
|
||||
<span id="setHighlightColour" class="prompt">Text highlight</span>
|
||||
<span id="makeLink" class="prompt">Link</span>
|
||||
</p>
|
||||
<p>
|
||||
<span id="increaseQuoteLevel">Quote</span>
|
||||
<span id="decreaseQuoteLevel">Dequote</span>
|
||||
|
||||
<span id="makeUnorderedList">List</span>
|
||||
<span id="removeList">Unlist</span>
|
||||
|
||||
<span id="insertImage" class="prompt">Insert image</span>
|
||||
<span id="setHTML" class="prompt">Set HTML</span>
|
||||
|
||||
<span id="undo">Undo</span>
|
||||
<span id="redo">Redo</span>
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<iframe src="build/Squire-UI.html" width="500" height="300"></iframe>
|
||||
<iframe src="build/document.html" onload="top.editor=this.contentWindow.editor" width="500" height="500"></iframe>
|
||||
|
||||
|
||||
<textarea id="foo"></textarea>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -57,3 +57,5 @@
|
|||
top:0;
|
||||
right:0;
|
||||
}
|
||||
|
||||
|
|
@ -1,47 +1,31 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<link href="Squire-UI.css" rel="stylesheet" type="text/css" type="text/css" rel="stylesheet" />
|
||||
<link href="assets/drop/drop-theme-arrows.css" type="text/css" rel="stylesheet" />
|
||||
|
||||
<link href="assets/font-awesome/font-awesome.min.css" type="text/css" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="menu" contenteditable="false">
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-bold"></i></div>
|
||||
<div class="item"><i class="fa fa-italic"></i></div>
|
||||
<div class="item"><i class="fa fa-underline"></i></div>
|
||||
<div id="font" class="item">
|
||||
<i class="fa fa-font"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-link"></i></div>
|
||||
<div class="item"><i class="fa fa-quote-right"></i></div>
|
||||
<div class="item"><i class="fa fa-list"></i></div>
|
||||
<div class="item">
|
||||
<i class="fa fa-picture-o"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-undo"></i></div>
|
||||
<div class="item"><i class="fa fa-undo flip"></i></div>
|
||||
</div>
|
||||
</header>
|
||||
<div class="templates hidden">
|
||||
<div class="drop-font">
|
||||
<i class="fa fa-chevron-up quit" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"></i>
|
||||
Text Size: <input type="number" min="12" max="72" step="6" value="12" name="font-size">
|
||||
<br>
|
||||
Font: <input type="text">
|
||||
|
||||
<div class="menu" contenteditable="false">
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-bold"></i></div>
|
||||
<div class="item"><i class="fa fa-italic"></i></div>
|
||||
<div class="item"><i class="fa fa-underline"></i></div>
|
||||
<div id="font" class="item">
|
||||
<i class="fa fa-font"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-link"></i></div>
|
||||
<div class="item"><i class="fa fa-quote-right"></i></div>
|
||||
<div class="item"><i class="fa fa-list"></i></div>
|
||||
<div class="item">
|
||||
<i class="fa fa-picture-o"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-undo"></i></div>
|
||||
<div class="item"><i class="fa fa-undo flip"></i></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="templates hidden">
|
||||
<div class="drop-font">
|
||||
<i class="fa fa-chevron-up quit" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"></i>
|
||||
Text Size: <input type="number" min="12" max="72" step="6" value="12" name="font-size">
|
||||
<br>
|
||||
Font: <input type="text">
|
||||
|
||||
<script src="assets/drop/drop.min.js" type="text/javascript"></script>
|
||||
<script src="Squire-UI.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,13 +1,231 @@
|
|||
|
||||
LazyLoad = (function (doc) {
|
||||
var env,
|
||||
|
||||
head,
|
||||
pending = {},
|
||||
pollCount = 0,
|
||||
queue = {css: [], js: []},
|
||||
styleSheets = doc.styleSheets;
|
||||
function createNode(name, attrs) {
|
||||
var node = doc.createElement(name), attr;
|
||||
|
||||
for (attr in attrs) {
|
||||
if (attrs.hasOwnProperty(attr)) {
|
||||
node.setAttribute(attr, attrs[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
function finish(type) {
|
||||
var p = pending[type],
|
||||
callback,
|
||||
urls;
|
||||
|
||||
if (p) {
|
||||
callback = p.callback;
|
||||
urls = p.urls;
|
||||
|
||||
urls.shift();
|
||||
pollCount = 0;
|
||||
|
||||
// If this is the last of the pending URLs, execute the callback and
|
||||
// start the next request in the queue (if any).
|
||||
if (!urls.length) {
|
||||
callback && callback.call(p.context, p.obj);
|
||||
pending[type] = null;
|
||||
queue[type].length && load(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getEnv() {
|
||||
var ua = navigator.userAgent;
|
||||
|
||||
env = {
|
||||
async: doc.createElement('script').async === true
|
||||
};
|
||||
|
||||
(env.webkit = /AppleWebKit\//.test(ua))
|
||||
|| (env.ie = /MSIE|Trident/.test(ua))
|
||||
|| (env.opera = /Opera/.test(ua))
|
||||
|| (env.gecko = /Gecko\//.test(ua))
|
||||
|| (env.unknown = true);
|
||||
}
|
||||
|
||||
function load(type, urls, callback, obj, context) {
|
||||
var _finish = function () { finish(type); },
|
||||
isCSS = type === 'css',
|
||||
nodes = [],
|
||||
i, len, node, p, pendingUrls, url;
|
||||
|
||||
env || getEnv();
|
||||
|
||||
if (urls) {
|
||||
urls = typeof urls === 'string' ? [urls] : urls.concat();
|
||||
|
||||
if (isCSS || env.async || env.gecko || env.opera) {
|
||||
// Load in parallel.
|
||||
queue[type].push({
|
||||
urls : urls,
|
||||
callback: callback,
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
} else {
|
||||
// Load sequentially.
|
||||
for (i = 0, len = urls.length; i < len; ++i) {
|
||||
queue[type].push({
|
||||
urls : [urls[i]],
|
||||
callback: i === len - 1 ? callback : null, // callback is only added to the last URL
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a previous load request of this type is currently in progress, we'll
|
||||
// wait our turn. Otherwise, grab the next item in the queue.
|
||||
if (pending[type] || !(p = pending[type] = queue[type].shift())) {
|
||||
return;
|
||||
}
|
||||
|
||||
head || (head = doc.head || doc.getElementsByTagName('head')[0]);
|
||||
pendingUrls = p.urls.concat();
|
||||
|
||||
for (i = 0, len = pendingUrls.length; i < len; ++i) {
|
||||
url = pendingUrls[i];
|
||||
|
||||
if (isCSS) {
|
||||
node = env.gecko ? createNode('style') : createNode('link', {
|
||||
href: url,
|
||||
rel : 'stylesheet'
|
||||
});
|
||||
} else {
|
||||
node = createNode('script', {src: url});
|
||||
node.async = false;
|
||||
}
|
||||
|
||||
node.className = 'lazyload';
|
||||
node.setAttribute('charset', 'utf-8');
|
||||
|
||||
if (env.ie && !isCSS && 'onreadystatechange' in node && !('draggable' in node)) {
|
||||
node.onreadystatechange = function () {
|
||||
if (/loaded|complete/.test(node.readyState)) {
|
||||
node.onreadystatechange = null;
|
||||
_finish();
|
||||
}
|
||||
};
|
||||
} else if (isCSS && (env.gecko || env.webkit)) {
|
||||
if (env.webkit) {
|
||||
p.urls[i] = node.href; // resolve relative URLs (or polling won't work)
|
||||
pollWebKit();
|
||||
} else {
|
||||
node.innerHTML = '@import "' + url + '";';
|
||||
pollGecko(node);
|
||||
}
|
||||
} else {
|
||||
node.onload = node.onerror = _finish;
|
||||
}
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
for (i = 0, len = nodes.length; i < len; ++i) {
|
||||
head.appendChild(nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function pollGecko(node) {
|
||||
var hasRules;
|
||||
|
||||
try {
|
||||
// We don't really need to store this value or ever refer to it again, but
|
||||
// if we don't store it, Closure Compiler assumes the code is useless and
|
||||
// removes it.
|
||||
hasRules = !!node.sheet.cssRules;
|
||||
} catch (ex) {
|
||||
// An exception means the stylesheet is still loading.
|
||||
pollCount += 1;
|
||||
|
||||
if (pollCount < 200) {
|
||||
setTimeout(function () { pollGecko(node); }, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened. Stop
|
||||
// polling and finish the pending requests to avoid blocking further
|
||||
// requests.
|
||||
hasRules && finish('css');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we get here, the stylesheet has loaded.
|
||||
finish('css');
|
||||
}
|
||||
|
||||
function pollWebKit() {
|
||||
var css = pending.css, i;
|
||||
|
||||
if (css) {
|
||||
i = styleSheets.length;
|
||||
|
||||
// Look for a stylesheet matching the pending URL.
|
||||
while (--i >= 0) {
|
||||
if (styleSheets[i].href === css.urls[0]) {
|
||||
finish('css');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pollCount += 1;
|
||||
|
||||
if (css) {
|
||||
if (pollCount < 200) {
|
||||
setTimeout(pollWebKit, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened, which may
|
||||
// indicate that the stylesheet has been removed from the document
|
||||
// before it had a chance to load. Stop polling and finish the pending
|
||||
// request to prevent blocking further requests.
|
||||
finish('css');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
css: function (urls, callback, obj, context) {
|
||||
load('css', urls, callback, obj, context);
|
||||
},
|
||||
js: function (urls, callback, obj, context) {
|
||||
load('js', urls, callback, obj, context);
|
||||
}
|
||||
|
||||
};
|
||||
})(this.document);
|
||||
|
||||
|
||||
(function() {
|
||||
var drop, quit;
|
||||
LazyLoad.js(['build/squire.js','build/assets/jQuery/jQuery.js','build/assets/drop/drop.min.js']);
|
||||
LazyLoad.css(['build/Squire-UI.css','build/assets/font-awesome/font-awesome.min.css', 'build/assets/drop/drop-theme-hubspot-popovers.css']);
|
||||
|
||||
SquireUI = function (options) {
|
||||
var originElement = $(options.replace);
|
||||
var container = originElement.parent();
|
||||
|
||||
originElement.remove();
|
||||
var editorContainer = $('<iframe />').appendTo(container);
|
||||
var editorDocument = editorContainer[0].contentWindow.document;
|
||||
|
||||
drop = new Drop({
|
||||
target: document.querySelector('#font'),
|
||||
content: document.querySelector('.drop-font').innerHTML,
|
||||
position: 'bottom left',
|
||||
openOn: 'click'
|
||||
});
|
||||
var editor = new Squire(editorDocument);
|
||||
|
||||
return editor;
|
||||
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
|
|
4
build/assets/jQuery/jQuery.js
Normal file
4
build/assets/jQuery/jQuery.js
Normal file
File diff suppressed because one or more lines are too long
390
build/assets/lazyload/lazyload.js
Normal file
390
build/assets/lazyload/lazyload.js
Normal file
|
@ -0,0 +1,390 @@
|
|||
/*jslint browser: true, eqeqeq: true, bitwise: true, newcap: true, immed: true, regexp: false */
|
||||
|
||||
/**
|
||||
LazyLoad makes it easy and painless to lazily load one or more external
|
||||
JavaScript or CSS files on demand either during or after the rendering of a web
|
||||
page.
|
||||
|
||||
Supported browsers include Firefox 2+, IE6+, Safari 3+ (including Mobile
|
||||
Safari), Google Chrome, and Opera 9+. Other browsers may or may not work and
|
||||
are not officially supported.
|
||||
|
||||
Visit https://github.com/rgrove/lazyload/ for more info.
|
||||
|
||||
Copyright (c) 2011 Ryan Grove <ryan@wonko.com>
|
||||
All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the 'Software'), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
@module lazyload
|
||||
@class LazyLoad
|
||||
@static
|
||||
*/
|
||||
|
||||
LazyLoad = (function (doc) {
|
||||
// -- Private Variables ------------------------------------------------------
|
||||
|
||||
// User agent and feature test information.
|
||||
var env,
|
||||
|
||||
// Reference to the <head> element (populated lazily).
|
||||
head,
|
||||
|
||||
// Requests currently in progress, if any.
|
||||
pending = {},
|
||||
|
||||
// Number of times we've polled to check whether a pending stylesheet has
|
||||
// finished loading. If this gets too high, we're probably stalled.
|
||||
pollCount = 0,
|
||||
|
||||
// Queued requests.
|
||||
queue = {css: [], js: []},
|
||||
|
||||
// Reference to the browser's list of stylesheets.
|
||||
styleSheets = doc.styleSheets;
|
||||
|
||||
// -- Private Methods --------------------------------------------------------
|
||||
|
||||
/**
|
||||
Creates and returns an HTML element with the specified name and attributes.
|
||||
|
||||
@method createNode
|
||||
@param {String} name element name
|
||||
@param {Object} attrs name/value mapping of element attributes
|
||||
@return {HTMLElement}
|
||||
@private
|
||||
*/
|
||||
function createNode(name, attrs) {
|
||||
var node = doc.createElement(name), attr;
|
||||
|
||||
for (attr in attrs) {
|
||||
if (attrs.hasOwnProperty(attr)) {
|
||||
node.setAttribute(attr, attrs[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
Called when the current pending resource of the specified type has finished
|
||||
loading. Executes the associated callback (if any) and loads the next
|
||||
resource in the queue.
|
||||
|
||||
@method finish
|
||||
@param {String} type resource type ('css' or 'js')
|
||||
@private
|
||||
*/
|
||||
function finish(type) {
|
||||
var p = pending[type],
|
||||
callback,
|
||||
urls;
|
||||
|
||||
if (p) {
|
||||
callback = p.callback;
|
||||
urls = p.urls;
|
||||
|
||||
urls.shift();
|
||||
pollCount = 0;
|
||||
|
||||
// If this is the last of the pending URLs, execute the callback and
|
||||
// start the next request in the queue (if any).
|
||||
if (!urls.length) {
|
||||
callback && callback.call(p.context, p.obj);
|
||||
pending[type] = null;
|
||||
queue[type].length && load(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Populates the <code>env</code> variable with user agent and feature test
|
||||
information.
|
||||
|
||||
@method getEnv
|
||||
@private
|
||||
*/
|
||||
function getEnv() {
|
||||
var ua = navigator.userAgent;
|
||||
|
||||
env = {
|
||||
// True if this browser supports disabling async mode on dynamically
|
||||
// created script nodes. See
|
||||
// http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order
|
||||
async: doc.createElement('script').async === true
|
||||
};
|
||||
|
||||
(env.webkit = /AppleWebKit\//.test(ua))
|
||||
|| (env.ie = /MSIE|Trident/.test(ua))
|
||||
|| (env.opera = /Opera/.test(ua))
|
||||
|| (env.gecko = /Gecko\//.test(ua))
|
||||
|| (env.unknown = true);
|
||||
}
|
||||
|
||||
/**
|
||||
Loads the specified resources, or the next resource of the specified type
|
||||
in the queue if no resources are specified. If a resource of the specified
|
||||
type is already being loaded, the new request will be queued until the
|
||||
first request has been finished.
|
||||
|
||||
When an array of resource URLs is specified, those URLs will be loaded in
|
||||
parallel if it is possible to do so while preserving execution order. All
|
||||
browsers support parallel loading of CSS, but only Firefox and Opera
|
||||
support parallel loading of scripts. In other browsers, scripts will be
|
||||
queued and loaded one at a time to ensure correct execution order.
|
||||
|
||||
@method load
|
||||
@param {String} type resource type ('css' or 'js')
|
||||
@param {String|Array} urls (optional) URL or array of URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when the
|
||||
resource is loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function will
|
||||
be executed in this object's context
|
||||
@private
|
||||
*/
|
||||
function load(type, urls, callback, obj, context) {
|
||||
var _finish = function () { finish(type); },
|
||||
isCSS = type === 'css',
|
||||
nodes = [],
|
||||
i, len, node, p, pendingUrls, url;
|
||||
|
||||
env || getEnv();
|
||||
|
||||
if (urls) {
|
||||
// If urls is a string, wrap it in an array. Otherwise assume it's an
|
||||
// array and create a copy of it so modifications won't be made to the
|
||||
// original.
|
||||
urls = typeof urls === 'string' ? [urls] : urls.concat();
|
||||
|
||||
// Create a request object for each URL. If multiple URLs are specified,
|
||||
// the callback will only be executed after all URLs have been loaded.
|
||||
//
|
||||
// Sadly, Firefox and Opera are the only browsers capable of loading
|
||||
// scripts in parallel while preserving execution order. In all other
|
||||
// browsers, scripts must be loaded sequentially.
|
||||
//
|
||||
// All browsers respect CSS specificity based on the order of the link
|
||||
// elements in the DOM, regardless of the order in which the stylesheets
|
||||
// are actually downloaded.
|
||||
if (isCSS || env.async || env.gecko || env.opera) {
|
||||
// Load in parallel.
|
||||
queue[type].push({
|
||||
urls : urls,
|
||||
callback: callback,
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
} else {
|
||||
// Load sequentially.
|
||||
for (i = 0, len = urls.length; i < len; ++i) {
|
||||
queue[type].push({
|
||||
urls : [urls[i]],
|
||||
callback: i === len - 1 ? callback : null, // callback is only added to the last URL
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a previous load request of this type is currently in progress, we'll
|
||||
// wait our turn. Otherwise, grab the next item in the queue.
|
||||
if (pending[type] || !(p = pending[type] = queue[type].shift())) {
|
||||
return;
|
||||
}
|
||||
|
||||
head || (head = doc.head || doc.getElementsByTagName('head')[0]);
|
||||
pendingUrls = p.urls.concat();
|
||||
|
||||
for (i = 0, len = pendingUrls.length; i < len; ++i) {
|
||||
url = pendingUrls[i];
|
||||
|
||||
if (isCSS) {
|
||||
node = env.gecko ? createNode('style') : createNode('link', {
|
||||
href: url,
|
||||
rel : 'stylesheet'
|
||||
});
|
||||
} else {
|
||||
node = createNode('script', {src: url});
|
||||
node.async = false;
|
||||
}
|
||||
|
||||
node.className = 'lazyload';
|
||||
node.setAttribute('charset', 'utf-8');
|
||||
|
||||
if (env.ie && !isCSS && 'onreadystatechange' in node && !('draggable' in node)) {
|
||||
node.onreadystatechange = function () {
|
||||
if (/loaded|complete/.test(node.readyState)) {
|
||||
node.onreadystatechange = null;
|
||||
_finish();
|
||||
}
|
||||
};
|
||||
} else if (isCSS && (env.gecko || env.webkit)) {
|
||||
// Gecko and WebKit don't support the onload event on link nodes.
|
||||
if (env.webkit) {
|
||||
// In WebKit, we can poll for changes to document.styleSheets to
|
||||
// figure out when stylesheets have loaded.
|
||||
p.urls[i] = node.href; // resolve relative URLs (or polling won't work)
|
||||
pollWebKit();
|
||||
} else {
|
||||
// In Gecko, we can import the requested URL into a <style> node and
|
||||
// poll for the existence of node.sheet.cssRules. Props to Zach
|
||||
// Leatherman for calling my attention to this technique.
|
||||
node.innerHTML = '@import "' + url + '";';
|
||||
pollGecko(node);
|
||||
}
|
||||
} else {
|
||||
node.onload = node.onerror = _finish;
|
||||
}
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
for (i = 0, len = nodes.length; i < len; ++i) {
|
||||
head.appendChild(nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Begins polling to determine when the specified stylesheet has finished loading
|
||||
in Gecko. Polling stops when all pending stylesheets have loaded or after 10
|
||||
seconds (to prevent stalls).
|
||||
|
||||
Thanks to Zach Leatherman for calling my attention to the @import-based
|
||||
cross-domain technique used here, and to Oleg Slobodskoi for an earlier
|
||||
same-domain implementation. See Zach's blog for more details:
|
||||
http://www.zachleat.com/web/2010/07/29/load-css-dynamically/
|
||||
|
||||
@method pollGecko
|
||||
@param {HTMLElement} node Style node to poll.
|
||||
@private
|
||||
*/
|
||||
function pollGecko(node) {
|
||||
var hasRules;
|
||||
|
||||
try {
|
||||
// We don't really need to store this value or ever refer to it again, but
|
||||
// if we don't store it, Closure Compiler assumes the code is useless and
|
||||
// removes it.
|
||||
hasRules = !!node.sheet.cssRules;
|
||||
} catch (ex) {
|
||||
// An exception means the stylesheet is still loading.
|
||||
pollCount += 1;
|
||||
|
||||
if (pollCount < 200) {
|
||||
setTimeout(function () { pollGecko(node); }, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened. Stop
|
||||
// polling and finish the pending requests to avoid blocking further
|
||||
// requests.
|
||||
hasRules && finish('css');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we get here, the stylesheet has loaded.
|
||||
finish('css');
|
||||
}
|
||||
|
||||
/**
|
||||
Begins polling to determine when pending stylesheets have finished loading
|
||||
in WebKit. Polling stops when all pending stylesheets have loaded or after 10
|
||||
seconds (to prevent stalls).
|
||||
|
||||
@method pollWebKit
|
||||
@private
|
||||
*/
|
||||
function pollWebKit() {
|
||||
var css = pending.css, i;
|
||||
|
||||
if (css) {
|
||||
i = styleSheets.length;
|
||||
|
||||
// Look for a stylesheet matching the pending URL.
|
||||
while (--i >= 0) {
|
||||
if (styleSheets[i].href === css.urls[0]) {
|
||||
finish('css');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pollCount += 1;
|
||||
|
||||
if (css) {
|
||||
if (pollCount < 200) {
|
||||
setTimeout(pollWebKit, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened, which may
|
||||
// indicate that the stylesheet has been removed from the document
|
||||
// before it had a chance to load. Stop polling and finish the pending
|
||||
// request to prevent blocking further requests.
|
||||
finish('css');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
/**
|
||||
Requests the specified CSS URL or URLs and executes the specified
|
||||
callback (if any) when they have finished loading. If an array of URLs is
|
||||
specified, the stylesheets will be loaded in parallel and the callback
|
||||
will be executed after all stylesheets have finished loading.
|
||||
|
||||
@method css
|
||||
@param {String|Array} urls CSS URL or array of CSS URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when
|
||||
the specified stylesheets are loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function
|
||||
will be executed in this object's context
|
||||
@static
|
||||
*/
|
||||
css: function (urls, callback, obj, context) {
|
||||
load('css', urls, callback, obj, context);
|
||||
},
|
||||
|
||||
/**
|
||||
Requests the specified JavaScript URL or URLs and executes the specified
|
||||
callback (if any) when they have finished loading. If an array of URLs is
|
||||
specified and the browser supports it, the scripts will be loaded in
|
||||
parallel and the callback will be executed after all scripts have
|
||||
finished loading.
|
||||
|
||||
Currently, only Firefox and Opera support parallel loading of scripts while
|
||||
preserving execution order. In other browsers, scripts will be
|
||||
queued and loaded one at a time to ensure correct execution order.
|
||||
|
||||
@method js
|
||||
@param {String|Array} urls JS URL or array of JS URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when
|
||||
the specified scripts are loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function
|
||||
will be executed in this object's context
|
||||
@static
|
||||
*/
|
||||
js: function (urls, callback, obj, context) {
|
||||
load('js', urls, callback, obj, context);
|
||||
}
|
||||
|
||||
};
|
||||
})(this.document);
|
|
@ -57,3 +57,5 @@
|
|||
top:0;
|
||||
right:0;
|
||||
}
|
||||
|
||||
|
|
@ -1,47 +1,31 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<link href="Squire-UI.css" rel="stylesheet" type="text/css" type="text/css" rel="stylesheet" />
|
||||
<link href="assets/drop/drop-theme-arrows.css" type="text/css" rel="stylesheet" />
|
||||
|
||||
<link href="assets/font-awesome/font-awesome.min.css" type="text/css" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="menu" contenteditable="false">
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-bold"></i></div>
|
||||
<div class="item"><i class="fa fa-italic"></i></div>
|
||||
<div class="item"><i class="fa fa-underline"></i></div>
|
||||
<div id="font" class="item">
|
||||
<i class="fa fa-font"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-link"></i></div>
|
||||
<div class="item"><i class="fa fa-quote-right"></i></div>
|
||||
<div class="item"><i class="fa fa-list"></i></div>
|
||||
<div class="item">
|
||||
<i class="fa fa-picture-o"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-undo"></i></div>
|
||||
<div class="item"><i class="fa fa-undo flip"></i></div>
|
||||
</div>
|
||||
</header>
|
||||
<div class="templates hidden">
|
||||
<div class="drop-font">
|
||||
<i class="fa fa-chevron-up quit" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"></i>
|
||||
Text Size: <input type="number" min="12" max="72" step="6" value="12" name="font-size">
|
||||
<br>
|
||||
Font: <input type="text">
|
||||
|
||||
<div class="menu" contenteditable="false">
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-bold"></i></div>
|
||||
<div class="item"><i class="fa fa-italic"></i></div>
|
||||
<div class="item"><i class="fa fa-underline"></i></div>
|
||||
<div id="font" class="item">
|
||||
<i class="fa fa-font"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-link"></i></div>
|
||||
<div class="item"><i class="fa fa-quote-right"></i></div>
|
||||
<div class="item"><i class="fa fa-list"></i></div>
|
||||
<div class="item">
|
||||
<i class="fa fa-picture-o"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="group">
|
||||
<div class="item"><i class="fa fa-undo"></i></div>
|
||||
<div class="item"><i class="fa fa-undo flip"></i></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="templates hidden">
|
||||
<div class="drop-font">
|
||||
<i class="fa fa-chevron-up quit" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"></i>
|
||||
Text Size: <input type="number" min="12" max="72" step="6" value="12" name="font-size">
|
||||
<br>
|
||||
Font: <input type="text">
|
||||
|
||||
<script src="assets/drop/drop.min.js" type="text/javascript"></script>
|
||||
<script src="Squire-UI.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</div>
|
||||
</div>
|
||||
|
|
232
ui/Squire-UI.js
232
ui/Squire-UI.js
|
@ -1,13 +1,231 @@
|
|||
|
||||
LazyLoad = (function (doc) {
|
||||
var env,
|
||||
|
||||
head,
|
||||
pending = {},
|
||||
pollCount = 0,
|
||||
queue = {css: [], js: []},
|
||||
styleSheets = doc.styleSheets;
|
||||
function createNode(name, attrs) {
|
||||
var node = doc.createElement(name), attr;
|
||||
|
||||
for (attr in attrs) {
|
||||
if (attrs.hasOwnProperty(attr)) {
|
||||
node.setAttribute(attr, attrs[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
function finish(type) {
|
||||
var p = pending[type],
|
||||
callback,
|
||||
urls;
|
||||
|
||||
if (p) {
|
||||
callback = p.callback;
|
||||
urls = p.urls;
|
||||
|
||||
urls.shift();
|
||||
pollCount = 0;
|
||||
|
||||
// If this is the last of the pending URLs, execute the callback and
|
||||
// start the next request in the queue (if any).
|
||||
if (!urls.length) {
|
||||
callback && callback.call(p.context, p.obj);
|
||||
pending[type] = null;
|
||||
queue[type].length && load(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getEnv() {
|
||||
var ua = navigator.userAgent;
|
||||
|
||||
env = {
|
||||
async: doc.createElement('script').async === true
|
||||
};
|
||||
|
||||
(env.webkit = /AppleWebKit\//.test(ua))
|
||||
|| (env.ie = /MSIE|Trident/.test(ua))
|
||||
|| (env.opera = /Opera/.test(ua))
|
||||
|| (env.gecko = /Gecko\//.test(ua))
|
||||
|| (env.unknown = true);
|
||||
}
|
||||
|
||||
function load(type, urls, callback, obj, context) {
|
||||
var _finish = function () { finish(type); },
|
||||
isCSS = type === 'css',
|
||||
nodes = [],
|
||||
i, len, node, p, pendingUrls, url;
|
||||
|
||||
env || getEnv();
|
||||
|
||||
if (urls) {
|
||||
urls = typeof urls === 'string' ? [urls] : urls.concat();
|
||||
|
||||
if (isCSS || env.async || env.gecko || env.opera) {
|
||||
// Load in parallel.
|
||||
queue[type].push({
|
||||
urls : urls,
|
||||
callback: callback,
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
} else {
|
||||
// Load sequentially.
|
||||
for (i = 0, len = urls.length; i < len; ++i) {
|
||||
queue[type].push({
|
||||
urls : [urls[i]],
|
||||
callback: i === len - 1 ? callback : null, // callback is only added to the last URL
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a previous load request of this type is currently in progress, we'll
|
||||
// wait our turn. Otherwise, grab the next item in the queue.
|
||||
if (pending[type] || !(p = pending[type] = queue[type].shift())) {
|
||||
return;
|
||||
}
|
||||
|
||||
head || (head = doc.head || doc.getElementsByTagName('head')[0]);
|
||||
pendingUrls = p.urls.concat();
|
||||
|
||||
for (i = 0, len = pendingUrls.length; i < len; ++i) {
|
||||
url = pendingUrls[i];
|
||||
|
||||
if (isCSS) {
|
||||
node = env.gecko ? createNode('style') : createNode('link', {
|
||||
href: url,
|
||||
rel : 'stylesheet'
|
||||
});
|
||||
} else {
|
||||
node = createNode('script', {src: url});
|
||||
node.async = false;
|
||||
}
|
||||
|
||||
node.className = 'lazyload';
|
||||
node.setAttribute('charset', 'utf-8');
|
||||
|
||||
if (env.ie && !isCSS && 'onreadystatechange' in node && !('draggable' in node)) {
|
||||
node.onreadystatechange = function () {
|
||||
if (/loaded|complete/.test(node.readyState)) {
|
||||
node.onreadystatechange = null;
|
||||
_finish();
|
||||
}
|
||||
};
|
||||
} else if (isCSS && (env.gecko || env.webkit)) {
|
||||
if (env.webkit) {
|
||||
p.urls[i] = node.href; // resolve relative URLs (or polling won't work)
|
||||
pollWebKit();
|
||||
} else {
|
||||
node.innerHTML = '@import "' + url + '";';
|
||||
pollGecko(node);
|
||||
}
|
||||
} else {
|
||||
node.onload = node.onerror = _finish;
|
||||
}
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
for (i = 0, len = nodes.length; i < len; ++i) {
|
||||
head.appendChild(nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function pollGecko(node) {
|
||||
var hasRules;
|
||||
|
||||
try {
|
||||
// We don't really need to store this value or ever refer to it again, but
|
||||
// if we don't store it, Closure Compiler assumes the code is useless and
|
||||
// removes it.
|
||||
hasRules = !!node.sheet.cssRules;
|
||||
} catch (ex) {
|
||||
// An exception means the stylesheet is still loading.
|
||||
pollCount += 1;
|
||||
|
||||
if (pollCount < 200) {
|
||||
setTimeout(function () { pollGecko(node); }, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened. Stop
|
||||
// polling and finish the pending requests to avoid blocking further
|
||||
// requests.
|
||||
hasRules && finish('css');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we get here, the stylesheet has loaded.
|
||||
finish('css');
|
||||
}
|
||||
|
||||
function pollWebKit() {
|
||||
var css = pending.css, i;
|
||||
|
||||
if (css) {
|
||||
i = styleSheets.length;
|
||||
|
||||
// Look for a stylesheet matching the pending URL.
|
||||
while (--i >= 0) {
|
||||
if (styleSheets[i].href === css.urls[0]) {
|
||||
finish('css');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pollCount += 1;
|
||||
|
||||
if (css) {
|
||||
if (pollCount < 200) {
|
||||
setTimeout(pollWebKit, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened, which may
|
||||
// indicate that the stylesheet has been removed from the document
|
||||
// before it had a chance to load. Stop polling and finish the pending
|
||||
// request to prevent blocking further requests.
|
||||
finish('css');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
css: function (urls, callback, obj, context) {
|
||||
load('css', urls, callback, obj, context);
|
||||
},
|
||||
js: function (urls, callback, obj, context) {
|
||||
load('js', urls, callback, obj, context);
|
||||
}
|
||||
|
||||
};
|
||||
})(this.document);
|
||||
|
||||
|
||||
(function() {
|
||||
var drop, quit;
|
||||
LazyLoad.js(['build/squire.js','build/assets/jQuery/jQuery.js','build/assets/drop/drop.min.js']);
|
||||
LazyLoad.css(['build/Squire-UI.css','build/assets/font-awesome/font-awesome.min.css', 'build/assets/drop/drop-theme-hubspot-popovers.css']);
|
||||
|
||||
SquireUI = function (options) {
|
||||
var originElement = $(options.replace);
|
||||
var container = originElement.parent();
|
||||
|
||||
originElement.remove();
|
||||
var editorContainer = $('<iframe />').appendTo(container);
|
||||
var editorDocument = editorContainer[0].contentWindow.document;
|
||||
|
||||
drop = new Drop({
|
||||
target: document.querySelector('#font'),
|
||||
content: document.querySelector('.drop-font').innerHTML,
|
||||
position: 'bottom left',
|
||||
openOn: 'click'
|
||||
});
|
||||
var editor = new Squire(editorDocument);
|
||||
|
||||
return editor;
|
||||
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
|
|
4
ui/assets/jQuery/jQuery.js
Normal file
4
ui/assets/jQuery/jQuery.js
Normal file
File diff suppressed because one or more lines are too long
390
ui/assets/lazyload/lazyload.js
Normal file
390
ui/assets/lazyload/lazyload.js
Normal file
|
@ -0,0 +1,390 @@
|
|||
/*jslint browser: true, eqeqeq: true, bitwise: true, newcap: true, immed: true, regexp: false */
|
||||
|
||||
/**
|
||||
LazyLoad makes it easy and painless to lazily load one or more external
|
||||
JavaScript or CSS files on demand either during or after the rendering of a web
|
||||
page.
|
||||
|
||||
Supported browsers include Firefox 2+, IE6+, Safari 3+ (including Mobile
|
||||
Safari), Google Chrome, and Opera 9+. Other browsers may or may not work and
|
||||
are not officially supported.
|
||||
|
||||
Visit https://github.com/rgrove/lazyload/ for more info.
|
||||
|
||||
Copyright (c) 2011 Ryan Grove <ryan@wonko.com>
|
||||
All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the 'Software'), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
@module lazyload
|
||||
@class LazyLoad
|
||||
@static
|
||||
*/
|
||||
|
||||
LazyLoad = (function (doc) {
|
||||
// -- Private Variables ------------------------------------------------------
|
||||
|
||||
// User agent and feature test information.
|
||||
var env,
|
||||
|
||||
// Reference to the <head> element (populated lazily).
|
||||
head,
|
||||
|
||||
// Requests currently in progress, if any.
|
||||
pending = {},
|
||||
|
||||
// Number of times we've polled to check whether a pending stylesheet has
|
||||
// finished loading. If this gets too high, we're probably stalled.
|
||||
pollCount = 0,
|
||||
|
||||
// Queued requests.
|
||||
queue = {css: [], js: []},
|
||||
|
||||
// Reference to the browser's list of stylesheets.
|
||||
styleSheets = doc.styleSheets;
|
||||
|
||||
// -- Private Methods --------------------------------------------------------
|
||||
|
||||
/**
|
||||
Creates and returns an HTML element with the specified name and attributes.
|
||||
|
||||
@method createNode
|
||||
@param {String} name element name
|
||||
@param {Object} attrs name/value mapping of element attributes
|
||||
@return {HTMLElement}
|
||||
@private
|
||||
*/
|
||||
function createNode(name, attrs) {
|
||||
var node = doc.createElement(name), attr;
|
||||
|
||||
for (attr in attrs) {
|
||||
if (attrs.hasOwnProperty(attr)) {
|
||||
node.setAttribute(attr, attrs[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
Called when the current pending resource of the specified type has finished
|
||||
loading. Executes the associated callback (if any) and loads the next
|
||||
resource in the queue.
|
||||
|
||||
@method finish
|
||||
@param {String} type resource type ('css' or 'js')
|
||||
@private
|
||||
*/
|
||||
function finish(type) {
|
||||
var p = pending[type],
|
||||
callback,
|
||||
urls;
|
||||
|
||||
if (p) {
|
||||
callback = p.callback;
|
||||
urls = p.urls;
|
||||
|
||||
urls.shift();
|
||||
pollCount = 0;
|
||||
|
||||
// If this is the last of the pending URLs, execute the callback and
|
||||
// start the next request in the queue (if any).
|
||||
if (!urls.length) {
|
||||
callback && callback.call(p.context, p.obj);
|
||||
pending[type] = null;
|
||||
queue[type].length && load(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Populates the <code>env</code> variable with user agent and feature test
|
||||
information.
|
||||
|
||||
@method getEnv
|
||||
@private
|
||||
*/
|
||||
function getEnv() {
|
||||
var ua = navigator.userAgent;
|
||||
|
||||
env = {
|
||||
// True if this browser supports disabling async mode on dynamically
|
||||
// created script nodes. See
|
||||
// http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order
|
||||
async: doc.createElement('script').async === true
|
||||
};
|
||||
|
||||
(env.webkit = /AppleWebKit\//.test(ua))
|
||||
|| (env.ie = /MSIE|Trident/.test(ua))
|
||||
|| (env.opera = /Opera/.test(ua))
|
||||
|| (env.gecko = /Gecko\//.test(ua))
|
||||
|| (env.unknown = true);
|
||||
}
|
||||
|
||||
/**
|
||||
Loads the specified resources, or the next resource of the specified type
|
||||
in the queue if no resources are specified. If a resource of the specified
|
||||
type is already being loaded, the new request will be queued until the
|
||||
first request has been finished.
|
||||
|
||||
When an array of resource URLs is specified, those URLs will be loaded in
|
||||
parallel if it is possible to do so while preserving execution order. All
|
||||
browsers support parallel loading of CSS, but only Firefox and Opera
|
||||
support parallel loading of scripts. In other browsers, scripts will be
|
||||
queued and loaded one at a time to ensure correct execution order.
|
||||
|
||||
@method load
|
||||
@param {String} type resource type ('css' or 'js')
|
||||
@param {String|Array} urls (optional) URL or array of URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when the
|
||||
resource is loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function will
|
||||
be executed in this object's context
|
||||
@private
|
||||
*/
|
||||
function load(type, urls, callback, obj, context) {
|
||||
var _finish = function () { finish(type); },
|
||||
isCSS = type === 'css',
|
||||
nodes = [],
|
||||
i, len, node, p, pendingUrls, url;
|
||||
|
||||
env || getEnv();
|
||||
|
||||
if (urls) {
|
||||
// If urls is a string, wrap it in an array. Otherwise assume it's an
|
||||
// array and create a copy of it so modifications won't be made to the
|
||||
// original.
|
||||
urls = typeof urls === 'string' ? [urls] : urls.concat();
|
||||
|
||||
// Create a request object for each URL. If multiple URLs are specified,
|
||||
// the callback will only be executed after all URLs have been loaded.
|
||||
//
|
||||
// Sadly, Firefox and Opera are the only browsers capable of loading
|
||||
// scripts in parallel while preserving execution order. In all other
|
||||
// browsers, scripts must be loaded sequentially.
|
||||
//
|
||||
// All browsers respect CSS specificity based on the order of the link
|
||||
// elements in the DOM, regardless of the order in which the stylesheets
|
||||
// are actually downloaded.
|
||||
if (isCSS || env.async || env.gecko || env.opera) {
|
||||
// Load in parallel.
|
||||
queue[type].push({
|
||||
urls : urls,
|
||||
callback: callback,
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
} else {
|
||||
// Load sequentially.
|
||||
for (i = 0, len = urls.length; i < len; ++i) {
|
||||
queue[type].push({
|
||||
urls : [urls[i]],
|
||||
callback: i === len - 1 ? callback : null, // callback is only added to the last URL
|
||||
obj : obj,
|
||||
context : context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a previous load request of this type is currently in progress, we'll
|
||||
// wait our turn. Otherwise, grab the next item in the queue.
|
||||
if (pending[type] || !(p = pending[type] = queue[type].shift())) {
|
||||
return;
|
||||
}
|
||||
|
||||
head || (head = doc.head || doc.getElementsByTagName('head')[0]);
|
||||
pendingUrls = p.urls.concat();
|
||||
|
||||
for (i = 0, len = pendingUrls.length; i < len; ++i) {
|
||||
url = pendingUrls[i];
|
||||
|
||||
if (isCSS) {
|
||||
node = env.gecko ? createNode('style') : createNode('link', {
|
||||
href: url,
|
||||
rel : 'stylesheet'
|
||||
});
|
||||
} else {
|
||||
node = createNode('script', {src: url});
|
||||
node.async = false;
|
||||
}
|
||||
|
||||
node.className = 'lazyload';
|
||||
node.setAttribute('charset', 'utf-8');
|
||||
|
||||
if (env.ie && !isCSS && 'onreadystatechange' in node && !('draggable' in node)) {
|
||||
node.onreadystatechange = function () {
|
||||
if (/loaded|complete/.test(node.readyState)) {
|
||||
node.onreadystatechange = null;
|
||||
_finish();
|
||||
}
|
||||
};
|
||||
} else if (isCSS && (env.gecko || env.webkit)) {
|
||||
// Gecko and WebKit don't support the onload event on link nodes.
|
||||
if (env.webkit) {
|
||||
// In WebKit, we can poll for changes to document.styleSheets to
|
||||
// figure out when stylesheets have loaded.
|
||||
p.urls[i] = node.href; // resolve relative URLs (or polling won't work)
|
||||
pollWebKit();
|
||||
} else {
|
||||
// In Gecko, we can import the requested URL into a <style> node and
|
||||
// poll for the existence of node.sheet.cssRules. Props to Zach
|
||||
// Leatherman for calling my attention to this technique.
|
||||
node.innerHTML = '@import "' + url + '";';
|
||||
pollGecko(node);
|
||||
}
|
||||
} else {
|
||||
node.onload = node.onerror = _finish;
|
||||
}
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
for (i = 0, len = nodes.length; i < len; ++i) {
|
||||
head.appendChild(nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Begins polling to determine when the specified stylesheet has finished loading
|
||||
in Gecko. Polling stops when all pending stylesheets have loaded or after 10
|
||||
seconds (to prevent stalls).
|
||||
|
||||
Thanks to Zach Leatherman for calling my attention to the @import-based
|
||||
cross-domain technique used here, and to Oleg Slobodskoi for an earlier
|
||||
same-domain implementation. See Zach's blog for more details:
|
||||
http://www.zachleat.com/web/2010/07/29/load-css-dynamically/
|
||||
|
||||
@method pollGecko
|
||||
@param {HTMLElement} node Style node to poll.
|
||||
@private
|
||||
*/
|
||||
function pollGecko(node) {
|
||||
var hasRules;
|
||||
|
||||
try {
|
||||
// We don't really need to store this value or ever refer to it again, but
|
||||
// if we don't store it, Closure Compiler assumes the code is useless and
|
||||
// removes it.
|
||||
hasRules = !!node.sheet.cssRules;
|
||||
} catch (ex) {
|
||||
// An exception means the stylesheet is still loading.
|
||||
pollCount += 1;
|
||||
|
||||
if (pollCount < 200) {
|
||||
setTimeout(function () { pollGecko(node); }, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened. Stop
|
||||
// polling and finish the pending requests to avoid blocking further
|
||||
// requests.
|
||||
hasRules && finish('css');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we get here, the stylesheet has loaded.
|
||||
finish('css');
|
||||
}
|
||||
|
||||
/**
|
||||
Begins polling to determine when pending stylesheets have finished loading
|
||||
in WebKit. Polling stops when all pending stylesheets have loaded or after 10
|
||||
seconds (to prevent stalls).
|
||||
|
||||
@method pollWebKit
|
||||
@private
|
||||
*/
|
||||
function pollWebKit() {
|
||||
var css = pending.css, i;
|
||||
|
||||
if (css) {
|
||||
i = styleSheets.length;
|
||||
|
||||
// Look for a stylesheet matching the pending URL.
|
||||
while (--i >= 0) {
|
||||
if (styleSheets[i].href === css.urls[0]) {
|
||||
finish('css');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pollCount += 1;
|
||||
|
||||
if (css) {
|
||||
if (pollCount < 200) {
|
||||
setTimeout(pollWebKit, 50);
|
||||
} else {
|
||||
// We've been polling for 10 seconds and nothing's happened, which may
|
||||
// indicate that the stylesheet has been removed from the document
|
||||
// before it had a chance to load. Stop polling and finish the pending
|
||||
// request to prevent blocking further requests.
|
||||
finish('css');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
/**
|
||||
Requests the specified CSS URL or URLs and executes the specified
|
||||
callback (if any) when they have finished loading. If an array of URLs is
|
||||
specified, the stylesheets will be loaded in parallel and the callback
|
||||
will be executed after all stylesheets have finished loading.
|
||||
|
||||
@method css
|
||||
@param {String|Array} urls CSS URL or array of CSS URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when
|
||||
the specified stylesheets are loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function
|
||||
will be executed in this object's context
|
||||
@static
|
||||
*/
|
||||
css: function (urls, callback, obj, context) {
|
||||
load('css', urls, callback, obj, context);
|
||||
},
|
||||
|
||||
/**
|
||||
Requests the specified JavaScript URL or URLs and executes the specified
|
||||
callback (if any) when they have finished loading. If an array of URLs is
|
||||
specified and the browser supports it, the scripts will be loaded in
|
||||
parallel and the callback will be executed after all scripts have
|
||||
finished loading.
|
||||
|
||||
Currently, only Firefox and Opera support parallel loading of scripts while
|
||||
preserving execution order. In other browsers, scripts will be
|
||||
queued and loaded one at a time to ensure correct execution order.
|
||||
|
||||
@method js
|
||||
@param {String|Array} urls JS URL or array of JS URLs to load
|
||||
@param {Function} callback (optional) callback function to execute when
|
||||
the specified scripts are loaded
|
||||
@param {Object} obj (optional) object to pass to the callback function
|
||||
@param {Object} context (optional) if provided, the callback function
|
||||
will be executed in this object's context
|
||||
@static
|
||||
*/
|
||||
js: function (urls, callback, obj, context) {
|
||||
load('js', urls, callback, obj, context);
|
||||
}
|
||||
|
||||
};
|
||||
})(this.document);
|
Loading…
Add table
Reference in a new issue