]> _ Git - fluidbook-html5.git/commitdiff
wip #2342 @5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 14 Nov 2018 19:03:59 +0000 (20:03 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 14 Nov 2018 19:03:59 +0000 (20:03 +0100)
js/libs/fluidbook/fluidbook.sound.js
js/libs/fluidbook/workers/loader.js
js/libs/fluidbook/workers/stats.js
js/libs/polyfills/promise.min.js [new file with mode: 0644]

index 8d0daa7d29fe941454e343d42df96d98af59d599..c3f6b2b3174fa7d3a15b61595fa84f80af551091 100644 (file)
@@ -109,7 +109,6 @@ FluidbookSound.prototype = {
             sound = 'page-flip-' + (Math.round(Math.random() + 1));
         }
 
-        console.log(sound);
         try {
             this.audios[sound].volume = 1;
             this.audios[sound].fastSeek(0);
index 38affb1a802d0b3a19a9c4e6ae41849592b7c95f..3cf04ebdfccc0820622b69970c6e77c2249a1bb3 100644 (file)
@@ -1,8 +1,76 @@
+importScripts('../../stand/axios.min.js', '../../polyfills/promise.min.js');
+
 onmessage = function (e) {
-    var url = 'https://stats.fluidbook.com/stats2.php?' + Object.keys(e.data).reduce(function (a, k) {
-        a.push(k + '=' + encodeURIComponent(e.data[k]));
-        return a
-    }, []).join('&');
-    axios.get(url);
-    postMessage('');
+    var data = e.data;
+
+    var response = {'request': data};
+    var callback = function () {
+        postMessage(response);
+    };
+
+    switch (data.action) {
+        case 'loadImage':
+            response.return = loadImage('../../../../' + data.src, data.width, data.height, data.type, callback);
+            break;
+        default:
+            break;
+    }
+};
+
+loadImage = function (src, width, height, type, callback) {
+
+    var callbackCalled = false;
+
+    var callCallback = function () {
+        setTimeout(function () {
+            if (!callbackCalled) {
+                callbackCalled = true;
+                callback();
+            }
+        }, 10);
+    };
+
+    axios.get(src, {
+        responseType: 'arraybuffer'
+    }).then(function (response) {
+        var res = '<img ';
+        if (width != undefined && width != null) {
+            res += 'width="' + width + '" ';
+        }
+        if (height != undefined && height != null) {
+            res += 'height="' + height + '" ';
+        }
+        if (type != undefined && type != null) {
+            res += 'type="' + type + '" ';
+        }
+        res += 'src="data:image/png;base64,' + new Buffer(response.data, 'binary').toString('base64') + '">';
+        response.img = res;
+    }).catch(function (error) {
+        // handle error
+        console.log(error);
+    }).then(function () {
+        console.log('always executed');
+        // always executed
+    });
+
+    // img.addEventListener('load', function () {
+    //     callCallback();
+    // });
+    // img.addEventListener('error', function () {
+    //     callCallback();
+    // });
+    // img.src = src;
+    // if (width != undefined && width != null) {
+    //     img.width = width;
+    // }
+    // if (height != undefined && height != null) {
+    //     img.height = height;
+    // }
+    // if (type != undefined && type != null) {
+    //     img.type = type;
+    // }
+    // if (img.complete || img.readyState === 'complete' || img.readyState === 4) {
+    //     callCallback();
+    // }
+    // return img;
 }
\ No newline at end of file
index ec9350b113e2f3ed5a01dff3d75d75f8d08d5d53..5931eb29bba7f83abba51f3073a5e60078753541 100644 (file)
@@ -1,4 +1,4 @@
-importScripts('../../stand/axios.min.js');
+importScripts('../../stand/axios.min.js', '../../polyfills/promise.min.js');
 
 onmessage = function (e) {
     var url = 'https://stats.fluidbook.com/stats2.php?' + Object.keys(e.data).reduce(function (a, k) {
diff --git a/js/libs/polyfills/promise.min.js b/js/libs/polyfills/promise.min.js
new file mode 100644 (file)
index 0000000..425c164
--- /dev/null
@@ -0,0 +1 @@
+!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(0,function(){"use strict";function e(e){var n=this.constructor;return this.then(function(t){return n.resolve(e()).then(function(){return t})},function(t){return n.resolve(e()).then(function(){return n.reject(t)})})}function n(){}function t(e){if(!(this instanceof t))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=undefined,this._deferreds=[],u(e,this)}function o(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,t._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var o;try{o=t(e._value)}catch(f){return void i(n.promise,f)}r(n.promise,o)}else(1===e._state?r:i)(n.promise,e._value)})):e._deferreds.push(n)}function r(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var o=n.then;if(n instanceof t)return e._state=3,e._value=n,void f(e);if("function"==typeof o)return void u(function(e,n){return function(){e.apply(n,arguments)}}(o,n),e)}e._state=1,e._value=n,f(e)}catch(r){i(e,r)}}function i(e,n){e._state=2,e._value=n,f(e)}function f(e){2===e._state&&0===e._deferreds.length&&t._immediateFn(function(){e._handled||t._unhandledRejectionFn(e._value)});for(var n=0,r=e._deferreds.length;r>n;n++)o(e,e._deferreds[n]);e._deferreds=null}function u(e,n){var t=!1;try{e(function(e){t||(t=!0,r(n,e))},function(e){t||(t=!0,i(n,e))})}catch(o){if(t)return;t=!0,i(n,o)}}var c=setTimeout;t.prototype["catch"]=function(e){return this.then(null,e)},t.prototype.then=function(e,t){var r=new this.constructor(n);return o(this,new function(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}(e,t,r)),r},t.prototype["finally"]=e,t.all=function(e){return new t(function(n,t){function o(e,f){try{if(f&&("object"==typeof f||"function"==typeof f)){var u=f.then;if("function"==typeof u)return void u.call(f,function(n){o(e,n)},t)}r[e]=f,0==--i&&n(r)}catch(c){t(c)}}if(!e||"undefined"==typeof e.length)throw new TypeError("Promise.all accepts an array");var r=Array.prototype.slice.call(e);if(0===r.length)return n([]);for(var i=r.length,f=0;r.length>f;f++)o(f,r[f])})},t.resolve=function(e){return e&&"object"==typeof e&&e.constructor===t?e:new t(function(n){n(e)})},t.reject=function(e){return new t(function(n,t){t(e)})},t.race=function(e){return new t(function(n,t){for(var o=0,r=e.length;r>o;o++)e[o].then(n,t)})},t._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){c(e,0)},t._unhandledRejectionFn=function(e){void 0!==console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var l=function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw Error("unable to locate global object")}();"Promise"in l?l.Promise.prototype["finally"]||(l.Promise.prototype["finally"]=e):l.Promise=t});