Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add simulated web worker to support IE9 and Mobile Safari

  • Loading branch information...
commit c96f86ed49a0635d013608b7e5655fc532889765 1 parent 5c4ef63
@timdream authored
View
5 en/index.html
@@ -132,9 +132,14 @@
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="../jquery.wordcloud.js"></script>
<script type="text/javascript" src="../jquery.getcontent.js"></script>
+<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../wordfreq.js"></script>
<script type="text/javascript" src="../wordcloud.js"></script>
<script type="text/javascript">
+
+window.Worker.iframeURI = '../worker.iframe.html';
+window.Worker.baseURI = 'en/';
+
var T = {
title: 'Word Clouds for $1',
downloading: 'Downloading ...',
View
4 tests/wordfreq.html
@@ -57,12 +57,16 @@
<textarea id="result" readonly="true" cols="80" rows="30">
</textarea>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
+<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../wordfreq.js"></script>
<script type="text/javascript">
jQuery(function ($) {
$('#supported').addClass((WordFreq.supported?'':'not_') + 'support');
+ window.Worker.iframeURI = '../worker.iframe.html';
+ window.Worker.baseURI = 'tests/';
+
var $r = $('#result');
function start() {
$r.val('start');
View
5 wordfreq.worker.js
@@ -1,6 +1,9 @@
"use strict";
+// For simulated worker in IE
+var send = (typeof workerPostMessage !== 'undefined')?workerPostMessage:postMessage;
+
// http://tartarus.org/~martin/PorterStemmer/js.txt
// Porter stemmer in Javascript
// Release 1 be 'andargor', Jul 2004
@@ -111,5 +114,5 @@ onmessage = function (ev) {
if (settings.processCJK) processCJK(text);
if (settings.processEnglish) processEnglish(text);
- postMessage({words:words, reps:reps});
+ send({words:words, reps:reps});
};
View
9 worker.iframe.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset=UTF-8>
+ <title>iframe worker</title>
+</head>
+<body>
+</body>
+</html>
View
101 worker.js
@@ -0,0 +1,101 @@
+if (!window.Worker || window.forceIframeWorker) {
+ if (window.Worker) window.nativeWorker = window.Worker;
+ window.Worker = function (script) {
+ var worker = this;
+
+ // prepare and inject iframe
+ worker._iframeEl = document.createElement('iframe');
+ worker._iframeEl.style.visibility = 'hidden';
+ worker._iframeEl.style.width = '1px';
+ worker._iframeEl.style.height = '1px';
+ worker._iframeEl.onload = function () {
+ var w = this.contentWindow,
+ doc = this.contentWindow.document;
+
+ // Some interfaces within the Worker scope.
+
+ w.Worker = window.Worker; // yes, worker could spawn another worker!
+ w.onmessage = function (ev) {}; // placeholder function
+ var postMessage = function (data) {
+ if (typeof worker.onmessage === 'function') {
+ worker.onmessage.call(
+ worker,
+ {
+ currentTarget: worker,
+ timeStamp: (new Date()).getTime(),
+ srcElement: worker,
+ target: worker,
+ data: data
+ }
+ );
+ }
+ };
+ w.postMessage = w.workerPostMessage = postMessage;
+ if (w.postMessage !== postMessage) {
+ // IE doesn't allow overwriting postMessage
+ }
+ w.close = function () {
+ worker.terminate();
+ };
+ w.importScripts = function () {
+ for (var i = 0; i < arguments.length; i++) {
+ var scriptEl = doc.createElement('script');
+ scriptEl.src = window.Worker.baseURI + script;
+ scriptEl.type = 'text/javascript';
+ doc.body.appendChild(scriptEl);
+ }
+ }
+
+ // inject worker script into iframe
+ var scriptEl = doc.createElement('script');
+ scriptEl.src = window.Worker.baseURI + script;
+ scriptEl.type = 'text/javascript';
+ scriptEl.onload = function () {
+ worker._quere.push = function (callback) {
+ if (!worker._unloaded) {
+ callback();
+ }
+ };
+ if (!worker._unloaded) {
+ while (worker._quere.length) {
+ (worker._quere.shift())();
+ }
+ }
+ };
+ doc.body.appendChild(scriptEl);
+ };
+ this._iframeEl.src = window.Worker.iframeURI;
+ (document.getElementsByTagName('head')[0] || document.body).appendChild(this._iframeEl);
+
+ worker._quere = [];
+ worker._unloaded = false;
+ };
+ window.Worker.prototype.postMessage = function (obj) {
+ var worker = this;
+ setTimeout(
+ function () {
+ worker._quere.push(
+ function () {
+ worker._iframeEl.contentWindow.onmessage({data:obj});
+ }
+ );
+ },
+ 0
+ );
+ };
+ window.Worker.prototype.terminate = function () {
+ if (!this._unloaded) {
+ (document.getElementsByTagName('head')[0] || document.body).removeChild(this._iframeEl);
+ }
+ this._iframeEl = null;
+ this._unloaded = true;
+ };
+ window.Worker.prototype.addEventListener = function () {
+ };
+ window.Worker.prototype.removeEventListener = function () {
+ };
+
+ window.Worker.notNative = true;
+ window.Worker.iframeURI = './worker.iframe.html';
+ window.Worker.baseURI = '';
+}
View
5 zh/index.html
@@ -135,9 +135,14 @@
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="../jquery.wordcloud.js"></script>
<script type="text/javascript" src="../jquery.getcontent.js"></script>
+<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../wordfreq.js"></script>
<script type="text/javascript" src="../wordcloud.js"></script>
<script type="text/javascript">
+
+window.Worker.iframeURI = '../worker.iframe.html';
+window.Worker.baseURI = 'zh/';
+
var T = {
title: '$1 的文字雲',
downloading: '下載中 ...',
Please sign in to comment.
Something went wrong with that request. Please try again.