drag an image into the browser window to modify it.
-
this script corrupts some bytes in a jpg image. because of the way jpg encoding works, the corrupted file still shows something. inspired by soulwires experiment in flash. this experiment was created by georg. you can follow him on twitter or explore the source code on github.
With this app, you can glitch your own images by dragging an image into the browser window. Use the sliders in the control panel to alter the glitched parameters. The image updates in real time.
+
This app corrupts some bytes in an image. Because of the way JPEG encoding works, the corrupted file still shows a corrupted image. It was inspired by soulwire’s experiment in Flash.
+
This tool was created by Georg. He is always happy to learn about the things that people are creating with his tools. You can follow him on Twitter or explore the source code of this app on GitHub.
+
If you like the glitcher, you can check out some of Georg’s other JavaScript experiments.
+
+ Please note: javascript and modern browsers are required for this app to work.
+
+
+
+
-
+
\ No newline at end of file
diff --git a/lang/de-de.json b/lang/de-de.json
new file mode 100644
index 0000000..7685117
--- /dev/null
+++ b/lang/de-de.json
@@ -0,0 +1,134 @@
+{
+ "lang": "en-us",
+ "index": {
+ "title": "Image Glitch Werkzeug",
+ "description": "Mit diesem Programm kannst du ein Bild so bearbeiten, dass es kaputt aussieht. Ziehe ein Bild in das Browserfenster um loszulegen.\n\nDas Programm verändert einige Bytes in der Bilddatei. Aufgrund der Art und Weise wie der [JPEG Logarithmus](https://de.wikipedia.org/wiki/JPEG) funktioniert, wird trotz eigenlich unlesbarer Rohdaten weiterhin ein Bild erzeugt. Dieses Programm ist angelehnt an ein ähnliches Experiment von [experiment](http://blog.soulwire.co.uk/laboratory/flash/as3-bitmapdata-glitch-generator) von [soulwire](http://github.com/soulwire) in Flash.\n\nDieses Programm wurde von [Georg](http://snorpey.com/) entwickelt. Er freut sich übrigens immer, von den Nutzern seiner Programme zu hören wie sie die Programme einsetzen. Du kannst Georg bei [Twitter](https://twitter.com/snorpey) folgen oder dir den Quellcode dieses Programms bei [GitHub](https://github.com/snorpey/jpg-glitch) ansehen.\n\nFalls dir dieses Programm gefallen hat, gefallen dir vielleicht auch Georgs andere [Experimente](http://snorpey.github.io/experiments/)."
+ },
+ "settings": {
+ "settings": "Einstellungen",
+ "settingstitle": "Ändere die Einstellungen des Programms",
+ "canzoomwithpointer": "Erlaube Zoomen mit Mausrad und Gesten",
+ "resizeuploadedimages": "Große Bilder verkleinen, um Fehler zu vermeiden",
+ "language": "Sprache (Language)",
+ "languageoptions": {
+ "de-de": "Deutsch DE",
+ "en-us": "Englisch (English US)"
+ },
+ "error": {
+ "save": "Konnte die Einstellungen nicht im Browser speichern.",
+ "load": "Konnte die Einstellungen nicht vom Browser laden."
+ }
+ },
+ "about": {
+ "info": "Über",
+ "infotitle": "Weitere Informationen über dieses Programm"
+ },
+ "controls": {
+ "controls": "Regler",
+ "controlstitle": "Regler ein- und ausblenden",
+ "randomize": "Zufall",
+ "randomizetitle": "Zufällige Reglerwerte wählen",
+ "center": "Mittig",
+ "centertitle": "Zentriere das Bild auf dem Bildschirm",
+ "original": "100%",
+ "originaltitle": "Zoome zur Originalgröße des Bildes",
+ "zoom": "Zoom",
+ "amount": "Betrag",
+ "seed": "Startwert",
+ "iterations": "Wiederholungen",
+ "quality": "Qualität",
+ "fullscreen": "Vollbild",
+ "fullscreentitle": "Vollbild ein- und ausschalten"
+ },
+ "nav": {
+ "menu": "Menü",
+ "menutitle": "Hauptmenü ein- und ausschalten"
+ },
+ "file": {
+ "open": "Öffnen",
+ "opentitle": "Eine Datei von deinem Gerät öffnen",
+ "import": "Importieren",
+ "importtitle": "Öffne ein Bild von deiner Festplatte",
+ "recent": "Bilder aus vorherigen Sitzungen",
+ "norecent": "Noch keine Bilder vorhanden",
+ "untitled": "Ohne Titel",
+ "del": "Löschen",
+ "deltitle": "Dieses Bild aus dem Speicher deines Browsers löschen",
+ "openimage": "Öffnen",
+ "openimagetitle": "Dieses Bild laden",
+ "offline": "Offline nehmen",
+ "offlinetitle": "Dieses Bild von Imgur löschen und vom Netz nehmen",
+ "openlink": "Link öffnen",
+ "openlinktitle": "Das Bild in einen neuen Browsertab öffnen",
+ "save": "Speichern",
+ "savetitle": "Aktuelles Bild für spätere Sitzungen speichern",
+ "download": "Herunterladen",
+ "downloadtitle": "Lade das bearbeitete Bild auf herunter",
+ "saveinbrowser": "Speichern",
+ "saveinbrowsertitle": "Speichere das Bild im Speicher deines Browsers",
+ "error": {
+ "load": "Konnte vorherige Sitzung nicht laden.",
+ "save": "Konnte aktuelle Sitzung nicht speichern.",
+ "openimage": "Kann das Bild nicht öffnen.",
+ "openfile": "Kann die Datei nicht öffnen."
+ },
+ "message": {
+ "before": "Das gleiche Bild wurde vorher schonmal gespeichert.",
+ "save": "Die Datei wurde in deinem Browser gespeichert.",
+ "del": "Eine Datei wurde aus dem Speicher deines Browsers gelöscht.",
+ "resize": "Dein Bild wurde etwas verkleinert, um Programmfehler zu vermeiden."
+ }
+ },
+ "share": {
+ "share": "Teilen",
+ "sharetitle": "Teile das Bild im Internet",
+ "info": "Du kannst das Bild auf [Imgur](https://imgur.com) hochladen, um es mit anderen zu teilen. Bitte beachte dass das Bild dann öffentlich einsehbar ist und die [Terms of Service](https://imgur.com/tos) von Imgur gelten.",
+ "openon": "Das Bild auf {$1} öffnen",
+ "openontitle": "Das Bild öffnen, dass auf {$1} geteilt wurde",
+ "shareon": "Auf {$1} teilen",
+ "shareontitle": "Das Bild auf {$1} teilen",
+ "upload": "Bild auf Imgur Hochladen",
+ "uploadtitle": "Lade das Bild auf Imgur hoch, um es mit anderen zu teilen",
+ "uploading": "Lade Bild hoch…",
+ "imagelink": "Online Link zum Bild",
+ "imagelinktitle": "Du kannst diesen Link nehmen um das Bild auf anderen Seiten zu teilen oder hochzuladen",
+ "opennewtabtitle": "Öffne das Bild in einem neuen Browsertab",
+ "imgur": "Imgur",
+ "reddit": "Reddit",
+ "twitter": "Twitter",
+ "facebook": "Facebook",
+ "pinterest": "Pinterest",
+ "recentlyshared": "Kürzlich geteilte Bilder",
+ "untitled": "Ohne Titel",
+ "openlink": "Öffne Link",
+ "openlinktitle": "Das Bild in einen neuen Browsertab öffnen",
+ "offline": "Offline nehmen",
+ "offlinetitle": "Dieses Bild von Imgur löschen und vom Netz nehmen",
+ "error": {
+ "base64": "Es gibt keine Base64-Adresse zum hochladen",
+ "upload": "Konnte das Bild nicht auf Imgur hochladen."
+ },
+ "message": {
+ "upload": "Das Bild wurde zu Imgur hochgeladen. [Bild öffnen.]({$1})",
+ "del": "Das Bild wurde von Imgur gelöscht."
+ }
+ },
+ "webcam": {
+ "webcam": "Webcam",
+ "webcamtitle": "Mache ein Bild mit deiner Webcam",
+ "webcamlabel": "Foto",
+ "trigger": "Ein Foto machen",
+ "triggertitle": "Klicken, um ein Foto mit deiner Webcam zu machen",
+ "picture": "Webcam Foto von {$1}",
+ "error": {
+ "access": "Der Zugriff auf deine Webcam wurde leider verweigert."
+ }
+ },
+ "welcome": {
+ "firstvisit": [
+ "Hallo! Es scheint als wärst du zum ersten Mal hier.",
+ "Um loszulegen, ziehe an den Reglern am unteren Fensterrand um das Bild zu bearbeiten.",
+ "Du kannst auch ein Bild importieren. Klicke dazu einfach im Menü auf ÖFFNEN."
+ ]
+ }
+}
\ No newline at end of file
diff --git a/lang/en-us.json b/lang/en-us.json
new file mode 100644
index 0000000..4c5a99b
--- /dev/null
+++ b/lang/en-us.json
@@ -0,0 +1,135 @@
+{
+ "lang": "en-us",
+ "index": {
+ "title": "Image Glitch Tool",
+ "description": "With this app, you can glitch your own images by dragging an image into the browser window. Use the sliders in the control panel to alter the glitched parameters. The image updates in real time.\n\nThis app corrupts some bytes in an image. Because of the way [JPEG encoding](https://en.wikipedia.org/wiki/JPEG) works, the corrupted file still shows a corrupted image. It was inspired by [soulwire](http://github.com/soulwire)’s [experiment](http://blog.soulwire.co.uk/laboratory/flash/as3-bitmapdata-glitch-generator) in Flash.\n\nThis tool was created by [Georg](http://snorpey.com/). He is always happy to learn about the things that people are creating with his tools. You can follow him on [Twitter](https://twitter.com/snorpey) or explore the source code of this app on [GitHub](https://github.com/snorpey/jpg-glitch).\n\nIf you like this glitch tool, you can check out some of Georg’s other [JavaScript experiments](http://snorpey.github.io/experiments/)."
+ },
+ "settings": {
+ "settings": "Settings",
+ "settingstitle": "Change the tool settings",
+ "canzoomwithpointer": "Enable zooming with mousewheel and touch gestures",
+ "language": "Language (Sprache)",
+ "languageoptions": {
+ "de-de": "German (Deutsch DE)",
+ "en-us": "English US"
+ },
+ "resizeuploadedimages": "Resize big images to avoid errors",
+ "error": {
+ "save": "Could not save settings to your browser.",
+ "load": "Could not load previous settings data from your browser."
+ }
+ },
+ "about": {
+ "info": "Info",
+ "infotitle": "Learn about the author of this tool."
+ },
+ "controls": {
+ "controls": "Controls",
+ "controlstitle": "Show and hide controls",
+ "randomize": "Randomize",
+ "randomizetitle": "Randomize control values",
+ "center": "Center",
+ "centertitle": "Fit image to screen",
+ "original": "100%",
+ "originaltitle": "Zoom image to its original size 100%",
+ "zoom": "Zoom",
+ "amount": "Amount",
+ "seed": "Seed",
+ "iterations": "Iterations",
+ "quality": "Quality",
+ "fullscreen": "Fullscreen",
+ "fullscreentitle": "Toggle Fullscreen"
+ },
+ "nav": {
+ "menu": "Menu",
+ "menutitle": "Toggle the main menu"
+ },
+ "file": {
+ "open": "Open",
+ "opentitle": "Open an image from your device",
+ "import": "Import File",
+ "importtitle": "Import image from your hard drive",
+ "recent": "Open recently edited file",
+ "norecent": "No recently edited files available",
+ "untitled": "Untitled",
+ "del": "delete",
+ "deltitle": "Delete this image from your browser’s storage",
+ "openimage": "Open",
+ "openimagetitle": "Open this image from your browser’s storage",
+ "offline": "Take Offline",
+ "offlinetitle": "Delete this image from Imgur",
+ "openlink": "Open Link",
+ "openlinktitle": "Open image link in new browser tab",
+ "save": "Save",
+ "savetitle": "Save current image for later sessions",
+ "download": "Download Image",
+ "downloadtitle": "Download the glitched image to your device",
+ "saveinbrowser": "Save in Browser",
+ "saveinbrowsertitle": "Save image in your browser’s storage",
+ "error": {
+ "load": "Could not get previous session data from your browser.",
+ "save": "Could not save data to your browser.",
+ "openimage": "Could not open image.",
+ "openfile": "Could not open file."
+ },
+ "message": {
+ "before": "This file has been saved before.",
+ "save": "A file was saved to your browser’s storage.",
+ "del": "A file was deleted from your browser’s storage.",
+ "resize": "Your image was scaled down a bit to work better with this app."
+ }
+ },
+ "share": {
+ "share": "Share",
+ "sharetitle": "Share your glitched image on the internet",
+ "info": "You can upload the image to [Imgur](https://imgur.com) to share it. Please note that after uploading the image will be publicly visible on the internet and the Imgur's [terms of service](https://imgur.com/tos) apply.",
+ "openon": "Open image on {$1}",
+ "openontitle": "Open the image you shared on {$1}",
+ "shareon": "Share image on {$1}",
+ "shareontitle": "Share your glitched image on {$1}",
+ "upload": "Upload Image to Imgur",
+ "uploadtitle": "Upload your glitched image to Imgur and share it with others online",
+ "uploading": "Uploading Image…",
+ "imagelink": "Online link to image",
+ "imagelinktitle": "You can use this link to share the image or embed it on other sites",
+ "opennewtabtitle": "Open the image in a new browser tab",
+ "imgur": "Imgur",
+ "reddit": "Reddit",
+ "twitter": "Twitter",
+ "facebook": "Facebook",
+ "pinterest": "Pinterest",
+ "recentlyshared": "Recently shared images",
+ "untitled": "Untitled",
+ "openlink": "Open Link",
+ "openlinktitle": "Open the online link of this image in a new browser tab",
+ "offline": "Take Offline",
+ "offlinetitle": "Delete this image from Imgur",
+ "error": {
+ "base64": "No base64 URL to upload.",
+ "upload": "Could not upload file to Imgur.",
+ "del": "Could not delete file from Imgur."
+ },
+ "message": {
+ "upload": "The image was successfully uploaded to Imgur. [Click here to open it]({$1})",
+ "del": "The image was successfully deleted from Imgur."
+ }
+ },
+ "webcam": {
+ "webcam": "Webcam",
+ "webcamtitle": "Take a picture with your webcam",
+ "webcamlabel": "Take a Picture",
+ "trigger": "Take Picture",
+ "triggertitle": "Click to take a picture with your webcam",
+ "picture": "'Webcam Picture {$1}",
+ "error": {
+ "access": "Could not access your webcam."
+ }
+ },
+ "welcome": {
+ "firstvisit": [
+ "Welcome! Looks like this is your first time here.",
+ "To get started, you can drag the sliders below and glitch an image.",
+ "You can also import an image from your device. Just click on the OPEN button in the menu."
+ ]
+ }
+}
\ No newline at end of file
diff --git a/manifest.json b/manifest.json
new file mode 100644
index 0000000..18827b7
--- /dev/null
+++ b/manifest.json
@@ -0,0 +1,39 @@
+{
+ "name": "Image Glitch Tool",
+ "short_name": "Glitch Tool",
+ "start_url": "./?utm_source=web_app_manifest",
+ "display": "standalone",
+ "background_color": "white",
+ "orientation": "any",
+ "icons": [ {
+ "src": "images/logos/glitch-48x48.png",
+ "sizes": "48x48",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-72x72.png",
+ "sizes": "72x72",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-96x96.png",
+ "sizes": "96x96",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-144x144.png",
+ "sizes": "144x144",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-168x168.png",
+ "sizes": "168x168",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-192x192.png",
+ "sizes": "192x192",
+ "type": "image/png"
+ }, {
+ "src": "images/logos/glitch-192x192.svg",
+ "sizes": "144x144 168x168 192x192",
+ "density": 2
+ } ],
+ "theme_color": "black",
+ "service_worker": { "src": "serviceworker.js" }
+}
\ No newline at end of file
diff --git a/scripts/config.js b/scripts/config.js
new file mode 100644
index 0000000..a6678b5
--- /dev/null
+++ b/scripts/config.js
@@ -0,0 +1,43 @@
+define(
+ function () {
+ return {
+ keys: {
+ // this is my api key
+ // please get your own if you're hosting this app
+ // on a different server.
+ // https://api.imgur.com/oauth2/addclient
+ // thank you.
+ imgur: 'a4c24380d884932',
+ storage: 'glitch-items'
+ },
+ defaultControlParams: {
+ amount: { min: 0, max: 99, value: 24 },
+ seed: { min: 0, max: 100, value: 53 },
+ iterations: { min: 0, max: 100, value: 21 },
+ quality: { min: 1, max: 99, value: 46 }
+ },
+ defaultImage: {
+ name: 'AbrahamLincoln',
+ path: 'images/lincoln.jpg'
+ },
+ workers: {
+ glitch: 'scripts/workers/glitchworker.js',
+ storage: 'scripts/workers/storageworker.js',
+ settings: 'scripts/workers/settingsworker.js'
+ },
+ language: {
+ dir: 'lang',
+ preset: 'en-us'
+ },
+ settings: {
+ canZoomWithPointer: { value: true },
+ resizeUploadedImages: { value: true },
+ language: { value: 'en-us', options: [ 'en-us', 'de-de' ] }
+ },
+ localForage: {
+ name : 'glitchtool',
+ storeName : 'keyvaluepairs'
+ }
+ };
+ }
+);
\ No newline at end of file
diff --git a/scripts/glitcher.js b/scripts/glitcher.js
new file mode 100644
index 0000000..05b1442
--- /dev/null
+++ b/scripts/glitcher.js
@@ -0,0 +1,331 @@
+/*global require, requirejs, define*/
+// gf: http://requirejs.org/docs/api.html#config
+requirejs.config( {
+ baseUrl: 'scripts/',
+ waitSeconds: 10
+} );
+
+require( [
+ 'config',
+ 'util/browser',
+ 'views/appview',
+ 'views/navview',
+ 'views/controlsview',
+ 'views/openfileview',
+ 'views/fullscreenview',
+ 'views/webcamview',
+ 'views/canvasview',
+ 'views/canvascontrolsview',
+ 'views/saveview',
+ 'views/shareview',
+ 'views/indicatorview',
+ 'views/workspacenavview',
+ 'views/aboutview',
+ 'views/draganddropview',
+ 'views/workspaceview',
+ 'views/welcomeview',
+ 'views/settingsview',
+ 'models/controlsmodel',
+ 'models/imagemodel',
+ 'models/glitchmodel',
+ 'models/storagemodel',
+ 'models/sharemodel',
+ 'models/networkmodel',
+ 'models/settingsmodel',
+ 'models/localisationmodel',
+ 'lib/localforage.nopromises'
+], function (
+ config,
+ browser,
+ AppView,
+ NavView,
+ ControlsView,
+ OpenFileView,
+ FullscreenView,
+ WebCamView,
+ CanvasView,
+ CanvasControlsView,
+ SaveView,
+ ShareView,
+ IndicatorView,
+ WorkspaceNavView,
+ AboutView,
+ DragAndDropView,
+ WorkspaceView,
+ WelcomeView,
+ SettingsView,
+ ControlsModel,
+ ImageModel,
+ GlitchModel,
+ StorageModel,
+ ShareModel,
+ NetworkModel,
+ SettingsModel,
+ LocalisationModel,
+ localforage
+) {
+ var imageModel = ImageModel();
+ var glitchModel = GlitchModel();
+ var shareModel = ShareModel();
+ var storageModel = StorageModel();
+ var controlsModel = ControlsModel( config.defaultControlParams );
+ var networkModel = NetworkModel();
+ var settingsModel = SettingsModel();
+ var localisationModel = LocalisationModel.sharedInstance;
+
+ var appView = AppView( document.body );
+ var navView = NavView( appView.el );
+ var workspaceView = WorkspaceView( appView.el );
+ var workspaceNavView = WorkspaceNavView( workspaceView.el );
+ var canvasControlsView = CanvasControlsView( workspaceNavView.el );
+ var controlsView = ControlsView( workspaceNavView.el, canvasControlsView.el, config.defaultControlParams );
+ var indicatorView = IndicatorView( workspaceView.el );
+ var canvasView = CanvasView( workspaceView.el, navView.el );
+ var openFileView = OpenFileView( navView.el );
+ var saveView = SaveView( navView.el );
+ var webcamView = WebCamView( navView.el );
+ var shareView = ShareView( navView.el );
+ var aboutView = AboutView( navView.el );
+ var settingsView = SettingsView( navView.el );
+ var fullscreenView = FullscreenView( workspaceView.el );
+ var dragAndDropView = DragAndDropView( canvasView.el );
+ var welcomeView = WelcomeView();
+
+ function init () {
+ addCSSClasses();
+ addSubscribers();
+
+ networkModel.checkConnectivity();
+
+ if ( browser.test( 'localforage' ) && localforage ) {
+ setUpLocalForage( function () {
+ storageModel.load();
+ settingsModel.load();
+ } );
+ } else {
+ settingsModel.load();
+ controlsView.loadInitialValues();
+ loadInitialItem();
+ }
+
+ document.documentElement.classList.add( 'is-loaded' );
+
+ setTimeout( function () {
+ document.documentElement.classList.remove( 'is-loading' );
+ }, 10 );
+ }
+
+ // hooks up all messaging between items
+ // using publisher/subscriber model
+ function addSubscribers () {
+ controlsView
+ .on( 'update', controlsModel.setValue )
+ .on( 'random', controlsModel.randomizeValues );
+
+ controlsModel
+ .on( 'update', controlsView.setValue )
+ .on( 'update', glitchModel.setValue )
+ .on( 'update', shareView.hideShareLinks );
+
+ canvasControlsView
+ .on( 'center', canvasView.animateToCenter )
+ .on( 'scale', canvasView.setScale );
+
+ canvasView
+ .on( 'scale', canvasControlsView.setScale )
+ .on( 'dblclick', canvasView.animateToCenter );
+
+ openFileView
+ .on( 'openfile', imageModel.loadFromFile )
+ .on( 'openfromlocalstorage', storageModel.loadItem )
+ .on( 'deletefromlocalstorage', storageModel.removeLocalData )
+ .on( 'deletefromimgur', shareModel.remove );
+
+ saveView
+ .on( 'savetolocalstorage', glitchModel.getImageGenerationFn( saveNewEntry ) )
+ .on( 'savetolocalstorage', updateDownloadLink )
+ .on( 'show', updateDownloadLink );
+
+ navView.on( 'toggleend', canvasView.resized );
+
+ imageModel
+ .on( 'load', glitchModel.setImageData )
+ .on( 'load', openFileView.dialog.hide )
+ .on( 'load', canvasView.animateToCenter )
+ .on( 'load', canvasView.show )
+ .on( 'update', canvasView.hide )
+ .on( 'error', indicatorView.showError )
+ .on( 'error', indicatorView.hideLoading )
+ .on( 'statusmessage', indicatorView.showMessage );
+
+ glitchModel
+ .on( 'glitch', canvasView.putImageData )
+ .on( 'glitch', canvasView.createImageUrl( shareModel.updateUrl ) )
+ .on( 'glitch', updateDownloadLink );
+
+ shareView
+ .on( 'share', shareModel.upload )
+ .on( 'deletefromimgur', shareModel.remove );
+
+ shareModel
+ .on( 'uploadstart', shareView.showUpload )
+ .on( 'uploadend', shareView.hideUpload )
+ .on( 'uploadcomplete', shareView.uploadComplete )
+ .on( 'uploadcomplete', glitchModel.getImageGenerationFn( saveNewEntry ) )
+ .on( 'removecomplete', storageModel.removeImgurData )
+ .on( 'removecomplete', shareView.hideShareLinks )
+ .on( 'error', indicatorView.showError )
+ .on( 'error', shareView.handleError )
+ .on( 'statusmessage', indicatorView.showMessage );
+
+ webcamView
+ .on( 'video', imageModel.loadFromVideo )
+ .on( 'error', indicatorView.showError );
+
+ dragAndDropView
+ .on( 'drop', imageModel.loadFromFile )
+ .on( 'drop', canvasView.hide );
+
+ welcomeView
+ .on( 'message', indicatorView.showWelcome );
+
+ workspaceView
+ .on( 'click', navView.closeSmallScreenNav );
+
+ settingsView
+ .on( 'settingchange', settingsModel.setValue );
+
+ storageModel
+ .on( 'update', openFileView.updateList )
+ .on( 'update', shareView.updateList )
+ .on( 'update', loadInitialItem )
+ .on( 'loaditem', loadEntry )
+ .on( 'statusmessage', indicatorView.showMessage )
+ .on( 'error', indicatorView.showError )
+ .on( 'firstvisit', welcomeView.show );
+
+ networkModel
+ .on( 'connect', shareView.showOnlineOptions )
+ .on( 'disconnect', shareView.hideOnlineOptions )
+ .on( 'connect', appView.showOnlineOptions )
+ .on( 'disconnect', appView.hideOnlineOptions );
+
+ settingsModel
+ .on( 'update', canvasView.panZoom.settingUpdated )
+ .on( 'update', imageModel.settingUpdated )
+ .on( 'update', settingsView.settingUpdated )
+ .on( 'update', localisationModel.settingUpdated )
+ .on( 'error', indicatorView.showError );
+
+ localisationModel
+ .on( 'error', indicatorView.showError );
+ }
+
+ function addCSSClasses () {
+ if ( browser.test( 'touch' ) ) {
+ document.documentElement.classList.add( 'has-touch' );
+ }
+ }
+
+ function setUpLocalForage ( callback ) {
+ if ( localforage ) {
+ localforage.config( config.localForage );
+
+ var driver = [
+ localforage.WEBSQL,
+ localforage.INDEXEDDB,
+ localforage.LOCALSTORAGE
+ ];
+
+ if ( browser.test( 'safari' ) ) {
+ driver = localforage.LOCALSTORAGE;
+ }
+
+ localforage
+ .setDriver( driver )
+ .then( callback );
+ } else {
+ if ( typeof callback === 'function' ) {
+ callback();
+ }
+ }
+ }
+
+ function loadInitialItem ( entries ) {
+ var lastItemUID = -1;
+
+ // load last saved item if possible,
+ // otherwise, load default image
+ if ( entries && Object.keys( entries ).length ) {
+ var now = Date.now();
+ var delta = Infinity;
+
+ for ( var uid in entries ) {
+ if ( entries[uid].timestamp ) {
+ if ( now - entries[uid].timestamp < delta ) {
+ delta = now - entries[uid].timestamp;
+ lastItemUID = uid;
+ }
+ }
+ }
+ }
+
+ storageModel.off( 'update', loadInitialItem );
+
+ if ( lastItemUID !== -1 ) {
+ storageModel.loadItem( lastItemUID );
+ } else {
+ imageModel.loadFromURL( config.defaultImage.path, config.defaultImage.name );
+ controlsView.loadInitialValues();
+ }
+ }
+
+ // saves new item to local storage
+ function saveNewEntry ( thumbnail, publicURL, imgurID, deleteHash ) {
+ var item = {
+ thumbnail: thumbnail,
+ src: imageModel.getLastImageSRC(),
+ values: controlsModel.getValues(),
+ name: imageModel.getLastFileName()
+ };
+
+ if ( publicURL ) {
+ item.publicUrl = publicURL;
+ }
+
+ if ( imgurID ) {
+ item.imgurID = imgurID;
+ }
+
+ if ( deleteHash ) {
+ item.deleteHash = deleteHash;
+ }
+
+ storageModel.add( item );
+ }
+
+ // loads item from localstorage
+ function loadEntry ( uid, entry ) {
+ for ( var key in entry.values ) {
+ controlsModel.setValue( key, entry.values[key] );
+ }
+
+ imageModel.loadFromURL( entry.src, entry.name );
+ }
+
+ // updates the download link url every time the controls were updated.
+ // add a delay of 200 to we don't update the DOM too often
+ var downloadLinkTimeoutId = NaN;
+
+ function updateDownloadLink () {
+ if ( saveView.getActive() ) {
+ clearTimeout( downloadLinkTimeoutId );
+
+ downloadLinkTimeoutId = setTimeout( function () {
+ glitchModel.getImageGenerationFn( saveView.updateDownloadLink, 'original' )( imageModel.getLastFileName() )
+ }, 200 );
+ }
+ }
+
+ init();
+} );
\ No newline at end of file
diff --git a/scripts/lib/almond-0.2.6.js b/scripts/lib/almond.js
similarity index 87%
rename from scripts/lib/almond-0.2.6.js
rename to scripts/lib/almond.js
index 807815c..2a06588 100644
--- a/scripts/lib/almond-0.2.6.js
+++ b/scripts/lib/almond.js
@@ -1,5 +1,5 @@
/**
- * almond 0.2.6 Copyright (c) 2011-2012, The Dojo Foundation All Rights Reserved.
+ * @license almond 0.3.1 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
@@ -16,7 +16,8 @@ var requirejs, require, define;
config = {},
defining = {},
hasOwn = Object.prototype.hasOwnProperty,
- aps = [].slice;
+ aps = [].slice,
+ jsSuffixRegExp = /\.js$/;
function hasProp(obj, prop) {
return hasOwn.call(obj, prop);
@@ -31,7 +32,7 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName) {
- var nameParts, nameSegment, mapValue, foundMap,
+ var nameParts, nameSegment, mapValue, foundMap, lastIndex,
foundI, foundStarMap, starI, i, j, part,
baseParts = baseName && baseName.split("/"),
map = config.map,
@@ -43,14 +44,19 @@ var requirejs, require, define;
//otherwise, assume it is a top-level require that will
//be relative to baseUrl in the end.
if (baseName) {
- //Convert baseName to array, and lop off the last part,
- //so that . matches that "directory" and not name of the baseName's
- //module. For instance, baseName of "one/two/three", maps to
- //"one/two/three.js", but we want the directory, "one/two" for
- //this normalization.
- baseParts = baseParts.slice(0, baseParts.length - 1);
+ name = name.split('/');
+ lastIndex = name.length - 1;
- name = baseParts.concat(name.split("/"));
+ // Node .js allowance:
+ if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
+ name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
+ }
+
+ //Lop off the last part of baseParts, so that . matches the
+ //"directory" and not name of the baseName's module. For instance,
+ //baseName of "one/two/three", maps to "one/two/three.js", but we
+ //want the directory, "one/two" for this normalization.
+ name = baseParts.slice(0, baseParts.length - 1).concat(name);
//start trimDots
for (i = 0; i < name.length; i += 1) {
@@ -142,7 +148,15 @@ var requirejs, require, define;
//A version of a require function that passes a moduleName
//value for items that may need to
//look up paths relative to the moduleName
- return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
+ var args = aps.call(arguments, 0);
+
+ //If first arg is not require('string'), and there is only
+ //one arg, it is the array form without a callback. Insert
+ //a null so that the following concat is correct.
+ if (typeof args[0] !== 'string' && args.length === 1) {
+ args.push(null);
+ }
+ return req.apply(undef, args.concat([relName, forceSync]));
};
}
@@ -259,14 +273,14 @@ var requirejs, require, define;
main = function (name, deps, callback, relName) {
var cjsModule, depName, ret, map, i,
args = [],
+ callbackType = typeof callback,
usingExports;
//Use name if no relName
relName = relName || name;
//Call the callback to define the module, if necessary.
- if (typeof callback === 'function') {
-
+ if (callbackType === 'undefined' || callbackType === 'function') {
//Pull out the defined dependencies and pass the ordered
//values to the callback.
//Default to [require, exports, module] if no deps
@@ -297,7 +311,7 @@ var requirejs, require, define;
}
}
- ret = callback.apply(defined[name], args);
+ ret = callback ? callback.apply(defined[name], args) : undefined;
if (name) {
//If setting exports via "module" is in play,
@@ -332,6 +346,13 @@ var requirejs, require, define;
} else if (!deps.splice) {
//deps is a config object, not an array.
config = deps;
+ if (config.deps) {
+ req(config.deps, config.callback);
+ }
+ if (!callback) {
+ return;
+ }
+
if (callback.splice) {
//callback is an array, which means it is a dependency list.
//Adjust args if there are dependencies
@@ -376,11 +397,7 @@ var requirejs, require, define;
* the config return value is used.
*/
req.config = function (cfg) {
- config = cfg;
- if (config.deps) {
- req(config.deps, config.callback);
- }
- return req;
+ return req(cfg);
};
/**
@@ -389,6 +406,9 @@ var requirejs, require, define;
requirejs._defined = defined;
define = function (name, deps, callback) {
+ if (typeof name !== 'string') {
+ throw new Error('See almond README: incorrect module build, no module name');
+ }
//This module may not have dependencies
if (!deps.splice) {
diff --git a/scripts/lib/getusermedia.js b/scripts/lib/getusermedia.js
deleted file mode 100644
index f3c7780..0000000
--- a/scripts/lib/getusermedia.js
+++ /dev/null
@@ -1,10 +0,0 @@
-;(function ()
-{
- navigator.getUserMedia = (
- navigator.getUserMedia ||
- navigator.webkitGetUserMedia ||
- navigator.mozGetUserMedia ||
- navigator.msGetUserMedia ||
- false
- );
-})()
\ No newline at end of file
diff --git a/scripts/lib/glitch-canvas-with-worker.js b/scripts/lib/glitch-canvas-with-worker.js
new file mode 100644
index 0000000..f75581c
--- /dev/null
+++ b/scripts/lib/glitch-canvas-with-worker.js
@@ -0,0 +1,292 @@
+//! glitch-canvas by snorpey, MIT License
+(function(window, factory) {
+ if (typeof define === "function" && define.amd) {
+ define(factory);
+ } else if (typeof exports === "object") {
+ module.exports = factory();
+ } else {
+ window.glitch = factory();
+ }
+})(this, function() {
+
+ function Glitcher ( webworkerPath ) {
+ if ( ! ( this instanceof Glitcher ) ) {
+ return new Glitcher( webworkerPath );
+ }
+
+ var self = this
+ var canvas1El = document.createElement( 'canvas' );
+ var canvas2El = document.createElement( 'canvas' );
+ var ctx1 = canvas1El.getContext( '2d' );
+ var ctx2 = canvas2El.getContext( '2d' );
+ var img = new Image();
+ var base64;
+ var img;
+ var newImageData;
+ var i;
+ var len;
+ var params;
+ var worker;
+ var base64Chars;
+ var base64Map;
+ var reversedBase64Map;
+ var byteArrayImageData;
+ var jpgHeaderLength;
+
+ function init () {
+ if ( webworkerPath && 'Worker' in window ) {
+ worker = new Worker( webworkerPath );
+ worker.addEventListener( 'message', workerResponded, false );
+ } else {
+ base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+ base64Map = base64Chars.split( '' );
+ reversedBase64Map = { };
+
+ base64Map.forEach( function ( val, key ) {
+ reversedBase64Map[val] = key;
+ } );
+ }
+
+ return self;
+ }
+
+ function glitch ( imageData, parameters, callback ) {
+ if (
+ isValidImageData( imageData ) &&
+ isType( parameters, 'parameters', 'object' ) &&
+ isType( callback, 'callback', 'function' )
+ ) {
+ resizeCanvas( canvas1El, imageData );
+ resizeCanvas( canvas2El, imageData );
+ params = getNormalizedParameters( parameters );
+ base64 = getBase64FromImageData( imageData, params.quality );
+
+ img.onload = function () {
+ ctx1.drawImage( img, 0, 0 );
+ newImageData = ctx1.getImageData( 0, 0, imageData.width, imageData.height );
+ callback ( newImageData );
+ img.onload = null;
+ };
+
+ if ( worker ) {
+ worker.postMessage( { glitch: { base64: base64, parameters: params } } );
+ } else {
+ byteArrayImageData = base64ToByteArray( base64 );
+ jpgHeaderLength = getJpegHeaderSize( byteArrayImageData );
+
+ for ( i = 0, len = params.iterations; i < len; i++ ) {
+ glitchJpegBytes( byteArrayImageData, jpgHeaderLength, params.seed, params.amount, i, params.iterations );
+ }
+
+ img.src = byteArrayToBase64( byteArrayImageData );
+ }
+ }
+
+ return self;
+ }
+
+ function workerResponded ( event ) {
+ if ( event && event.data && event.data.glitched ) {
+ glitched( event.data.glitched );
+ } else {
+ window.console && console.log( 'glitchworker response', event.data );
+ }
+ }
+
+ function workerFailed ( err ) {
+ window.console && console.log( err.message || err );
+ }
+
+ function glitched ( newBase64ImageData ) {
+ img.src = newBase64ImageData;
+ }
+
+ function resizeCanvas ( canvas, size ) {
+ if ( canvas.width !== size.width ) {
+ canvas.width = size.width;
+ }
+
+ if ( canvas.height !== size.height ) {
+ canvas.height = size.height;
+ }
+ }
+
+ function getNormalizedParameters ( parameters ) {
+ return {
+ seed: ( parameters.seed || 0 ) / 100,
+ quality: ( parameters.quality || 0 ) / 100,
+ amount: ( parameters.amount || 0 ) / 100,
+ iterations: parameters.iterations || 0
+ };
+ }
+
+ function getBase64FromImageData ( imageData, quality ) {
+ var q = typeof quality === 'number' && quality < 1 && quality > 0 ? quality : .1;
+
+ ctx2.putImageData( imageData, 0, 0 );
+
+ var base64 = canvas2El.toDataURL( 'image/jpeg', q );
+
+ switch ( base64.length % 4 ) {
+ case 3:
+ base64 += '=';
+ break;
+
+ case 2:
+ base64 += '==';
+ break;
+
+ case 1:
+ base64 += '===';
+ break;
+ }
+
+ return base64;
+ }
+
+ function isValidImageData ( imageData ) {
+ if (
+ isType( imageData, 'imageData', 'object' ) &&
+ isType( imageData.width, 'imageData.width', 'number' ) &&
+ isType( imageData.height, 'imageData.height', 'number' ) &&
+ isType( imageData.data, 'imageData.data', 'object' ) &&
+ isType( imageData.data.length, 'imageData.data.length', 'number' ) &&
+ checkNumber( imageData.data.length, 'imageData.data.length', isPositive, '> 0' )
+ ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function isType ( it, name, expectedType ) {
+ if ( typeof it === expectedType ) {
+ return true;
+ } else {
+ error( it, 'typeof ' + name, '"' + expectedType + '"', '"' + typeof it + '"' );
+ return false;
+ }
+ }
+
+ function checkNumber ( it, name, condition, conditionName ) {
+ if ( condition( it ) === true) {
+ return true;
+ } else {
+ error( it, name, conditionName, 'not' );
+ }
+ }
+
+ function isPositive ( nr ) {
+ return nr > 0;
+ }
+
+ function error ( it, name, expected, result ) {
+ throw new Error( 'glitch(): Expected ' + name + ' to be ' + expected + ', but it was ' + result + '.' );
+ }
+
+ // all functions beyond this point are only used if no webworker is available
+
+ function glitchJpegBytes ( byteArray, jpgHeaderLength, seed, amount, i, len ) {
+ var maxIndex = byteArray.length - jpgHeaderLength - 4;
+ var pxMin = parseInt( maxIndex / len * i, 10 );
+ var pxMax = parseInt( maxIndex / len * ( i + 1 ), 10 );
+ var delta = pxMax - pxMin;
+ var pxIndex = parseInt( pxMin + delta * seed, 10 );
+
+ if ( pxIndex > maxIndex ) {
+ pxIndex = maxIndex;
+ }
+
+ var index = Math.floor( jpgHeaderLength + pxIndex );
+
+ byteArray[index] = Math.floor( amount * 256 );
+ }
+
+ function base64ToByteArray ( str ) {
+ var result = [ ];
+ var digitNum;
+ var cur;
+ var prev;
+
+ for ( i = 23, len = str.length; i < len; i++ ) {
+ cur = reversedBase64Map[str.charAt( i )];
+ digitNum = ( i - 23 ) % 4;
+
+ switch ( digitNum ) {
+ case 1:
+ result.push( prev << 2 | cur >> 4 );
+ break;
+
+ case 2:
+ result.push( ( prev & 15 ) << 4 | cur >> 2 );
+ break;
+
+ case 3:
+ result.push( ( prev & 3 ) << 6 | cur );
+ break;
+ }
+
+ prev = cur;
+ }
+
+ return result;
+ }
+
+ function byteArrayToBase64 ( arr ) {
+ var result = [ 'data:image/jpeg;base64,' ];
+ var byteNum;
+ var cur;
+ var prev;
+
+ for ( i = 0, len = arr.length; i < len; i++ ) {
+ cur = arr[i];
+ byteNum = i % 3;
+
+ switch (byteNum) {
+ case 0:
+ result.push( base64Map[cur >> 2] );
+ break;
+
+ case 1:
+ result.push( base64Map[( prev & 3 ) << 4 | cur >> 4] );
+ break;
+
+ case 2:
+ result.push( base64Map[( prev & 15 ) << 2 | cur >> 6] );
+ result.push( base64Map[cur & 63] );
+ break;
+ }
+
+ prev = cur;
+ }
+
+ if ( byteNum === 0 ) {
+ result.push( base64Map[( prev & 3 ) << 4] );
+ result.push( '==' );
+ } else if ( byteNum === 1 ) {
+ result.push( base64Map[( prev & 15 ) << 2] );
+ result.push( '=' );
+ }
+
+ return result.join( '' );
+ }
+
+ function getJpegHeaderSize ( data ) {
+ var result = 417;
+
+ for ( i = 0, len = data.length; i < len; i++ ) {
+ if ( data[i] === 255 && data[i + 1] === 218 ) {
+ result = i + 2;
+ break;
+ }
+ }
+
+ return result;
+ }
+
+ self.glitch = glitch;
+ self.init = init;
+ }
+
+ return Glitcher;
+});
\ No newline at end of file
diff --git a/scripts/lib/glitch-canvas.js b/scripts/lib/glitch-canvas.js
deleted file mode 100644
index 949493c..0000000
--- a/scripts/lib/glitch-canvas.js
+++ /dev/null
@@ -1,198 +0,0 @@
-//! glitch-canvas by snorpey, MIT License
-(function(window, factory) {
- if (typeof define === "function" && define.amd) {
- define(factory);
- } else if (typeof exports === "object") {
- module.exports = factory();
- } else {
- window.glitch = factory();
- }
-})(this, function() {
- var canvas_1 = document.createElement("canvas");
- var canvas_2 = document.createElement("canvas");
- var ctx_1 = canvas_1.getContext("2d");
- var ctx_2 = canvas_2.getContext("2d");
- var base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- var base64_map = base64_chars.split("");
- var reversed_base64_map = {};
- var params;
- var base64;
- var byte_array;
- var jpg_header_length;
- var img;
- var new_image_data;
- var i;
- var len;
- base64_map.forEach(function(val, key) {
- reversed_base64_map[val] = key;
- });
- function glitchImageData(image_data, parameters, callback) {
- if (isValidImageData(image_data) && checkType(parameters, "parameters", "object") && checkType(callback, "callback", "function")) {
- params = getNormalizedParameters(parameters);
- resizeCanvas(canvas_1, image_data);
- resizeCanvas(canvas_2, image_data);
- base64 = getBase64FromImageData(image_data, params.quality);
- byte_array = base64ToByteArray(base64);
- jpg_header_length = getJpegHeaderSize(byte_array);
- for (i = 0, len = params.iterations; i < len; i++) {
- glitchJpegBytes(byte_array, jpg_header_length, params.seed, params.amount, i, params.iterations);
- }
- img = new Image();
- img.onload = function() {
- ctx_1.drawImage(img, 0, 0);
- new_image_data = ctx_1.getImageData(0, 0, image_data.width, image_data.height);
- callback(new_image_data);
- };
- img.src = byteArrayToBase64(byte_array);
- }
- }
- function resizeCanvas(canvas, size) {
- if (canvas.width !== size.width) {
- canvas.width = size.width;
- }
- if (canvas.height !== size.height) {
- canvas.height = size.height;
- }
- }
- function glitchJpegBytes(byte_array, jpg_header_length, seed, amount, i, len) {
- var max_index = byte_array.length - jpg_header_length - 4;
- var px_min = parseInt(max_index / len * i, 10);
- var px_max = parseInt(max_index / len * (i + 1), 10);
- var delta = px_max - px_min;
- var px_i = parseInt(px_min + delta * seed, 10);
- if (px_i > max_index) {
- px_i = max_index;
- }
- var index = Math.floor(jpg_header_length + px_i);
- byte_array[index] = Math.floor(amount * 256);
- }
- function getBase64FromImageData(image_data, quality) {
- var q = typeof quality === "number" && quality < 1 && quality > 0 ? quality : .1;
- ctx_2.putImageData(image_data, 0, 0);
- var base64 = canvas_2.toDataURL("image/jpeg", q);
- switch (base64.length % 4) {
- case 3:
- base64 += "=";
- break;
-
- case 2:
- base64 += "==";
- break;
-
- case 1:
- base64 += "===";
- break;
- }
- return base64;
- }
- function getJpegHeaderSize(data) {
- var result = 417;
- for (i = 0, len = data.length; i < len; i++) {
- if (data[i] === 255 && data[i + 1] === 218) {
- result = i + 2;
- break;
- }
- }
- return result;
- }
- function base64ToByteArray(str) {
- var result = [];
- var digit_num;
- var cur;
- var prev;
- for (i = 23, len = str.length; i < len; i++) {
- cur = reversed_base64_map[str.charAt(i)];
- digit_num = (i - 23) % 4;
- switch (digit_num) {
- case 1:
- result.push(prev << 2 | cur >> 4);
- break;
-
- case 2:
- result.push((prev & 15) << 4 | cur >> 2);
- break;
-
- case 3:
- result.push((prev & 3) << 6 | cur);
- break;
- }
- prev = cur;
- }
- return result;
- }
- function byteArrayToBase64(arr) {
- var result = [ "data:image/jpeg;base64," ];
- var byte_num;
- var cur;
- var prev;
- for (i = 0, len = arr.length; i < len; i++) {
- cur = arr[i];
- byte_num = i % 3;
- switch (byte_num) {
- case 0:
- result.push(base64_map[cur >> 2]);
- break;
-
- case 1:
- result.push(base64_map[(prev & 3) << 4 | cur >> 4]);
- break;
-
- case 2:
- result.push(base64_map[(prev & 15) << 2 | cur >> 6]);
- result.push(base64_map[cur & 63]);
- break;
- }
- prev = cur;
- }
- if (byte_num === 0) {
- result.push(base64_map[(prev & 3) << 4]);
- result.push("==");
- } else if (byte_num === 1) {
- result.push(base64_map[(prev & 15) << 2]);
- result.push("=");
- }
- return result.join("");
- }
- function getImageDataCopy(image_data) {
- var copy = ctx_2.createImageData(image_data.width, image_data.height);
- copy.data.set(image_data.data);
- return copy;
- }
- function getNormalizedParameters(parameters) {
- return {
- seed: (parameters.seed || 0) / 100,
- quality: (parameters.quality || 0) / 100,
- amount: (parameters.amount || 0) / 100,
- iterations: parameters.iterations || 0
- };
- }
- function isValidImageData(image_data) {
- if (checkType(image_data, "image_data", "object") && checkType(image_data.width, "image_data.width", "number") && checkType(image_data.height, "image_data.height", "number") && checkType(image_data.data, "image_data.data", "object") && checkType(image_data.data.length, "image_data.data.length", "number") && checkNumber(image_data.data.length, "image_data.data.length", isPositive, "> 0")) {
- return true;
- } else {
- return false;
- }
- }
- function checkType(it, name, expected_type) {
- if (typeof it === expected_type) {
- return true;
- } else {
- error(it, "typeof " + name, '"' + expected_type + '"', '"' + typeof it + '"');
- return false;
- }
- }
- function checkNumber(it, name, condition, condition_name) {
- if (condition(it) === true) {
- return true;
- } else {
- error(it, name, condition_name, "not");
- }
- }
- function isPositive(nr) {
- return nr > 0;
- }
- function error(it, name, expected, result) {
- throw new Error("glitch(): Expected " + name + " to be " + expected + ", but it was " + result + ".");
- }
- return glitchImageData;
-});
\ No newline at end of file
diff --git a/scripts/lib/localforage.nopromises.js b/scripts/lib/localforage.nopromises.js
new file mode 100644
index 0000000..1445c28
--- /dev/null
+++ b/scripts/lib/localforage.nopromises.js
@@ -0,0 +1,2103 @@
+if ( 'Promise' in ( typeof window !== 'undefined' ? window : self ) ) {
+/*!
+ localForage -- Offline Storage, Improved
+ Version 1.3.1
+ https://mozilla.github.io/localForage
+ (c) 2013-2015 Mozilla, Apache License 2.0
+*/
+(function webpackUniversalModuleDefinition(root, factory) {
+ if(typeof exports === 'object' && typeof module === 'object')
+ module.exports = factory();
+ else if(typeof define === 'function' && define.amd)
+ define([], factory);
+ else if(typeof exports === 'object')
+ exports["localforage"] = factory();
+ else
+ root["localforage"] = factory();
+})(this, function() {
+return /******/ (function(modules) { // webpackBootstrap
+/******/ // The module cache
+/******/ var installedModules = {};
+
+/******/ // The require function
+/******/ function __webpack_require__(moduleId) {
+
+/******/ // Check if module is in cache
+/******/ if(installedModules[moduleId])
+/******/ return installedModules[moduleId].exports;
+
+/******/ // Create a new module (and put it into the cache)
+/******/ var module = installedModules[moduleId] = {
+/******/ exports: {},
+/******/ id: moduleId,
+/******/ loaded: false
+/******/ };
+
+/******/ // Execute the module function
+/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
+
+/******/ // Flag the module as loaded
+/******/ module.loaded = true;
+
+/******/ // Return the exports of the module
+/******/ return module.exports;
+/******/ }
+
+
+/******/ // expose the modules object (__webpack_modules__)
+/******/ __webpack_require__.m = modules;
+
+/******/ // expose the module cache
+/******/ __webpack_require__.c = installedModules;
+
+/******/ // __webpack_public_path__
+/******/ __webpack_require__.p = "";
+
+/******/ // Load entry module and return exports
+/******/ return __webpack_require__(0);
+/******/ })
+/************************************************************************/
+/******/ ([
+/* 0 */
+/***/ function(module, exports, __webpack_require__) {
+
+ 'use strict';
+
+ exports.__esModule = true;
+
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
+
+ (function () {
+ 'use strict';
+
+ // Custom drivers are stored here when `defineDriver()` is called.
+ // They are shared across all instances of localForage.
+ var CustomDrivers = {};
+
+ var DriverType = {
+ INDEXEDDB: 'asyncStorage',
+ LOCALSTORAGE: 'localStorageWrapper',
+ WEBSQL: 'webSQLStorage'
+ };
+
+ var DefaultDriverOrder = [DriverType.INDEXEDDB, DriverType.WEBSQL, DriverType.LOCALSTORAGE];
+
+ var LibraryMethods = ['clear', 'getItem', 'iterate', 'key', 'keys', 'length', 'removeItem', 'setItem'];
+
+ var DefaultConfig = {
+ description: '',
+ driver: DefaultDriverOrder.slice(),
+ name: 'localforage',
+ // Default DB size is _JUST UNDER_ 5MB, as it's the highest size
+ // we can use without a prompt.
+ size: 4980736,
+ storeName: 'keyvaluepairs',
+ version: 1.0
+ };
+
+ // Check to see if IndexedDB is available and if it is the latest
+ // implementation; it's our preferred backend library. We use "_spec_test"
+ // as the name of the database because it's not the one we'll operate on,
+ // but it's useful to make sure its using the right spec.
+ // See: https://github.com/mozilla/localForage/issues/128
+ var driverSupport = (function (self) {
+ // Initialize IndexedDB; fall back to vendor-prefixed versions
+ // if needed.
+ var indexedDB = indexedDB || self.indexedDB || self.webkitIndexedDB || self.mozIndexedDB || self.OIndexedDB || self.msIndexedDB;
+
+ var result = {};
+
+ result[DriverType.WEBSQL] = !!self.openDatabase;
+ result[DriverType.INDEXEDDB] = !!(function () {
+ // We mimic PouchDB here; just UA test for Safari (which, as of
+ // iOS 8/Yosemite, doesn't properly support IndexedDB).
+ // IndexedDB support is broken and different from Blink's.
+ // This is faster than the test case (and it's sync), so we just
+ // do this. *SIGH*
+ // http://bl.ocks.org/nolanlawson/raw/c83e9039edf2278047e9/
+ //
+ // We test for openDatabase because IE Mobile identifies itself
+ // as Safari. Oh the lulz...
+ if (typeof self.openDatabase !== 'undefined' && self.navigator && self.navigator.userAgent && /Safari/.test(self.navigator.userAgent) && !/Chrome/.test(self.navigator.userAgent)) {
+ return false;
+ }
+ try {
+ return indexedDB && typeof indexedDB.open === 'function' &&
+ // Some Samsung/HTC Android 4.0-4.3 devices
+ // have older IndexedDB specs; if this isn't available
+ // their IndexedDB is too old for us to use.
+ // (Replaces the onupgradeneeded test.)
+ typeof self.IDBKeyRange !== 'undefined';
+ } catch (e) {
+ return false;
+ }
+ })();
+
+ result[DriverType.LOCALSTORAGE] = !!(function () {
+ try {
+ return self.localStorage && 'setItem' in self.localStorage && self.localStorage.setItem;
+ } catch (e) {
+ return false;
+ }
+ })();
+
+ return result;
+ })(this);
+
+ var isArray = Array.isArray || function (arg) {
+ return Object.prototype.toString.call(arg) === '[object Array]';
+ };
+
+ function callWhenReady(localForageInstance, libraryMethod) {
+ localForageInstance[libraryMethod] = function () {
+ var _args = arguments;
+ return localForageInstance.ready().then(function () {
+ return localForageInstance[libraryMethod].apply(localForageInstance, _args);
+ });
+ };
+ }
+
+ function extend() {
+ for (var i = 1; i < arguments.length; i++) {
+ var arg = arguments[i];
+
+ if (arg) {
+ for (var key in arg) {
+ if (arg.hasOwnProperty(key)) {
+ if (isArray(arg[key])) {
+ arguments[0][key] = arg[key].slice();
+ } else {
+ arguments[0][key] = arg[key];
+ }
+ }
+ }
+ }
+ }
+
+ return arguments[0];
+ }
+
+ function isLibraryDriver(driverName) {
+ for (var driver in DriverType) {
+ if (DriverType.hasOwnProperty(driver) && DriverType[driver] === driverName) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ var LocalForage = (function () {
+ function LocalForage(options) {
+ _classCallCheck(this, LocalForage);
+
+ this.INDEXEDDB = DriverType.INDEXEDDB;
+ this.LOCALSTORAGE = DriverType.LOCALSTORAGE;
+ this.WEBSQL = DriverType.WEBSQL;
+
+ this._defaultConfig = extend({}, DefaultConfig);
+ this._config = extend({}, this._defaultConfig, options);
+ this._driverSet = null;
+ this._initDriver = null;
+ this._ready = false;
+ this._dbInfo = null;
+
+ this._wrapLibraryMethodsWithReady();
+ this.setDriver(this._config.driver);
+ }
+
+ // The actual localForage object that we expose as a module or via a
+ // global. It's extended by pulling in one of our other libraries.
+
+ // Set any config values for localForage; can be called anytime before
+ // the first API call (e.g. `getItem`, `setItem`).
+ // We loop through options so we don't overwrite existing config
+ // values.
+
+ LocalForage.prototype.config = function config(options) {
+ // If the options argument is an object, we use it to set values.
+ // Otherwise, we return either a specified config value or all
+ // config values.
+ if (typeof options === 'object') {
+ // If localforage is ready and fully initialized, we can't set
+ // any new configuration values. Instead, we return an error.
+ if (this._ready) {
+ return new Error("Can't call config() after localforage " + 'has been used.');
+ }
+
+ for (var i in options) {
+ if (i === 'storeName') {
+ options[i] = options[i].replace(/\W/g, '_');
+ }
+
+ this._config[i] = options[i];
+ }
+
+ // after all config options are set and
+ // the driver option is used, try setting it
+ if ('driver' in options && options.driver) {
+ this.setDriver(this._config.driver);
+ }
+
+ return true;
+ } else if (typeof options === 'string') {
+ return this._config[options];
+ } else {
+ return this._config;
+ }
+ };
+
+ // Used to define a custom driver, shared across all instances of
+ // localForage.
+
+ LocalForage.prototype.defineDriver = function defineDriver(driverObject, callback, errorCallback) {
+ var promise = new Promise(function (resolve, reject) {
+ try {
+ var driverName = driverObject._driver;
+ var complianceError = new Error('Custom driver not compliant; see ' + 'https://mozilla.github.io/localForage/#definedriver');
+ var namingError = new Error('Custom driver name already in use: ' + driverObject._driver);
+
+ // A driver name should be defined and not overlap with the
+ // library-defined, default drivers.
+ if (!driverObject._driver) {
+ reject(complianceError);
+ return;
+ }
+ if (isLibraryDriver(driverObject._driver)) {
+ reject(namingError);
+ return;
+ }
+
+ var customDriverMethods = LibraryMethods.concat('_initStorage');
+ for (var i = 0; i < customDriverMethods.length; i++) {
+ var customDriverMethod = customDriverMethods[i];
+ if (!customDriverMethod || !driverObject[customDriverMethod] || typeof driverObject[customDriverMethod] !== 'function') {
+ reject(complianceError);
+ return;
+ }
+ }
+
+ var supportPromise = Promise.resolve(true);
+ if ('_support' in driverObject) {
+ if (driverObject._support && typeof driverObject._support === 'function') {
+ supportPromise = driverObject._support();
+ } else {
+ supportPromise = Promise.resolve(!!driverObject._support);
+ }
+ }
+
+ supportPromise.then(function (supportResult) {
+ driverSupport[driverName] = supportResult;
+ CustomDrivers[driverName] = driverObject;
+ resolve();
+ }, reject);
+ } catch (e) {
+ reject(e);
+ }
+ });
+
+ promise.then(callback, errorCallback);
+ return promise;
+ };
+
+ LocalForage.prototype.driver = function driver() {
+ return this._driver || null;
+ };
+
+ LocalForage.prototype.getDriver = function getDriver(driverName, callback, errorCallback) {
+ var self = this;
+ var getDriverPromise = (function () {
+ if (isLibraryDriver(driverName)) {
+ switch (driverName) {
+ case self.INDEXEDDB:
+ return new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(1));
+ });
+ case self.LOCALSTORAGE:
+ return new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(2));
+ });
+ case self.WEBSQL:
+ return new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(4));
+ });
+ }
+ } else if (CustomDrivers[driverName]) {
+ return Promise.resolve(CustomDrivers[driverName]);
+ }
+
+ return Promise.reject(new Error('Driver not found.'));
+ })();
+
+ getDriverPromise.then(callback, errorCallback);
+ return getDriverPromise;
+ };
+
+ LocalForage.prototype.getSerializer = function getSerializer(callback) {
+ var serializerPromise = new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(3));
+ });
+ if (callback && typeof callback === 'function') {
+ serializerPromise.then(function (result) {
+ callback(result);
+ });
+ }
+ return serializerPromise;
+ };
+
+ LocalForage.prototype.ready = function ready(callback) {
+ var self = this;
+
+ var promise = self._driverSet.then(function () {
+ if (self._ready === null) {
+ self._ready = self._initDriver();
+ }
+
+ return self._ready;
+ });
+
+ promise.then(callback, callback);
+ return promise;
+ };
+
+ LocalForage.prototype.setDriver = function setDriver(drivers, callback, errorCallback) {
+ var self = this;
+
+ if (!isArray(drivers)) {
+ drivers = [drivers];
+ }
+
+ var supportedDrivers = this._getSupportedDrivers(drivers);
+
+ function setDriverToConfig() {
+ self._config.driver = self.driver();
+ }
+
+ function initDriver(supportedDrivers) {
+ return function () {
+ var currentDriverIndex = 0;
+
+ function driverPromiseLoop() {
+ while (currentDriverIndex < supportedDrivers.length) {
+ var driverName = supportedDrivers[currentDriverIndex];
+ currentDriverIndex++;
+
+ self._dbInfo = null;
+ self._ready = null;
+
+ return self.getDriver(driverName).then(function (driver) {
+ self._extend(driver);
+ setDriverToConfig();
+
+ self._ready = self._initStorage(self._config);
+ return self._ready;
+ })['catch'](driverPromiseLoop);
+ }
+
+ setDriverToConfig();
+ var error = new Error('No available storage method found.');
+ self._driverSet = Promise.reject(error);
+ return self._driverSet;
+ }
+
+ return driverPromiseLoop();
+ };
+ }
+
+ // There might be a driver initialization in progress
+ // so wait for it to finish in order to avoid a possible
+ // race condition to set _dbInfo
+ var oldDriverSetDone = this._driverSet !== null ? this._driverSet['catch'](function () {
+ return Promise.resolve();
+ }) : Promise.resolve();
+
+ this._driverSet = oldDriverSetDone.then(function () {
+ var driverName = supportedDrivers[0];
+ self._dbInfo = null;
+ self._ready = null;
+
+ return self.getDriver(driverName).then(function (driver) {
+ self._driver = driver._driver;
+ setDriverToConfig();
+ self._wrapLibraryMethodsWithReady();
+ self._initDriver = initDriver(supportedDrivers);
+ });
+ })['catch'](function () {
+ setDriverToConfig();
+ var error = new Error('No available storage method found.');
+ self._driverSet = Promise.reject(error);
+ return self._driverSet;
+ });
+
+ this._driverSet.then(callback, errorCallback);
+ return this._driverSet;
+ };
+
+ LocalForage.prototype.supports = function supports(driverName) {
+ return !!driverSupport[driverName];
+ };
+
+ LocalForage.prototype._extend = function _extend(libraryMethodsAndProperties) {
+ extend(this, libraryMethodsAndProperties);
+ };
+
+ LocalForage.prototype._getSupportedDrivers = function _getSupportedDrivers(drivers) {
+ var supportedDrivers = [];
+ for (var i = 0, len = drivers.length; i < len; i++) {
+ var driverName = drivers[i];
+ if (this.supports(driverName)) {
+ supportedDrivers.push(driverName);
+ }
+ }
+ return supportedDrivers;
+ };
+
+ LocalForage.prototype._wrapLibraryMethodsWithReady = function _wrapLibraryMethodsWithReady() {
+ // Add a stub for each driver API method that delays the call to the
+ // corresponding driver method until localForage is ready. These stubs
+ // will be replaced by the driver methods as soon as the driver is
+ // loaded, so there is no performance impact.
+ for (var i = 0; i < LibraryMethods.length; i++) {
+ callWhenReady(this, LibraryMethods[i]);
+ }
+ };
+
+ LocalForage.prototype.createInstance = function createInstance(options) {
+ return new LocalForage(options);
+ };
+
+ return LocalForage;
+ })();
+
+ var localForage = new LocalForage();
+
+ exports['default'] = localForage;
+ }).call(typeof window !== 'undefined' ? window : self);
+ module.exports = exports['default'];
+
+/***/ },
+/* 1 */
+/***/ function(module, exports) {
+
+ // Some code originally from async_storage.js in
+ // [Gaia](https://github.com/mozilla-b2g/gaia).
+ 'use strict';
+
+ exports.__esModule = true;
+ (function () {
+ 'use strict';
+
+ var globalObject = this;
+ // Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
+ var indexedDB = indexedDB || this.indexedDB || this.webkitIndexedDB || this.mozIndexedDB || this.OIndexedDB || this.msIndexedDB;
+
+ // If IndexedDB isn't available, we get outta here!
+ if (!indexedDB) {
+ return;
+ }
+
+ var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support';
+ var supportsBlobs;
+ var dbContexts;
+
+ // Abstracts constructing a Blob object, so it also works in older
+ // browsers that don't support the native Blob constructor. (i.e.
+ // old QtWebKit versions, at least).
+ function _createBlob(parts, properties) {
+ parts = parts || [];
+ properties = properties || {};
+ try {
+ return new Blob(parts, properties);
+ } catch (e) {
+ if (e.name !== 'TypeError') {
+ throw e;
+ }
+ var BlobBuilder = globalObject.BlobBuilder || globalObject.MSBlobBuilder || globalObject.MozBlobBuilder || globalObject.WebKitBlobBuilder;
+ var builder = new BlobBuilder();
+ for (var i = 0; i < parts.length; i += 1) {
+ builder.append(parts[i]);
+ }
+ return builder.getBlob(properties.type);
+ }
+ }
+
+ // Transform a binary string to an array buffer, because otherwise
+ // weird stuff happens when you try to work with the binary string directly.
+ // It is known.
+ // From http://stackoverflow.com/questions/14967647/ (continues on next line)
+ // encode-decode-image-with-base64-breaks-image (2013-04-21)
+ function _binStringToArrayBuffer(bin) {
+ var length = bin.length;
+ var buf = new ArrayBuffer(length);
+ var arr = new Uint8Array(buf);
+ for (var i = 0; i < length; i++) {
+ arr[i] = bin.charCodeAt(i);
+ }
+ return buf;
+ }
+
+ // Fetch a blob using ajax. This reveals bugs in Chrome < 43.
+ // For details on all this junk:
+ // https://github.com/nolanlawson/state-of-binary-data-in-the-browser#readme
+ function _blobAjax(url) {
+ return new Promise(function (resolve, reject) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url);
+ xhr.withCredentials = true;
+ xhr.responseType = 'arraybuffer';
+
+ xhr.onreadystatechange = function () {
+ if (xhr.readyState !== 4) {
+ return;
+ }
+ if (xhr.status === 200) {
+ return resolve({
+ response: xhr.response,
+ type: xhr.getResponseHeader('Content-Type')
+ });
+ }
+ reject({ status: xhr.status, response: xhr.response });
+ };
+ xhr.send();
+ });
+ }
+
+ //
+ // Detect blob support. Chrome didn't support it until version 38.
+ // In version 37 they had a broken version where PNGs (and possibly
+ // other binary types) aren't stored correctly, because when you fetch
+ // them, the content type is always null.
+ //
+ // Furthermore, they have some outstanding bugs where blobs occasionally
+ // are read by FileReader as null, or by ajax as 404s.
+ //
+ // Sadly we use the 404 bug to detect the FileReader bug, so if they
+ // get fixed independently and released in different versions of Chrome,
+ // then the bug could come back. So it's worthwhile to watch these issues:
+ // 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916
+ // FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836
+ //
+ function _checkBlobSupportWithoutCaching(idb) {
+ return new Promise(function (resolve, reject) {
+ var blob = _createBlob([''], { type: 'image/png' });
+ var txn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], 'readwrite');
+ txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key');
+ txn.oncomplete = function () {
+ // have to do it in a separate transaction, else the correct
+ // content type is always returned
+ var blobTxn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], 'readwrite');
+ var getBlobReq = blobTxn.objectStore(DETECT_BLOB_SUPPORT_STORE).get('key');
+ getBlobReq.onerror = reject;
+ getBlobReq.onsuccess = function (e) {
+
+ var storedBlob = e.target.result;
+ var url = URL.createObjectURL(storedBlob);
+
+ _blobAjax(url).then(function (res) {
+ resolve(!!(res && res.type === 'image/png'));
+ }, function () {
+ resolve(false);
+ }).then(function () {
+ URL.revokeObjectURL(url);
+ });
+ };
+ };
+ })['catch'](function () {
+ return false; // error, so assume unsupported
+ });
+ }
+
+ function _checkBlobSupport(idb) {
+ if (typeof supportsBlobs === 'boolean') {
+ return Promise.resolve(supportsBlobs);
+ }
+ return _checkBlobSupportWithoutCaching(idb).then(function (value) {
+ supportsBlobs = value;
+ return supportsBlobs;
+ });
+ }
+
+ // encode a blob for indexeddb engines that don't support blobs
+ function _encodeBlob(blob) {
+ return new Promise(function (resolve, reject) {
+ var reader = new FileReader();
+ reader.onerror = reject;
+ reader.onloadend = function (e) {
+ var base64 = btoa(e.target.result || '');
+ resolve({
+ __local_forage_encoded_blob: true,
+ data: base64,
+ type: blob.type
+ });
+ };
+ reader.readAsBinaryString(blob);
+ });
+ }
+
+ // decode an encoded blob
+ function _decodeBlob(encodedBlob) {
+ var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data));
+ return _createBlob([arrayBuff], { type: encodedBlob.type });
+ }
+
+ // is this one of our fancy encoded blobs?
+ function _isEncodedBlob(value) {
+ return value && value.__local_forage_encoded_blob;
+ }
+
+ // Open the IndexedDB database (automatically creates one if one didn't
+ // previously exist), using any options set in the config.
+ function _initStorage(options) {
+ var self = this;
+ var dbInfo = {
+ db: null
+ };
+
+ if (options) {
+ for (var i in options) {
+ dbInfo[i] = options[i];
+ }
+ }
+
+ // Initialize a singleton container for all running localForages.
+ if (!dbContexts) {
+ dbContexts = {};
+ }
+
+ // Get the current context of the database;
+ var dbContext = dbContexts[dbInfo.name];
+
+ // ...or create a new context.
+ if (!dbContext) {
+ dbContext = {
+ // Running localForages sharing a database.
+ forages: [],
+ // Shared database.
+ db: null
+ };
+ // Register the new context in the global container.
+ dbContexts[dbInfo.name] = dbContext;
+ }
+
+ // Register itself as a running localForage in the current context.
+ dbContext.forages.push(this);
+
+ // Create an array of readiness of the related localForages.
+ var readyPromises = [];
+
+ function ignoreErrors() {
+ // Don't handle errors here,
+ // just makes sure related localForages aren't pending.
+ return Promise.resolve();
+ }
+
+ for (var j = 0; j < dbContext.forages.length; j++) {
+ var forage = dbContext.forages[j];
+ if (forage !== this) {
+ // Don't wait for itself...
+ readyPromises.push(forage.ready()['catch'](ignoreErrors));
+ }
+ }
+
+ // Take a snapshot of the related localForages.
+ var forages = dbContext.forages.slice(0);
+
+ // Initialize the connection process only when
+ // all the related localForages aren't pending.
+ return Promise.all(readyPromises).then(function () {
+ dbInfo.db = dbContext.db;
+ // Get the connection or open a new one without upgrade.
+ return _getOriginalConnection(dbInfo);
+ }).then(function (db) {
+ dbInfo.db = db;
+ if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) {
+ // Reopen the database for upgrading.
+ return _getUpgradedConnection(dbInfo);
+ }
+ return db;
+ }).then(function (db) {
+ dbInfo.db = dbContext.db = db;
+ self._dbInfo = dbInfo;
+ // Share the final connection amongst related localForages.
+ for (var k = 0; k < forages.length; k++) {
+ var forage = forages[k];
+ if (forage !== self) {
+ // Self is already up-to-date.
+ forage._dbInfo.db = dbInfo.db;
+ forage._dbInfo.version = dbInfo.version;
+ }
+ }
+ });
+ }
+
+ function _getOriginalConnection(dbInfo) {
+ return _getConnection(dbInfo, false);
+ }
+
+ function _getUpgradedConnection(dbInfo) {
+ return _getConnection(dbInfo, true);
+ }
+
+ function _getConnection(dbInfo, upgradeNeeded) {
+ return new Promise(function (resolve, reject) {
+ if (dbInfo.db) {
+ if (upgradeNeeded) {
+ dbInfo.db.close();
+ } else {
+ return resolve(dbInfo.db);
+ }
+ }
+
+ var dbArgs = [dbInfo.name];
+
+ if (upgradeNeeded) {
+ dbArgs.push(dbInfo.version);
+ }
+
+ var openreq = indexedDB.open.apply(indexedDB, dbArgs);
+
+ if (upgradeNeeded) {
+ openreq.onupgradeneeded = function (e) {
+ var db = openreq.result;
+ try {
+ db.createObjectStore(dbInfo.storeName);
+ if (e.oldVersion <= 1) {
+ // Added when support for blob shims was added
+ db.createObjectStore(DETECT_BLOB_SUPPORT_STORE);
+ }
+ } catch (ex) {
+ if (ex.name === 'ConstraintError') {
+ globalObject.console.warn('The database "' + dbInfo.name + '"' + ' has been upgraded from version ' + e.oldVersion + ' to version ' + e.newVersion + ', but the storage "' + dbInfo.storeName + '" already exists.');
+ } else {
+ throw ex;
+ }
+ }
+ };
+ }
+
+ openreq.onerror = function () {
+ reject(openreq.error);
+ };
+
+ openreq.onsuccess = function () {
+ resolve(openreq.result);
+ };
+ });
+ }
+
+ function _isUpgradeNeeded(dbInfo, defaultVersion) {
+ if (!dbInfo.db) {
+ return true;
+ }
+
+ var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName);
+ var isDowngrade = dbInfo.version < dbInfo.db.version;
+ var isUpgrade = dbInfo.version > dbInfo.db.version;
+
+ if (isDowngrade) {
+ // If the version is not the default one
+ // then warn for impossible downgrade.
+ if (dbInfo.version !== defaultVersion) {
+ globalObject.console.warn('The database "' + dbInfo.name + '"' + ' can\'t be downgraded from version ' + dbInfo.db.version + ' to version ' + dbInfo.version + '.');
+ }
+ // Align the versions to prevent errors.
+ dbInfo.version = dbInfo.db.version;
+ }
+
+ if (isUpgrade || isNewStore) {
+ // If the store is new then increment the version (if needed).
+ // This will trigger an "upgradeneeded" event which is required
+ // for creating a store.
+ if (isNewStore) {
+ var incVersion = dbInfo.db.version + 1;
+ if (incVersion > dbInfo.version) {
+ dbInfo.version = incVersion;
+ }
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ function getItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
+ var req = store.get(key);
+
+ req.onsuccess = function () {
+ var value = req.result;
+ if (value === undefined) {
+ value = null;
+ }
+ if (_isEncodedBlob(value)) {
+ value = _decodeBlob(value);
+ }
+ resolve(value);
+ };
+
+ req.onerror = function () {
+ reject(req.error);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Iterate over all items stored in database.
+ function iterate(iterator, callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
+
+ var req = store.openCursor();
+ var iterationNumber = 1;
+
+ req.onsuccess = function () {
+ var cursor = req.result;
+
+ if (cursor) {
+ var value = cursor.value;
+ if (_isEncodedBlob(value)) {
+ value = _decodeBlob(value);
+ }
+ var result = iterator(value, cursor.key, iterationNumber++);
+
+ if (result !== void 0) {
+ resolve(result);
+ } else {
+ cursor['continue']();
+ }
+ } else {
+ resolve();
+ }
+ };
+
+ req.onerror = function () {
+ reject(req.error);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+
+ return promise;
+ }
+
+ function setItem(key, value, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ var dbInfo;
+ self.ready().then(function () {
+ dbInfo = self._dbInfo;
+ if (value instanceof Blob) {
+ return _checkBlobSupport(dbInfo.db).then(function (blobSupport) {
+ if (blobSupport) {
+ return value;
+ }
+ return _encodeBlob(value);
+ });
+ }
+ return value;
+ }).then(function (value) {
+ var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
+ var store = transaction.objectStore(dbInfo.storeName);
+
+ // The reason we don't _save_ null is because IE 10 does
+ // not support saving the `null` type in IndexedDB. How
+ // ironic, given the bug below!
+ // See: https://github.com/mozilla/localForage/issues/161
+ if (value === null) {
+ value = undefined;
+ }
+
+ var req = store.put(value, key);
+ transaction.oncomplete = function () {
+ // Cast to undefined so the value passed to
+ // callback/promise is the same as what one would get out
+ // of `getItem()` later. This leads to some weirdness
+ // (setItem('foo', undefined) will return `null`), but
+ // it's not my fault localStorage is our baseline and that
+ // it's weird.
+ if (value === undefined) {
+ value = null;
+ }
+
+ resolve(value);
+ };
+ transaction.onabort = transaction.onerror = function () {
+ var err = req.error ? req.error : req.transaction.error;
+ reject(err);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function removeItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
+ var store = transaction.objectStore(dbInfo.storeName);
+
+ // We use a Grunt task to make this safe for IE and some
+ // versions of Android (including those used by Cordova).
+ // Normally IE won't like `.delete()` and will insist on
+ // using `['delete']()`, but we have a build step that
+ // fixes this for us now.
+ var req = store['delete'](key);
+ transaction.oncomplete = function () {
+ resolve();
+ };
+
+ transaction.onerror = function () {
+ reject(req.error);
+ };
+
+ // The request will be also be aborted if we've exceeded our storage
+ // space.
+ transaction.onabort = function () {
+ var err = req.error ? req.error : req.transaction.error;
+ reject(err);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function clear(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
+ var store = transaction.objectStore(dbInfo.storeName);
+ var req = store.clear();
+
+ transaction.oncomplete = function () {
+ resolve();
+ };
+
+ transaction.onabort = transaction.onerror = function () {
+ var err = req.error ? req.error : req.transaction.error;
+ reject(err);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function length(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
+ var req = store.count();
+
+ req.onsuccess = function () {
+ resolve(req.result);
+ };
+
+ req.onerror = function () {
+ reject(req.error);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function key(n, callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ if (n < 0) {
+ resolve(null);
+
+ return;
+ }
+
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
+
+ var advanced = false;
+ var req = store.openCursor();
+ req.onsuccess = function () {
+ var cursor = req.result;
+ if (!cursor) {
+ // this means there weren't enough keys
+ resolve(null);
+
+ return;
+ }
+
+ if (n === 0) {
+ // We have the first key, return it if that's what they
+ // wanted.
+ resolve(cursor.key);
+ } else {
+ if (!advanced) {
+ // Otherwise, ask the cursor to skip ahead n
+ // records.
+ advanced = true;
+ cursor.advance(n);
+ } else {
+ // When we get here, we've got the nth key.
+ resolve(cursor.key);
+ }
+ }
+ };
+
+ req.onerror = function () {
+ reject(req.error);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function keys(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
+
+ var req = store.openCursor();
+ var keys = [];
+
+ req.onsuccess = function () {
+ var cursor = req.result;
+
+ if (!cursor) {
+ resolve(keys);
+ return;
+ }
+
+ keys.push(cursor.key);
+ cursor['continue']();
+ };
+
+ req.onerror = function () {
+ reject(req.error);
+ };
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function executeCallback(promise, callback) {
+ if (callback) {
+ promise.then(function (result) {
+ callback(null, result);
+ }, function (error) {
+ callback(error);
+ });
+ }
+ }
+
+ var asyncStorage = {
+ _driver: 'asyncStorage',
+ _initStorage: _initStorage,
+ iterate: iterate,
+ getItem: getItem,
+ setItem: setItem,
+ removeItem: removeItem,
+ clear: clear,
+ length: length,
+ key: key,
+ keys: keys
+ };
+
+ exports['default'] = asyncStorage;
+ }).call(typeof window !== 'undefined' ? window : self);
+ module.exports = exports['default'];
+
+/***/ },
+/* 2 */
+/***/ function(module, exports, __webpack_require__) {
+
+ // If IndexedDB isn't available, we'll fall back to localStorage.
+ // Note that this will have considerable performance and storage
+ // side-effects (all data will be serialized on save and only data that
+ // can be converted to a string via `JSON.stringify()` will be saved).
+ 'use strict';
+
+ exports.__esModule = true;
+ (function () {
+ 'use strict';
+
+ var globalObject = this;
+ var localStorage = null;
+
+ // If the app is running inside a Google Chrome packaged webapp, or some
+ // other context where localStorage isn't available, we don't use
+ // localStorage. This feature detection is preferred over the old
+ // `if (window.chrome && window.chrome.runtime)` code.
+ // See: https://github.com/mozilla/localForage/issues/68
+ try {
+ // If localStorage isn't available, we get outta here!
+ // This should be inside a try catch
+ if (!this.localStorage || !('setItem' in this.localStorage)) {
+ return;
+ }
+ // Initialize localStorage and create a variable to use throughout
+ // the code.
+ localStorage = this.localStorage;
+ } catch (e) {
+ return;
+ }
+
+ // Config the localStorage backend, using options set in the config.
+ function _initStorage(options) {
+ var self = this;
+ var dbInfo = {};
+ if (options) {
+ for (var i in options) {
+ dbInfo[i] = options[i];
+ }
+ }
+
+ dbInfo.keyPrefix = dbInfo.name + '/';
+
+ if (dbInfo.storeName !== self._defaultConfig.storeName) {
+ dbInfo.keyPrefix += dbInfo.storeName + '/';
+ }
+
+ self._dbInfo = dbInfo;
+
+ return new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(3));
+ }).then(function (lib) {
+ dbInfo.serializer = lib;
+ return Promise.resolve();
+ });
+ }
+
+ // Remove all keys from the datastore, effectively destroying all data in
+ // the app's key/value store!
+ function clear(callback) {
+ var self = this;
+ var promise = self.ready().then(function () {
+ var keyPrefix = self._dbInfo.keyPrefix;
+
+ for (var i = localStorage.length - 1; i >= 0; i--) {
+ var key = localStorage.key(i);
+
+ if (key.indexOf(keyPrefix) === 0) {
+ localStorage.removeItem(key);
+ }
+ }
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Retrieve an item from the store. Unlike the original async_storage
+ // library in Gaia, we don't modify return values at all. If a key's value
+ // is `undefined`, we pass that value to the callback function.
+ function getItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var result = localStorage.getItem(dbInfo.keyPrefix + key);
+
+ // If a result was found, parse it from the serialized
+ // string into a JS object. If result isn't truthy, the key
+ // is likely undefined and we'll pass it straight to the
+ // callback.
+ if (result) {
+ result = dbInfo.serializer.deserialize(result);
+ }
+
+ return result;
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Iterate over all items in the store.
+ function iterate(iterator, callback) {
+ var self = this;
+
+ var promise = self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var keyPrefix = dbInfo.keyPrefix;
+ var keyPrefixLength = keyPrefix.length;
+ var length = localStorage.length;
+
+ // We use a dedicated iterator instead of the `i` variable below
+ // so other keys we fetch in localStorage aren't counted in
+ // the `iterationNumber` argument passed to the `iterate()`
+ // callback.
+ //
+ // See: github.com/mozilla/localForage/pull/435#discussion_r38061530
+ var iterationNumber = 1;
+
+ for (var i = 0; i < length; i++) {
+ var key = localStorage.key(i);
+ if (key.indexOf(keyPrefix) !== 0) {
+ continue;
+ }
+ var value = localStorage.getItem(key);
+
+ // If a result was found, parse it from the serialized
+ // string into a JS object. If result isn't truthy, the
+ // key is likely undefined and we'll pass it straight
+ // to the iterator.
+ if (value) {
+ value = dbInfo.serializer.deserialize(value);
+ }
+
+ value = iterator(value, key.substring(keyPrefixLength), iterationNumber++);
+
+ if (value !== void 0) {
+ return value;
+ }
+ }
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Same as localStorage's key() method, except takes a callback.
+ function key(n, callback) {
+ var self = this;
+ var promise = self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var result;
+ try {
+ result = localStorage.key(n);
+ } catch (error) {
+ result = null;
+ }
+
+ // Remove the prefix from the key, if a key is found.
+ if (result) {
+ result = result.substring(dbInfo.keyPrefix.length);
+ }
+
+ return result;
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function keys(callback) {
+ var self = this;
+ var promise = self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ var length = localStorage.length;
+ var keys = [];
+
+ for (var i = 0; i < length; i++) {
+ if (localStorage.key(i).indexOf(dbInfo.keyPrefix) === 0) {
+ keys.push(localStorage.key(i).substring(dbInfo.keyPrefix.length));
+ }
+ }
+
+ return keys;
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Supply the number of keys in the datastore to the callback function.
+ function length(callback) {
+ var self = this;
+ var promise = self.keys().then(function (keys) {
+ return keys.length;
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Remove an item from the store, nice and simple.
+ function removeItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ localStorage.removeItem(dbInfo.keyPrefix + key);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Set a key's value and run an optional callback once the value is set.
+ // Unlike Gaia's implementation, the callback function is passed the value,
+ // in case you want to operate on that value only after you're sure it
+ // saved, or something like that.
+ function setItem(key, value, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = self.ready().then(function () {
+ // Convert undefined values to null.
+ // https://github.com/mozilla/localForage/pull/42
+ if (value === undefined) {
+ value = null;
+ }
+
+ // Save the original value to pass to the callback.
+ var originalValue = value;
+
+ return new Promise(function (resolve, reject) {
+ var dbInfo = self._dbInfo;
+ dbInfo.serializer.serialize(value, function (value, error) {
+ if (error) {
+ reject(error);
+ } else {
+ try {
+ localStorage.setItem(dbInfo.keyPrefix + key, value);
+ resolve(originalValue);
+ } catch (e) {
+ // localStorage capacity exceeded.
+ // TODO: Make this a specific error/event.
+ if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
+ reject(e);
+ }
+ reject(e);
+ }
+ }
+ });
+ });
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function executeCallback(promise, callback) {
+ if (callback) {
+ promise.then(function (result) {
+ callback(null, result);
+ }, function (error) {
+ callback(error);
+ });
+ }
+ }
+
+ var localStorageWrapper = {
+ _driver: 'localStorageWrapper',
+ _initStorage: _initStorage,
+ // Default API, from Gaia/localStorage.
+ iterate: iterate,
+ getItem: getItem,
+ setItem: setItem,
+ removeItem: removeItem,
+ clear: clear,
+ length: length,
+ key: key,
+ keys: keys
+ };
+
+ exports['default'] = localStorageWrapper;
+ }).call(typeof window !== 'undefined' ? window : self);
+ module.exports = exports['default'];
+
+/***/ },
+/* 3 */
+/***/ function(module, exports) {
+
+ 'use strict';
+
+ exports.__esModule = true;
+ (function () {
+ 'use strict';
+
+ // Sadly, the best way to save binary data in WebSQL/localStorage is serializing
+ // it to Base64, so this is how we store it to prevent very strange errors with less
+ // verbose ways of binary <-> string data storage.
+ var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+
+ var BLOB_TYPE_PREFIX = '~~local_forage_type~';
+ var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/;
+
+ var SERIALIZED_MARKER = '__lfsc__:';
+ var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length;
+
+ // OMG the serializations!
+ var TYPE_ARRAYBUFFER = 'arbf';
+ var TYPE_BLOB = 'blob';
+ var TYPE_INT8ARRAY = 'si08';
+ var TYPE_UINT8ARRAY = 'ui08';
+ var TYPE_UINT8CLAMPEDARRAY = 'uic8';
+ var TYPE_INT16ARRAY = 'si16';
+ var TYPE_INT32ARRAY = 'si32';
+ var TYPE_UINT16ARRAY = 'ur16';
+ var TYPE_UINT32ARRAY = 'ui32';
+ var TYPE_FLOAT32ARRAY = 'fl32';
+ var TYPE_FLOAT64ARRAY = 'fl64';
+ var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length;
+
+ // Get out of our habit of using `window` inline, at least.
+ var globalObject = this;
+
+ // Abstracts constructing a Blob object, so it also works in older
+ // browsers that don't support the native Blob constructor. (i.e.
+ // old QtWebKit versions, at least).
+ function _createBlob(parts, properties) {
+ parts = parts || [];
+ properties = properties || {};
+
+ try {
+ return new Blob(parts, properties);
+ } catch (err) {
+ if (err.name !== 'TypeError') {
+ throw err;
+ }
+
+ var BlobBuilder = globalObject.BlobBuilder || globalObject.MSBlobBuilder || globalObject.MozBlobBuilder || globalObject.WebKitBlobBuilder;
+
+ var builder = new BlobBuilder();
+ for (var i = 0; i < parts.length; i += 1) {
+ builder.append(parts[i]);
+ }
+
+ return builder.getBlob(properties.type);
+ }
+ }
+
+ // Serialize a value, afterwards executing a callback (which usually
+ // instructs the `setItem()` callback/promise to be executed). This is how
+ // we store binary data with localStorage.
+ function serialize(value, callback) {
+ var valueString = '';
+ if (value) {
+ valueString = value.toString();
+ }
+
+ // Cannot use `value instanceof ArrayBuffer` or such here, as these
+ // checks fail when running the tests using casper.js...
+ //
+ // TODO: See why those tests fail and use a better solution.
+ if (value && (value.toString() === '[object ArrayBuffer]' || value.buffer && value.buffer.toString() === '[object ArrayBuffer]')) {
+ // Convert binary arrays to a string and prefix the string with
+ // a special marker.
+ var buffer;
+ var marker = SERIALIZED_MARKER;
+
+ if (value instanceof ArrayBuffer) {
+ buffer = value;
+ marker += TYPE_ARRAYBUFFER;
+ } else {
+ buffer = value.buffer;
+
+ if (valueString === '[object Int8Array]') {
+ marker += TYPE_INT8ARRAY;
+ } else if (valueString === '[object Uint8Array]') {
+ marker += TYPE_UINT8ARRAY;
+ } else if (valueString === '[object Uint8ClampedArray]') {
+ marker += TYPE_UINT8CLAMPEDARRAY;
+ } else if (valueString === '[object Int16Array]') {
+ marker += TYPE_INT16ARRAY;
+ } else if (valueString === '[object Uint16Array]') {
+ marker += TYPE_UINT16ARRAY;
+ } else if (valueString === '[object Int32Array]') {
+ marker += TYPE_INT32ARRAY;
+ } else if (valueString === '[object Uint32Array]') {
+ marker += TYPE_UINT32ARRAY;
+ } else if (valueString === '[object Float32Array]') {
+ marker += TYPE_FLOAT32ARRAY;
+ } else if (valueString === '[object Float64Array]') {
+ marker += TYPE_FLOAT64ARRAY;
+ } else {
+ callback(new Error('Failed to get type for BinaryArray'));
+ }
+ }
+
+ callback(marker + bufferToString(buffer));
+ } else if (valueString === '[object Blob]') {
+ // Conver the blob to a binaryArray and then to a string.
+ var fileReader = new FileReader();
+
+ fileReader.onload = function () {
+ // Backwards-compatible prefix for the blob type.
+ var str = BLOB_TYPE_PREFIX + value.type + '~' + bufferToString(this.result);
+
+ callback(SERIALIZED_MARKER + TYPE_BLOB + str);
+ };
+
+ fileReader.readAsArrayBuffer(value);
+ } else {
+ try {
+ callback(JSON.stringify(value));
+ } catch (e) {
+ console.error("Couldn't convert value into a JSON string: ", value);
+
+ callback(null, e);
+ }
+ }
+ }
+
+ // Deserialize data we've inserted into a value column/field. We place
+ // special markers into our strings to mark them as encoded; this isn't
+ // as nice as a meta field, but it's the only sane thing we can do whilst
+ // keeping localStorage support intact.
+ //
+ // Oftentimes this will just deserialize JSON content, but if we have a
+ // special marker (SERIALIZED_MARKER, defined above), we will extract
+ // some kind of arraybuffer/binary data/typed array out of the string.
+ function deserialize(value) {
+ // If we haven't marked this string as being specially serialized (i.e.
+ // something other than serialized JSON), we can just return it and be
+ // done with it.
+ if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) {
+ return JSON.parse(value);
+ }
+
+ // The following code deals with deserializing some kind of Blob or
+ // TypedArray. First we separate out the type of data we're dealing
+ // with from the data itself.
+ var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
+ var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH);
+
+ var blobType;
+ // Backwards-compatible blob type serialization strategy.
+ // DBs created with older versions of localForage will simply not have the blob type.
+ if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) {
+ var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX);
+ blobType = matcher[1];
+ serializedString = serializedString.substring(matcher[0].length);
+ }
+ var buffer = stringToBuffer(serializedString);
+
+ // Return the right type based on the code/type set during
+ // serialization.
+ switch (type) {
+ case TYPE_ARRAYBUFFER:
+ return buffer;
+ case TYPE_BLOB:
+ return _createBlob([buffer], { type: blobType });
+ case TYPE_INT8ARRAY:
+ return new Int8Array(buffer);
+ case TYPE_UINT8ARRAY:
+ return new Uint8Array(buffer);
+ case TYPE_UINT8CLAMPEDARRAY:
+ return new Uint8ClampedArray(buffer);
+ case TYPE_INT16ARRAY:
+ return new Int16Array(buffer);
+ case TYPE_UINT16ARRAY:
+ return new Uint16Array(buffer);
+ case TYPE_INT32ARRAY:
+ return new Int32Array(buffer);
+ case TYPE_UINT32ARRAY:
+ return new Uint32Array(buffer);
+ case TYPE_FLOAT32ARRAY:
+ return new Float32Array(buffer);
+ case TYPE_FLOAT64ARRAY:
+ return new Float64Array(buffer);
+ default:
+ throw new Error('Unkown type: ' + type);
+ }
+ }
+
+ function stringToBuffer(serializedString) {
+ // Fill the string into a ArrayBuffer.
+ var bufferLength = serializedString.length * 0.75;
+ var len = serializedString.length;
+ var i;
+ var p = 0;
+ var encoded1, encoded2, encoded3, encoded4;
+
+ if (serializedString[serializedString.length - 1] === '=') {
+ bufferLength--;
+ if (serializedString[serializedString.length - 2] === '=') {
+ bufferLength--;
+ }
+ }
+
+ var buffer = new ArrayBuffer(bufferLength);
+ var bytes = new Uint8Array(buffer);
+
+ for (i = 0; i < len; i += 4) {
+ encoded1 = BASE_CHARS.indexOf(serializedString[i]);
+ encoded2 = BASE_CHARS.indexOf(serializedString[i + 1]);
+ encoded3 = BASE_CHARS.indexOf(serializedString[i + 2]);
+ encoded4 = BASE_CHARS.indexOf(serializedString[i + 3]);
+
+ /*jslint bitwise: true */
+ bytes[p++] = encoded1 << 2 | encoded2 >> 4;
+ bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2;
+ bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63;
+ }
+ return buffer;
+ }
+
+ // Converts a buffer to a string to store, serialized, in the backend
+ // storage library.
+ function bufferToString(buffer) {
+ // base64-arraybuffer
+ var bytes = new Uint8Array(buffer);
+ var base64String = '';
+ var i;
+
+ for (i = 0; i < bytes.length; i += 3) {
+ /*jslint bitwise: true */
+ base64String += BASE_CHARS[bytes[i] >> 2];
+ base64String += BASE_CHARS[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
+ base64String += BASE_CHARS[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
+ base64String += BASE_CHARS[bytes[i + 2] & 63];
+ }
+
+ if (bytes.length % 3 === 2) {
+ base64String = base64String.substring(0, base64String.length - 1) + '=';
+ } else if (bytes.length % 3 === 1) {
+ base64String = base64String.substring(0, base64String.length - 2) + '==';
+ }
+
+ return base64String;
+ }
+
+ var localforageSerializer = {
+ serialize: serialize,
+ deserialize: deserialize,
+ stringToBuffer: stringToBuffer,
+ bufferToString: bufferToString
+ };
+
+ exports['default'] = localforageSerializer;
+ }).call(typeof window !== 'undefined' ? window : self);
+ module.exports = exports['default'];
+
+/***/ },
+/* 4 */
+/***/ function(module, exports, __webpack_require__) {
+
+ /*
+ * Includes code from:
+ *
+ * base64-arraybuffer
+ * https://github.com/niklasvh/base64-arraybuffer
+ *
+ * Copyright (c) 2012 Niklas von Hertzen
+ * Licensed under the MIT license.
+ */
+ 'use strict';
+
+ exports.__esModule = true;
+ (function () {
+ 'use strict';
+
+ var globalObject = this;
+ var openDatabase = this.openDatabase;
+
+ // If WebSQL methods aren't available, we can stop now.
+ if (!openDatabase) {
+ return;
+ }
+
+ // Open the WebSQL database (automatically creates one if one didn't
+ // previously exist), using any options set in the config.
+ function _initStorage(options) {
+ var self = this;
+ var dbInfo = {
+ db: null
+ };
+
+ if (options) {
+ for (var i in options) {
+ dbInfo[i] = typeof options[i] !== 'string' ? options[i].toString() : options[i];
+ }
+ }
+
+ var dbInfoPromise = new Promise(function (resolve, reject) {
+ // Open the database; the openDatabase API will automatically
+ // create it for us if it doesn't exist.
+ try {
+ dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), dbInfo.description, dbInfo.size);
+ } catch (e) {
+ return self.setDriver(self.LOCALSTORAGE).then(function () {
+ return self._initStorage(options);
+ }).then(resolve)['catch'](reject);
+ }
+
+ // Create our key/value table if it doesn't exist.
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + ' (id INTEGER PRIMARY KEY, key unique, value)', [], function () {
+ self._dbInfo = dbInfo;
+ resolve();
+ }, function (t, error) {
+ reject(error);
+ });
+ });
+ });
+
+ return new Promise(function (resolve, reject) {
+ resolve(__webpack_require__(3));
+ }).then(function (lib) {
+ dbInfo.serializer = lib;
+ return dbInfoPromise;
+ });
+ }
+
+ function getItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE key = ? LIMIT 1', [key], function (t, results) {
+ var result = results.rows.length ? results.rows.item(0).value : null;
+
+ // Check to see if this is serialized content we need to
+ // unpack.
+ if (result) {
+ result = dbInfo.serializer.deserialize(result);
+ }
+
+ resolve(result);
+ }, function (t, error) {
+
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function iterate(iterator, callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('SELECT * FROM ' + dbInfo.storeName, [], function (t, results) {
+ var rows = results.rows;
+ var length = rows.length;
+
+ for (var i = 0; i < length; i++) {
+ var item = rows.item(i);
+ var result = item.value;
+
+ // Check to see if this is serialized content
+ // we need to unpack.
+ if (result) {
+ result = dbInfo.serializer.deserialize(result);
+ }
+
+ result = iterator(result, item.key, i + 1);
+
+ // void(0) prevents problems with redefinition
+ // of `undefined`.
+ if (result !== void 0) {
+ resolve(result);
+ return;
+ }
+ }
+
+ resolve();
+ }, function (t, error) {
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function setItem(key, value, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ // The localStorage API doesn't return undefined values in an
+ // "expected" way, so undefined is always cast to null in all
+ // drivers. See: https://github.com/mozilla/localForage/pull/42
+ if (value === undefined) {
+ value = null;
+ }
+
+ // Save the original value to pass to the callback.
+ var originalValue = value;
+
+ var dbInfo = self._dbInfo;
+ dbInfo.serializer.serialize(value, function (value, error) {
+ if (error) {
+ reject(error);
+ } else {
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('INSERT OR REPLACE INTO ' + dbInfo.storeName + ' (key, value) VALUES (?, ?)', [key, value], function () {
+ resolve(originalValue);
+ }, function (t, error) {
+ reject(error);
+ });
+ }, function (sqlError) {
+ // The transaction failed; check
+ // to see if it's a quota error.
+ if (sqlError.code === sqlError.QUOTA_ERR) {
+ // We reject the callback outright for now, but
+ // it's worth trying to re-run the transaction.
+ // Even if the user accepts the prompt to use
+ // more storage on Safari, this error will
+ // be called.
+ //
+ // TODO: Try to re-run the transaction.
+ reject(sqlError);
+ }
+ });
+ }
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function removeItem(key, callback) {
+ var self = this;
+
+ // Cast the key to a string, as that's all we can set as a key.
+ if (typeof key !== 'string') {
+ globalObject.console.warn(key + ' used as a key, but it is not a string.');
+ key = String(key);
+ }
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('DELETE FROM ' + dbInfo.storeName + ' WHERE key = ?', [key], function () {
+ resolve();
+ }, function (t, error) {
+
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Deletes every item in the table.
+ // TODO: Find out if this resets the AUTO_INCREMENT number.
+ function clear(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('DELETE FROM ' + dbInfo.storeName, [], function () {
+ resolve();
+ }, function (t, error) {
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Does a simple `COUNT(key)` to get the number of items stored in
+ // localForage.
+ function length(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ // Ahhh, SQL makes this one soooooo easy.
+ t.executeSql('SELECT COUNT(key) as c FROM ' + dbInfo.storeName, [], function (t, results) {
+ var result = results.rows.item(0).c;
+
+ resolve(result);
+ }, function (t, error) {
+
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ // Return the key located at key index X; essentially gets the key from a
+ // `WHERE id = ?`. This is the most efficient way I can think to implement
+ // this rarely-used (in my experience) part of the API, but it can seem
+ // inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so
+ // the ID of each key will change every time it's updated. Perhaps a stored
+ // procedure for the `setItem()` SQL would solve this problem?
+ // TODO: Don't change ID on `setItem()`.
+ function key(n, callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('SELECT key FROM ' + dbInfo.storeName + ' WHERE id = ? LIMIT 1', [n + 1], function (t, results) {
+ var result = results.rows.length ? results.rows.item(0).key : null;
+ resolve(result);
+ }, function (t, error) {
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function keys(callback) {
+ var self = this;
+
+ var promise = new Promise(function (resolve, reject) {
+ self.ready().then(function () {
+ var dbInfo = self._dbInfo;
+ dbInfo.db.transaction(function (t) {
+ t.executeSql('SELECT key FROM ' + dbInfo.storeName, [], function (t, results) {
+ var keys = [];
+
+ for (var i = 0; i < results.rows.length; i++) {
+ keys.push(results.rows.item(i).key);
+ }
+
+ resolve(keys);
+ }, function (t, error) {
+
+ reject(error);
+ });
+ });
+ })['catch'](reject);
+ });
+
+ executeCallback(promise, callback);
+ return promise;
+ }
+
+ function executeCallback(promise, callback) {
+ if (callback) {
+ promise.then(function (result) {
+ callback(null, result);
+ }, function (error) {
+ callback(error);
+ });
+ }
+ }
+
+ var webSQLStorage = {
+ _driver: 'webSQLStorage',
+ _initStorage: _initStorage,
+ iterate: iterate,
+ getItem: getItem,
+ setItem: setItem,
+ removeItem: removeItem,
+ clear: clear,
+ length: length,
+ key: key,
+ keys: keys
+ };
+
+ exports['default'] = webSQLStorage;
+ }).call(typeof window !== 'undefined' ? window : self);
+ module.exports = exports['default'];
+
+/***/ }
+/******/ ])
+});
+;
+}
\ No newline at end of file
diff --git a/scripts/lib/md5.js b/scripts/lib/md5.js
new file mode 100644
index 0000000..f92ba37
--- /dev/null
+++ b/scripts/lib/md5.js
@@ -0,0 +1,274 @@
+/*
+ * JavaScript MD5 1.0.1
+ * https://github.com/blueimp/JavaScript-MD5
+ *
+ * Copyright 2011, Sebastian Tschan
+ * https://blueimp.net
+ *
+ * Licensed under the MIT license:
+ * http://www.opensource.org/licenses/MIT
+ *
+ * Based on
+ * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
+ * Digest Algorithm, as defined in RFC 1321.
+ * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
+ * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
+ * Distributed under the BSD License
+ * See http://pajhome.org.uk/crypt/md5 for more info.
+ */
+
+/*jslint bitwise: true */
+/*global unescape, define */
+
+(function ($) {
+ 'use strict';
+
+ /*
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally
+ * to work around bugs in some JS interpreters.
+ */
+ function safe_add(x, y) {
+ var lsw = (x & 0xFFFF) + (y & 0xFFFF),
+ msw = (x >> 16) + (y >> 16) + (lsw >> 16);
+ return (msw << 16) | (lsw & 0xFFFF);
+ }
+
+ /*
+ * Bitwise rotate a 32-bit number to the left.
+ */
+ function bit_rol(num, cnt) {
+ return (num << cnt) | (num >>> (32 - cnt));
+ }
+
+ /*
+ * These functions implement the four basic operations the algorithm uses.
+ */
+ function md5_cmn(q, a, b, x, s, t) {
+ return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
+ }
+ function md5_ff(a, b, c, d, x, s, t) {
+ return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
+ }
+ function md5_gg(a, b, c, d, x, s, t) {
+ return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
+ }
+ function md5_hh(a, b, c, d, x, s, t) {
+ return md5_cmn(b ^ c ^ d, a, b, x, s, t);
+ }
+ function md5_ii(a, b, c, d, x, s, t) {
+ return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
+ }
+
+ /*
+ * Calculate the MD5 of an array of little-endian words, and a bit length.
+ */
+ function binl_md5(x, len) {
+ /* append padding */
+ x[len >> 5] |= 0x80 << (len % 32);
+ x[(((len + 64) >>> 9) << 4) + 14] = len;
+
+ var i, olda, oldb, oldc, oldd,
+ a = 1732584193,
+ b = -271733879,
+ c = -1732584194,
+ d = 271733878;
+
+ for (i = 0; i < x.length; i += 16) {
+ olda = a;
+ oldb = b;
+ oldc = c;
+ oldd = d;
+
+ a = md5_ff(a, b, c, d, x[i], 7, -680876936);
+ d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
+ c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
+ b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
+ a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
+ d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
+ c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
+ b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
+ a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
+ d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
+ c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
+ b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
+ a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
+ d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
+ c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
+ b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
+
+ a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
+ d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
+ c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
+ b = md5_gg(b, c, d, a, x[i], 20, -373897302);
+ a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
+ d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
+ c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
+ b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
+ a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
+ d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
+ c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
+ b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
+ a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
+ d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
+ c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
+ b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
+
+ a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
+ d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
+ c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
+ b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
+ a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
+ d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
+ c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
+ b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
+ a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
+ d = md5_hh(d, a, b, c, x[i], 11, -358537222);
+ c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
+ b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
+ a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
+ d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
+ c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
+ b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
+
+ a = md5_ii(a, b, c, d, x[i], 6, -198630844);
+ d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
+ c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
+ b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
+ a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
+ d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
+ c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
+ b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
+ a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
+ d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
+ c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
+ b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
+ a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
+ d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
+ c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
+ b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
+
+ a = safe_add(a, olda);
+ b = safe_add(b, oldb);
+ c = safe_add(c, oldc);
+ d = safe_add(d, oldd);
+ }
+ return [a, b, c, d];
+ }
+
+ /*
+ * Convert an array of little-endian words to a string
+ */
+ function binl2rstr(input) {
+ var i,
+ output = '';
+ for (i = 0; i < input.length * 32; i += 8) {
+ output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
+ }
+ return output;
+ }
+
+ /*
+ * Convert a raw string to an array of little-endian words
+ * Characters >255 have their high-byte silently ignored.
+ */
+ function rstr2binl(input) {
+ var i,
+ output = [];
+ output[(input.length >> 2) - 1] = undefined;
+ for (i = 0; i < output.length; i += 1) {
+ output[i] = 0;
+ }
+ for (i = 0; i < input.length * 8; i += 8) {
+ output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
+ }
+ return output;
+ }
+
+ /*
+ * Calculate the MD5 of a raw string
+ */
+ function rstr_md5(s) {
+ return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
+ }
+
+ /*
+ * Calculate the HMAC-MD5, of a key and some data (raw strings)
+ */
+ function rstr_hmac_md5(key, data) {
+ var i,
+ bkey = rstr2binl(key),
+ ipad = [],
+ opad = [],
+ hash;
+ ipad[15] = opad[15] = undefined;
+ if (bkey.length > 16) {
+ bkey = binl_md5(bkey, key.length * 8);
+ }
+ for (i = 0; i < 16; i += 1) {
+ ipad[i] = bkey[i] ^ 0x36363636;
+ opad[i] = bkey[i] ^ 0x5C5C5C5C;
+ }
+ hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
+ return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
+ }
+
+ /*
+ * Convert a raw string to a hex string
+ */
+ function rstr2hex(input) {
+ var hex_tab = '0123456789abcdef',
+ output = '',
+ x,
+ i;
+ for (i = 0; i < input.length; i += 1) {
+ x = input.charCodeAt(i);
+ output += hex_tab.charAt((x >>> 4) & 0x0F) +
+ hex_tab.charAt(x & 0x0F);
+ }
+ return output;
+ }
+
+ /*
+ * Encode a string as utf-8
+ */
+ function str2rstr_utf8(input) {
+ return unescape(encodeURIComponent(input));
+ }
+
+ /*
+ * Take string arguments and return either raw or hex encoded strings
+ */
+ function raw_md5(s) {
+ return rstr_md5(str2rstr_utf8(s));
+ }
+ function hex_md5(s) {
+ return rstr2hex(raw_md5(s));
+ }
+ function raw_hmac_md5(k, d) {
+ return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d));
+ }
+ function hex_hmac_md5(k, d) {
+ return rstr2hex(raw_hmac_md5(k, d));
+ }
+
+ function md5(string, key, raw) {
+ if (!key) {
+ if (!raw) {
+ return hex_md5(string);
+ }
+ return raw_md5(string);
+ }
+ if (!raw) {
+ return hex_hmac_md5(key, string);
+ }
+ return raw_hmac_md5(key, string);
+ }
+
+ if (typeof define === 'function' && define.amd) {
+ define(function () {
+ return md5;
+ });
+ } else {
+ $.md5 = md5;
+ }
+}(this));
diff --git a/scripts/lib/raf.js b/scripts/lib/raf.js
deleted file mode 100644
index 370894e..0000000
--- a/scripts/lib/raf.js
+++ /dev/null
@@ -1,31 +0,0 @@
-// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
-// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
-
-// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
-
-// MIT license
-
-(function() {
- var lastTime = 0;
- var vendors = ['ms', 'moz', 'webkit', 'o'];
- for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
- window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
- window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
- || window[vendors[x]+'CancelRequestAnimationFrame'];
- }
-
- if (!window.requestAnimationFrame)
- window.requestAnimationFrame = function(callback, element) {
- var currTime = new Date().getTime();
- var timeToCall = Math.max(0, 16 - (currTime - lastTime));
- var id = window.setTimeout(function() { callback(currTime + timeToCall); },
- timeToCall);
- lastTime = currTime + timeToCall;
- return id;
- };
-
- if (!window.cancelAnimationFrame)
- window.cancelAnimationFrame = function(id) {
- clearTimeout(id);
- };
-}());
\ No newline at end of file
diff --git a/scripts/lib/require-2.1.4.js b/scripts/lib/require.js
similarity index 84%
rename from scripts/lib/require-2.1.4.js
rename to scripts/lib/require.js
index 5b26875..5237640 100644
--- a/scripts/lib/require-2.1.4.js
+++ b/scripts/lib/require.js
@@ -1,5 +1,5 @@
/** vim: et:ts=4:sw=4:sts=4
- * @license RequireJS 2.1.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
+ * @license RequireJS 2.1.20 Copyright (c) 2010-2015, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
@@ -12,7 +12,7 @@ var requirejs, require, define;
(function (global) {
var req, s, head, baseElement, dataMain, src,
interactiveScript, currentlyAddingScript, mainScript, subPath,
- version = '2.1.4',
+ version = '2.1.20',
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
jsSuffixRegExp = /\.js$/,
@@ -21,8 +21,7 @@ var requirejs, require, define;
ostring = op.toString,
hasOwn = op.hasOwnProperty,
ap = Array.prototype,
- apsp = ap.splice,
- isBrowser = !!(typeof window !== 'undefined' && navigator && document),
+ isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document),
isWebWorker = !isBrowser && typeof importScripts !== 'undefined',
//PS3 indicates loaded and complete, but need to wait for complete
//specifically. Sequence is 'loading', 'loaded', execution,
@@ -108,7 +107,10 @@ var requirejs, require, define;
if (source) {
eachProp(source, function (value, prop) {
if (force || !hasProp(target, prop)) {
- if (deepStringMixin && typeof value !== 'string') {
+ if (deepStringMixin && typeof value === 'object' && value &&
+ !isArray(value) && !isFunction(value) &&
+ !(value instanceof RegExp)) {
+
if (!target[prop]) {
target[prop] = {};
}
@@ -134,7 +136,11 @@ var requirejs, require, define;
return document.getElementsByTagName('script');
}
- //Allow getting a global that expressed in
+ function defaultOnError(err) {
+ throw err;
+ }
+
+ //Allow getting a global that is expressed in
//dot notation, like 'a.b.c'.
function getGlobal(value) {
if (!value) {
@@ -173,7 +179,7 @@ var requirejs, require, define;
if (typeof requirejs !== 'undefined') {
if (isFunction(requirejs)) {
- //Do not overwrite and existing requirejs instance.
+ //Do not overwrite an existing requirejs instance.
return;
}
cfg = requirejs;
@@ -191,19 +197,27 @@ var requirejs, require, define;
var inCheckLoaded, Module, context, handlers,
checkLoadedTimeoutId,
config = {
+ //Defaults. Do not set a default for map
+ //config to speed up normalize(), which
+ //will run faster if there is no default.
waitSeconds: 7,
baseUrl: './',
paths: {},
+ bundles: {},
pkgs: {},
shim: {},
- map: {},
config: {}
},
registry = {},
+ //registry of just enabled modules, to speed
+ //cycle breaking code when lots of modules
+ //are registered, but not activated.
+ enabledRegistry = {},
undefEvents = {},
defQueue = [],
defined = {},
urlFetched = {},
+ bundlesMap = {},
requireCounter = 1,
unnormalizedCounter = 1;
@@ -218,20 +232,19 @@ var requirejs, require, define;
*/
function trimDots(ary) {
var i, part;
- for (i = 0; ary[i]; i += 1) {
+ for (i = 0; i < ary.length; i++) {
part = ary[i];
if (part === '.') {
ary.splice(i, 1);
i -= 1;
} else if (part === '..') {
- if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
- //End of the line. Keep at least one non-dot
- //path segment at the front so it can be mapped
- //correctly to disk. Otherwise, there is likely
- //no path mapping for a path starting with '..'.
- //This can still fail, but catches the most reasonable
- //uses of ..
- break;
+ // If at the start, or previous value is still ..,
+ // keep them so that when converted to a path it may
+ // still work when converted to a path, even though
+ // as an ID it is less than ideal. In larger point
+ // releases, may be better to just kick out an error.
+ if (i === 0 || (i === 1 && ary[2] === '..') || ary[i - 1] === '..') {
+ continue;
} else if (i > 0) {
ary.splice(i - 1, 2);
i -= 2;
@@ -251,54 +264,45 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName, applyMap) {
- var pkgName, pkgConfig, mapValue, nameParts, i, j, nameSegment,
- foundMap, foundI, foundStarMap, starI,
- baseParts = baseName && baseName.split('/'),
- normalizedBaseParts = baseParts,
+ var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex,
+ foundMap, foundI, foundStarMap, starI, normalizedBaseParts,
+ baseParts = (baseName && baseName.split('/')),
map = config.map,
starMap = map && map['*'];
//Adjust any relative paths.
- if (name && name.charAt(0) === '.') {
- //If have a base name, try to normalize against it,
- //otherwise, assume it is a top-level require that will
- //be relative to baseUrl in the end.
- if (baseName) {
- if (getOwn(config.pkgs, baseName)) {
- //If the baseName is a package name, then just treat it as one
- //name to concat the name with.
- normalizedBaseParts = baseParts = [baseName];
- } else {
- //Convert baseName to array, and lop off the last part,
- //so that . matches that 'directory' and not name of the baseName's
- //module. For instance, baseName of 'one/two/three', maps to
- //'one/two/three.js', but we want the directory, 'one/two' for
- //this normalization.
- normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
- }
+ if (name) {
+ name = name.split('/');
+ lastIndex = name.length - 1;
- name = normalizedBaseParts.concat(name.split('/'));
- trimDots(name);
-
- //Some use of packages may use a . path to reference the
- //'main' module name, so normalize for that.
- pkgConfig = getOwn(config.pkgs, (pkgName = name[0]));
- name = name.join('/');
- if (pkgConfig && name === pkgName + '/' + pkgConfig.main) {
- name = pkgName;
- }
- } else if (name.indexOf('./') === 0) {
- // No baseName, so this is ID is resolved relative
- // to baseUrl, pull off the leading dot.
- name = name.substring(2);
+ // If wanting node ID compatibility, strip .js from end
+ // of IDs. Have to do this here, and not in nameToUrl
+ // because node allows either .js or non .js to map
+ // to same file.
+ if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
+ name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
}
+
+ // Starts with a '.' so need the baseName
+ if (name[0].charAt(0) === '.' && baseParts) {
+ //Convert baseName to array, and lop off the last part,
+ //so that . matches that 'directory' and not name of the baseName's
+ //module. For instance, baseName of 'one/two/three', maps to
+ //'one/two/three.js', but we want the directory, 'one/two' for
+ //this normalization.
+ normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
+ name = normalizedBaseParts.concat(name);
+ }
+
+ trimDots(name);
+ name = name.join('/');
}
//Apply map config if available.
- if (applyMap && (baseParts || starMap) && map) {
+ if (applyMap && map && (baseParts || starMap)) {
nameParts = name.split('/');
- for (i = nameParts.length; i > 0; i -= 1) {
+ outerLoop: for (i = nameParts.length; i > 0; i -= 1) {
nameSegment = nameParts.slice(0, i).join('/');
if (baseParts) {
@@ -315,16 +319,12 @@ var requirejs, require, define;
//Match, update name to the new value.
foundMap = mapValue;
foundI = i;
- break;
+ break outerLoop;
}
}
}
}
- if (foundMap) {
- break;
- }
-
//Check for a star map match, but just hold on to it,
//if there is a shorter segment match later in a matching
//config, then favor over this star map.
@@ -345,7 +345,11 @@ var requirejs, require, define;
}
}
- return name;
+ // If the name points to a package's name, use
+ // the package main instead.
+ pkgMain = getOwn(config.pkgs, name);
+
+ return pkgMain ? pkgMain : name;
}
function removeScript(name) {
@@ -363,12 +367,17 @@ var requirejs, require, define;
function hasPathFallback(id) {
var pathConfig = getOwn(config.paths, id);
if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) {
- removeScript(id);
//Pop off the first array value, since it failed, and
//retry
pathConfig.shift();
context.require.undef(id);
- context.require([id]);
+
+ //Custom require that does not do map translation, since
+ //ID is "absolute", already mapped/resolved.
+ context.makeRequire(null, {
+ skipMap: true
+ })([id]);
+
return true;
}
}
@@ -434,7 +443,16 @@ var requirejs, require, define;
return normalize(name, parentName, applyMap);
});
} else {
- normalizedName = normalize(name, parentName, applyMap);
+ // If nested plugin references, then do not try to
+ // normalize, as it will not normalize correctly. This
+ // places a restriction on resourceIds, and the longer
+ // term solution is not to normalize until plugins are
+ // loaded and all normalizations to allow for async
+ // loading of a loader plugin. But for now, fixes the
+ // common uses. Details in #1131
+ normalizedName = name.indexOf('!') === -1 ?
+ normalize(name, parentName, applyMap) :
+ name;
}
} else {
//A regular module.
@@ -494,7 +512,12 @@ var requirejs, require, define;
fn(defined[id]);
}
} else {
- getModule(depMap).on(name, fn);
+ mod = getModule(depMap);
+ if (mod.error && name === 'error') {
+ fn(mod.error);
+ } else {
+ mod.on(name, fn);
+ }
}
}
@@ -530,11 +553,13 @@ var requirejs, require, define;
function takeGlobalQueue() {
//Push all the globalDefQueue items into the context's defQueue
if (globalDefQueue.length) {
- //Array splice in the values since the context code has a
- //local var ref to defQueue, so cannot just reassign the one
- //on context.
- apsp.apply(defQueue,
- [defQueue.length - 1, 0].concat(globalDefQueue));
+ each(globalDefQueue, function(queueItem) {
+ var id = queueItem[0];
+ if (typeof id === 'string') {
+ context.defQueueMap[id] = true;
+ }
+ defQueue.push(queueItem);
+ });
globalDefQueue = [];
}
}
@@ -551,7 +576,7 @@ var requirejs, require, define;
mod.usingExports = true;
if (mod.map.isDefine) {
if (mod.exports) {
- return mod.exports;
+ return (defined[mod.map.id] = mod.exports);
} else {
return (mod.exports = defined[mod.map.id] = {});
}
@@ -565,9 +590,9 @@ var requirejs, require, define;
id: mod.map.id,
uri: mod.map.url,
config: function () {
- return (config.config && getOwn(config.config, mod.map.id)) || {};
+ return getOwn(config.config, mod.map.id) || {};
},
- exports: defined[mod.map.id]
+ exports: mod.exports || (mod.exports = {})
});
}
}
@@ -576,6 +601,7 @@ var requirejs, require, define;
function cleanRegistry(id) {
//Clean up machinery used for waiting modules.
delete registry[id];
+ delete enabledRegistry[id];
}
function breakCycle(mod, traced, processed) {
@@ -607,7 +633,7 @@ var requirejs, require, define;
}
function checkLoaded() {
- var map, modId, err, usingPathFallback,
+ var err, usingPathFallback,
waitInterval = config.waitSeconds * 1000,
//It is possible to disable the wait interval by using waitSeconds of 0.
expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
@@ -624,9 +650,9 @@ var requirejs, require, define;
inCheckLoaded = true;
//Figure out the state of all the modules.
- eachProp(registry, function (mod) {
- map = mod.map;
- modId = map.id;
+ eachProp(enabledRegistry, function (mod) {
+ var map = mod.map,
+ modId = map.id;
//Skip things that are not enabled or in error state.
if (!mod.enabled) {
@@ -805,7 +831,7 @@ var requirejs, require, define;
},
/**
- * Checks is the module is ready to define itself, and if so,
+ * Checks if the module is ready to define itself, and if so,
* define it.
*/
check: function () {
@@ -820,7 +846,10 @@ var requirejs, require, define;
factory = this.factory;
if (!this.inited) {
- this.fetch();
+ // Only fetch if not already in the defQueue.
+ if (!hasProp(context.defQueueMap, id)) {
+ this.fetch();
+ }
} else if (this.error) {
this.emit('error', this.error);
} else if (!this.defining) {
@@ -833,8 +862,13 @@ var requirejs, require, define;
if (this.depCount < 1 && !this.defined) {
if (isFunction(factory)) {
//If there is an error listener, favor passing
- //to that instead of throwing an error.
- if (this.events.error) {
+ //to that instead of throwing an error. However,
+ //only do it for define()'d modules. require
+ //errbacks should not be called for failures in
+ //their callbacks (#699). However if a global
+ //onError is set, use that.
+ if ((this.events.error && this.map.isDefine) ||
+ req.onError !== defaultOnError) {
try {
exports = context.execCb(id, factory, depExports, exports);
} catch (e) {
@@ -844,17 +878,14 @@ var requirejs, require, define;
exports = context.execCb(id, factory, depExports, exports);
}
- if (this.map.isDefine) {
- //If setting exports via 'module' is in play,
- //favor that over return value and exports. After that,
- //favor a non-undefined return value over exports use.
+ // Favor return value over exports. If node/cjs in play,
+ // then will not have a return value anyway. Favor
+ // module.exports assignment over exports object.
+ if (this.map.isDefine && exports === undefined) {
cjsModule = this.module;
- if (cjsModule &&
- cjsModule.exports !== undefined &&
- //Make sure it is not already the exports value
- cjsModule.exports !== this.exports) {
+ if (cjsModule) {
exports = cjsModule.exports;
- } else if (exports === undefined && this.usingExports) {
+ } else if (this.usingExports) {
//exports already set the defined value.
exports = this.exports;
}
@@ -862,8 +893,8 @@ var requirejs, require, define;
if (err) {
err.requireMap = this.map;
- err.requireModules = [this.map.id];
- err.requireType = 'define';
+ err.requireModules = this.map.isDefine ? [this.map.id] : null;
+ err.requireType = this.map.isDefine ? 'define' : 'require';
return onError((this.error = err));
}
@@ -883,7 +914,7 @@ var requirejs, require, define;
}
//Clean up
- delete registry[id];
+ cleanRegistry(id);
this.defined = true;
}
@@ -914,6 +945,7 @@ var requirejs, require, define;
on(pluginMap, 'defined', bind(this, function (plugin) {
var load, normalizedMap, normalizedMod,
+ bundleId = getOwn(bundlesMap, this.map.id),
name = this.map.name,
parentName = this.map.parentMap ? this.map.parentMap.name : null,
localRequire = context.makeRequire(map.parentMap, {
@@ -959,6 +991,14 @@ var requirejs, require, define;
return;
}
+ //If a paths config, then just load that file instead to
+ //resolve the plugin, as it is built into that paths layer.
+ if (bundleId) {
+ this.map.url = context.nameToUrl(bundleId);
+ this.load();
+ return;
+ }
+
load = bind(this, function (value) {
this.init([], function () { return value; }, null, {
enabled: true
@@ -1049,6 +1089,7 @@ var requirejs, require, define;
},
enable: function () {
+ enabledRegistry[this.map.id] = this;
this.enabled = true;
//Set flag mentioning that the module is enabling,
@@ -1080,12 +1121,22 @@ var requirejs, require, define;
this.depCount += 1;
on(depMap, 'defined', bind(this, function (depExports) {
+ if (this.undefed) {
+ return;
+ }
this.defineDep(i, depExports);
this.check();
}));
if (this.errback) {
- on(depMap, 'error', this.errback);
+ on(depMap, 'error', bind(this, this.errback));
+ } else if (this.events.error) {
+ // No direct errback on this module, but something
+ // else is listening for errors, so be sure to
+ // propagate the error correctly.
+ on(depMap, 'error', bind(this, function(err) {
+ this.emit('error', err);
+ }));
}
}
@@ -1189,13 +1240,15 @@ var requirejs, require, define;
while (defQueue.length) {
args = defQueue.shift();
if (args[0] === null) {
- return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
+ return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' +
+ args[args.length - 1]));
} else {
//args are id, deps, factory. Should be normalized by the
//define() function.
callGetModule(args);
}
}
+ context.defQueueMap = {};
}
context = {
@@ -1205,9 +1258,11 @@ var requirejs, require, define;
defined: defined,
urlFetched: urlFetched,
defQueue: defQueue,
+ defQueueMap: {},
Module: Module,
makeModuleMap: makeModuleMap,
nextTick: req.nextTick,
+ onError: onError,
/**
* Set a configuration for the context.
@@ -1221,28 +1276,38 @@ var requirejs, require, define;
}
}
- //Save off the paths and packages since they require special processing,
+ //Save off the paths since they require special processing,
//they are additive.
- var pkgs = config.pkgs,
- shim = config.shim,
+ var shim = config.shim,
objs = {
paths: true,
+ bundles: true,
config: true,
map: true
};
eachProp(cfg, function (value, prop) {
if (objs[prop]) {
- if (prop === 'map') {
- mixin(config[prop], value, true, true);
- } else {
- mixin(config[prop], value, true);
+ if (!config[prop]) {
+ config[prop] = {};
}
+ mixin(config[prop], value, true, true);
} else {
config[prop] = value;
}
});
+ //Reverse map the bundles
+ if (cfg.bundles) {
+ eachProp(cfg.bundles, function (value, prop) {
+ each(value, function (v) {
+ if (v !== prop) {
+ bundlesMap[v] = prop;
+ }
+ });
+ });
+ }
+
//Merge shim
if (cfg.shim) {
eachProp(cfg.shim, function (value, id) {
@@ -1263,29 +1328,25 @@ var requirejs, require, define;
//Adjust packages if necessary.
if (cfg.packages) {
each(cfg.packages, function (pkgObj) {
- var location;
+ var location, name;
- pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj;
+ pkgObj = typeof pkgObj === 'string' ? {name: pkgObj} : pkgObj;
+
+ name = pkgObj.name;
location = pkgObj.location;
+ if (location) {
+ config.paths[name] = pkgObj.location;
+ }
- //Create a brand new object on pkgs, since currentPackages can
- //be passed in again, and config.pkgs is the internal transformed
- //state for all package configs.
- pkgs[pkgObj.name] = {
- name: pkgObj.name,
- location: location || pkgObj.name,
- //Remove leading dot in main, so main paths are normalized,
- //and remove any trailing .js, since different package
- //envs have different conventions: some use a module name,
- //some use a file name.
- main: (pkgObj.main || 'main')
- .replace(currDirRegExp, '')
- .replace(jsSuffixRegExp, '')
- };
+ //Save pointer to main module ID for pkg name.
+ //Remove leading dot in main, so main paths are normalized,
+ //and remove any trailing .js, since different package
+ //envs have different conventions: some use a module name,
+ //some use a file name.
+ config.pkgs[name] = pkgObj.name + '/' + (pkgObj.main || 'main')
+ .replace(currDirRegExp, '')
+ .replace(jsSuffixRegExp, '');
});
-
- //Done with modifications, assing packages back to context config
- config.pkgs = pkgs;
}
//If there are any "waiting to execute" modules in the registry,
@@ -1296,7 +1357,7 @@ var requirejs, require, define;
//late to modify them, and ignore unnormalized ones
//since they are transient.
if (!mod.inited && !mod.map.unnormalized) {
- mod.map = makeModuleMap(id);
+ mod.map = makeModuleMap(id, null, true);
}
});
@@ -1345,7 +1406,7 @@ var requirejs, require, define;
//Synchronous access to one module. If require.get is
//available (as in the Node adapter), prefer that.
if (req.get) {
- return req.get(context, deps, relMap);
+ return req.get(context, deps, relMap, localRequire);
}
//Normalize module name, if it contains . or ..
@@ -1396,7 +1457,7 @@ var requirejs, require, define;
* plain URLs like nameToUrl.
*/
toUrl: function (moduleNamePlusExt) {
- var ext, url,
+ var ext,
index = moduleNamePlusExt.lastIndexOf('.'),
segment = moduleNamePlusExt.split('/')[0],
isRelative = segment === '.' || segment === '..';
@@ -1408,9 +1469,8 @@ var requirejs, require, define;
moduleNamePlusExt = moduleNamePlusExt.substring(0, index);
}
- url = context.nameToUrl(normalize(moduleNamePlusExt,
- relMap && relMap.id, true), ext || '.fake');
- return ext ? url : url.substring(0, url.length - 5);
+ return context.nameToUrl(normalize(moduleNamePlusExt,
+ relMap && relMap.id, true), ext, true);
},
defined: function (id) {
@@ -1433,10 +1493,23 @@ var requirejs, require, define;
var map = makeModuleMap(id, relMap, true),
mod = getOwn(registry, id);
+ mod.undefed = true;
+ removeScript(id);
+
delete defined[id];
delete urlFetched[map.url];
delete undefEvents[id];
+ //Clean queued defines too. Go backwards
+ //in array so that the splices do not
+ //mess up the iteration.
+ eachReverse(defQueue, function(args, i) {
+ if (args[0] === id) {
+ defQueue.splice(i, 1);
+ }
+ });
+ delete context.defQueueMap[id];
+
if (mod) {
//Hold on to listeners in case the
//module will be attempted to be reloaded
@@ -1456,7 +1529,7 @@ var requirejs, require, define;
/**
* Called to enable a module if it is still in the registry
* awaiting enablement. A second arg, parent, the parent module,
- * is passed in for context, when this method is overriden by
+ * is passed in for context, when this method is overridden by
* the optimizer. Not shown here to keep code compact.
*/
enable: function (depMap) {
@@ -1497,6 +1570,7 @@ var requirejs, require, define;
callGetModule(args);
}
+ context.defQueueMap = {};
//Do this after the cycle of callGetModule in case the result
//of those calls/init calls changes the registry.
@@ -1529,9 +1603,20 @@ var requirejs, require, define;
* it is assumed to have already been normalized. This is an
* internal API, not a public one. Use toUrl for the public API.
*/
- nameToUrl: function (moduleName, ext) {
- var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
- parentPath;
+ nameToUrl: function (moduleName, ext, skipExt) {
+ var paths, syms, i, parentModule, url,
+ parentPath, bundleId,
+ pkgMain = getOwn(config.pkgs, moduleName);
+
+ if (pkgMain) {
+ moduleName = pkgMain;
+ }
+
+ bundleId = getOwn(bundlesMap, moduleName);
+
+ if (bundleId) {
+ return context.nameToUrl(bundleId, ext, skipExt);
+ }
//If a colon is in the URL, it indicates a protocol is used and it is just
//an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?)
@@ -1545,7 +1630,6 @@ var requirejs, require, define;
} else {
//A module that needs to be converted to a path.
paths = config.paths;
- pkgs = config.pkgs;
syms = moduleName.split('/');
//For each module name segment, see if there is a path
@@ -1553,7 +1637,7 @@ var requirejs, require, define;
//and work up from it.
for (i = syms.length; i > 0; i -= 1) {
parentModule = syms.slice(0, i).join('/');
- pkg = getOwn(pkgs, parentModule);
+
parentPath = getOwn(paths, parentModule);
if (parentPath) {
//If an array, it means there are a few choices,
@@ -1563,22 +1647,12 @@ var requirejs, require, define;
}
syms.splice(0, i, parentPath);
break;
- } else if (pkg) {
- //If module name is just the package name, then looking
- //for the main module.
- if (moduleName === pkg.name) {
- pkgPath = pkg.location + '/' + pkg.main;
- } else {
- pkgPath = pkg.location;
- }
- syms.splice(0, i, pkgPath);
- break;
}
}
//Join the path parts together, then figure out if baseUrl is needed.
url = syms.join('/');
- url += (ext || (/\?/.test(url) ? '' : '.js'));
+ url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js'));
url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
}
@@ -1594,7 +1668,7 @@ var requirejs, require, define;
},
/**
- * Executes a module callack function. Broken out as a separate function
+ * Executes a module callback function. Broken out as a separate function
* solely to allow the build system to sequence the files in the built
* layer in the right sequence.
*
@@ -1632,7 +1706,7 @@ var requirejs, require, define;
onScriptError: function (evt) {
var data = getScriptData(evt);
if (!hasPathFallback(data.id)) {
- return onError(makeError('scripterror', 'Script error', evt, [data.id]));
+ return onError(makeError('scripterror', 'Script error for: ' + data.id, evt, [data.id]));
}
}
};
@@ -1761,8 +1835,19 @@ var requirejs, require, define;
* function. Intercept/override it if you want custom error handling.
* @param {Error} err the error object.
*/
- req.onError = function (err) {
- throw err;
+ req.onError = defaultOnError;
+
+ /**
+ * Creates the node for the load command. Only used in browser envs.
+ */
+ req.createNode = function (config, moduleName, url) {
+ var node = config.xhtml ?
+ document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') :
+ document.createElement('script');
+ node.type = config.scriptType || 'text/javascript';
+ node.charset = 'utf-8';
+ node.async = true;
+ return node;
};
/**
@@ -1779,12 +1864,10 @@ var requirejs, require, define;
node;
if (isBrowser) {
//In the browser so use a script tag
- node = config.xhtml ?
- document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') :
- document.createElement('script');
- node.type = config.scriptType || 'text/javascript';
- node.charset = 'utf-8';
- node.async = true;
+ node = req.createNode(config, moduleName, url);
+ if (config.onNodeCreated) {
+ config.onNodeCreated(node, config, moduleName, url);
+ }
node.setAttribute('data-requirecontext', context.contextName);
node.setAttribute('data-requiremodule', moduleName);
@@ -1817,7 +1900,7 @@ var requirejs, require, define;
node.attachEvent('onreadystatechange', context.onScriptLoad);
//It would be great to add an error handler here to catch
//404s in IE9+. However, onreadystatechange will fire before
- //the error handler, so that does not help. If addEvenListener
+ //the error handler, so that does not help. If addEventListener
//is used, then IE will fire error before load, but we cannot
//use that pathway given the connect.microsoft.com issue
//mentioned above about not doing the 'script execute,
@@ -1846,16 +1929,24 @@ var requirejs, require, define;
return node;
} else if (isWebWorker) {
- //In a web worker, use importScripts. This is not a very
- //efficient use of importScripts, importScripts will block until
- //its script is downloaded and evaluated. However, if web workers
- //are in play, the expectation that a build has been done so that
- //only one script needs to be loaded anyway. This may need to be
- //reevaluated if other use cases become common.
- importScripts(url);
+ try {
+ //In a web worker, use importScripts. This is not a very
+ //efficient use of importScripts, importScripts will block until
+ //its script is downloaded and evaluated. However, if web workers
+ //are in play, the expectation that a build has been done so that
+ //only one script needs to be loaded anyway. This may need to be
+ //reevaluated if other use cases become common.
+ importScripts(url);
- //Account for anonymous modules
- context.completeLoad(moduleName);
+ //Account for anonymous modules
+ context.completeLoad(moduleName);
+ } catch (e) {
+ context.onError(makeError('importscripts',
+ 'importScripts failed for ' +
+ moduleName + ' at ' + url,
+ e,
+ [moduleName]));
+ }
}
};
@@ -1873,7 +1964,7 @@ var requirejs, require, define;
}
//Look for a data-main script attribute, which could also adjust the baseUrl.
- if (isBrowser) {
+ if (isBrowser && !cfg.skipDataMain) {
//Figure out baseUrl. Get it from the script tag with require.js in it.
eachReverse(scripts(), function (script) {
//Set the 'head' where we can append children by
@@ -1887,24 +1978,31 @@ var requirejs, require, define;
//baseUrl, if it is not already set.
dataMain = script.getAttribute('data-main');
if (dataMain) {
+ //Preserve dataMain in case it is a path (i.e. contains '?')
+ mainScript = dataMain;
+
//Set final baseUrl if there is not already an explicit one.
if (!cfg.baseUrl) {
//Pull off the directory of data-main for use as the
//baseUrl.
- src = dataMain.split('/');
+ src = mainScript.split('/');
mainScript = src.pop();
subPath = src.length ? src.join('/') + '/' : './';
cfg.baseUrl = subPath;
- dataMain = mainScript;
}
- //Strip off any trailing .js since dataMain is now
+ //Strip off any trailing .js since mainScript is now
//like a module name.
- dataMain = dataMain.replace(jsSuffixRegExp, '');
+ mainScript = mainScript.replace(jsSuffixRegExp, '');
+
+ //If mainScript is still a path, fall back to dataMain
+ if (req.jsExtRegExp.test(mainScript)) {
+ mainScript = dataMain;
+ }
//Put the data-main script in the files to load.
- cfg.deps = cfg.deps ? cfg.deps.concat(dataMain) : [dataMain];
+ cfg.deps = cfg.deps ? cfg.deps.concat(mainScript) : [mainScript];
return true;
}
@@ -1932,12 +2030,13 @@ var requirejs, require, define;
//This module may not have dependencies
if (!isArray(deps)) {
callback = deps;
- deps = [];
+ deps = null;
}
//If no name, and callback is a function, then figure out if it a
//CommonJS thing with dependencies.
- if (!deps.length && isFunction(callback)) {
+ if (!deps && isFunction(callback)) {
+ deps = [];
//Remove comments from the callback string,
//look for require calls, and pull them into the dependencies,
//but only if there are function args.
@@ -1976,14 +2075,18 @@ var requirejs, require, define;
//where the module name is not known until the script onload event
//occurs. If no context, use the global queue, and get it processed
//in the onscript load callback.
- (context ? context.defQueue : globalDefQueue).push([name, deps, callback]);
+ if (context) {
+ context.defQueue.push([name, deps, callback]);
+ context.defQueueMap[name] = true;
+ } else {
+ globalDefQueue.push([name, deps, callback]);
+ }
};
define.amd = {
jQuery: true
};
-
/**
* Executes the text. Normally just uses eval, but can be modified
* to use a better, environment-specific call. Only used for transpiling
diff --git a/scripts/lib/require.min.js b/scripts/lib/require.min.js
deleted file mode 100644
index c221717..0000000
--- a/scripts/lib/require.min.js
+++ /dev/null
@@ -1,2 +0,0 @@
-/* RequireJS 2.1.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. Available via the MIT or new BSD license. * see: http://github.com/jrburke/requirejs for details*/
-var requirejs,require,define;!function(global){function isFunction(a){return"[object Function]"===ostring.call(a)}function isArray(a){return"[object Array]"===ostring.call(a)}function each(a,b){if(a){var c;for(c=0;c-1&&(!a[c]||!b(a[c],c,a));c-=1);}}function hasProp(a,b){return hasOwn.call(a,b)}function getOwn(a,b){return hasProp(a,b)&&a[b]}function eachProp(a,b){var c;for(c in a)if(hasProp(a,c)&&b(a[c],c))break}function mixin(a,b,c,d){return b&&eachProp(b,function(b,e){(c||!hasProp(a,e))&&(d&&"string"!=typeof b?(a[e]||(a[e]={}),mixin(a[e],b,c,d)):a[e]=b)}),a}function bind(a,b){return function(){return b.apply(a,arguments)}}function scripts(){return document.getElementsByTagName("script")}function getGlobal(a){if(!a)return a;var b=global;return each(a.split("."),function(a){b=b[a]}),b}function makeError(a,b,c,d){var e=new Error(b+"\nhttp://requirejs.org/docs/errors.html#"+a);return e.requireType=a,e.requireModules=d,c&&(e.originalError=c),e}function newContext(a){function o(a){var b,c;for(b=0;a[b];b+=1)if(c=a[b],"."===c)a.splice(b,1),b-=1;else if(".."===c){if(1===b&&(".."===a[2]||".."===a[0]))break;b>0&&(a.splice(b-1,2),b-=2)}}function p(a,b,c){var d,e,f,h,i,j,k,l,m,n,p,q=b&&b.split("/"),r=q,s=g.map,t=s&&s["*"];if(a&&"."===a.charAt(0)&&(b?(r=getOwn(g.pkgs,b)?q=[b]:q.slice(0,q.length-1),a=r.concat(a.split("/")),o(a),e=getOwn(g.pkgs,d=a[0]),a=a.join("/"),e&&a===d+"/"+e.main&&(a=d)):0===a.indexOf("./")&&(a=a.substring(2))),c&&(q||t)&&s){for(h=a.split("/"),i=h.length;i>0;i-=1){if(k=h.slice(0,i).join("/"),q)for(j=q.length;j>0;j-=1)if(f=getOwn(s,q.slice(0,j).join("/")),f&&(f=getOwn(f,k))){l=f,m=i;break}if(l)break;!n&&t&&getOwn(t,k)&&(n=getOwn(t,k),p=i)}!l&&n&&(l=n,m=p),l&&(h.splice(0,m,l),a=h.join("/"))}return a}function q(a){isBrowser&&each(scripts(),function(b){return b.getAttribute("data-requiremodule")===a&&b.getAttribute("data-requirecontext")===d.contextName?(b.parentNode.removeChild(b),!0):void 0})}function r(a){var b=getOwn(g.paths,a);return b&&isArray(b)&&b.length>1?(q(a),b.shift(),d.require.undef(a),d.require([a]),!0):void 0}function s(a){var b,c=a?a.indexOf("!"):-1;return c>-1&&(b=a.substring(0,c),a=a.substring(c+1,a.length)),[b,a]}function t(a,b,c,e){var f,g,h,i,j=null,l=b?b.name:null,o=a,q=!0,r="";return a||(q=!1,a="_@r"+(m+=1)),i=s(a),j=i[0],a=i[1],j&&(j=p(j,l,e),g=getOwn(k,j)),a&&(j?r=g&&g.normalize?g.normalize(a,function(a){return p(a,l,e)}):p(a,l,e):(r=p(a,l,e),i=s(r),j=i[0],r=i[1],c=!0,f=d.nameToUrl(r))),h=!j||g||c?"":"_unnormalized"+(n+=1),{prefix:j,name:r,parentMap:b,unnormalized:!!h,url:f,originalName:o,isDefine:q,id:(j?j+"!"+r:r)+h}}function u(a){var b=a.id,c=getOwn(h,b);return c||(c=h[b]=new d.Module(a)),c}function v(a,b,c){var d=a.id,e=getOwn(h,d);!hasProp(k,d)||e&&!e.defineEmitComplete?u(a).on(b,c):"defined"===b&&c(k[d])}function w(a,b){var c=a.requireModules,d=!1;b?b(a):(each(c,function(b){var c=getOwn(h,b);c&&(c.error=a,c.events.error&&(d=!0,c.emit("error",a)))}),d||req.onError(a))}function x(){globalDefQueue.length&&(apsp.apply(j,[j.length-1,0].concat(globalDefQueue)),globalDefQueue=[])}function y(a){delete h[a]}function z(a,b,c){var d=a.map.id;a.error?a.emit("error",a.error):(b[d]=!0,each(a.depMaps,function(d,e){var f=d.id,g=getOwn(h,f);!g||a.depMatched[e]||c[f]||(getOwn(b,f)?(a.defineDep(e,k[f]),a.check()):z(g,b,c))}),c[d]=!0)}function A(){var a,c,e,i,j=1e3*g.waitSeconds,k=j&&d.startTime+j<(new Date).getTime(),l=[],m=[],n=!1,o=!0;if(!b){if(b=!0,eachProp(h,function(b){if(a=b.map,c=a.id,b.enabled&&(a.isDefine||m.push(b),!b.error))if(!b.inited&&k)r(c)?(i=!0,n=!0):(l.push(c),q(c));else if(!b.inited&&b.fetched&&a.isDefine&&(n=!0,!a.prefix))return o=!1}),k&&l.length)return e=makeError("timeout","Load timeout for modules: "+l,null,l),e.contextName=d.contextName,w(e);o&&each(m,function(a){z(a,{},{})}),k&&!i||!n||!isBrowser&&!isWebWorker||f||(f=setTimeout(function(){f=0,A()},50)),b=!1}}function B(a){hasProp(k,a[0])||u(t(a[0],null,!0)).init(a[1],a[2])}function C(a,b,c,d){a.detachEvent&&!isOpera?d&&a.detachEvent(d,b):a.removeEventListener(c,b,!1)}function D(a){var b=a.currentTarget||a.srcElement;return C(b,d.onScriptLoad,"load","onreadystatechange"),C(b,d.onScriptError,"error"),{node:b,id:b&&b.getAttribute("data-requiremodule")}}function E(){var a;for(x();j.length;){if(a=j.shift(),null===a[0])return w(makeError("mismatch","Mismatched anonymous define() module: "+a[a.length-1]));B(a)}}var b,c,d,e,f,g={waitSeconds:7,baseUrl:"./",paths:{},pkgs:{},shim:{},map:{},config:{}},h={},i={},j=[],k={},l={},m=1,n=1;return e={require:function(a){return a.require?a.require:a.require=d.makeRequire(a.map)},exports:function(a){return a.usingExports=!0,a.map.isDefine?a.exports?a.exports:a.exports=k[a.map.id]={}:void 0},module:function(a){return a.module?a.module:a.module={id:a.map.id,uri:a.map.url,config:function(){return g.config&&getOwn(g.config,a.map.id)||{}},exports:k[a.map.id]}}},c=function(a){this.events=getOwn(i,a.id)||{},this.map=a,this.shim=getOwn(g.shim,a.id),this.depExports=[],this.depMaps=[],this.depMatched=[],this.pluginMaps={},this.depCount=0},c.prototype={init:function(a,b,c,d){d=d||{},this.inited||(this.factory=b,c?this.on("error",c):this.events.error&&(c=bind(this,function(a){this.emit("error",a)})),this.depMaps=a&&a.slice(0),this.errback=c,this.inited=!0,this.ignore=d.ignore,d.enabled||this.enabled?this.enable():this.check())},defineDep:function(a,b){this.depMatched[a]||(this.depMatched[a]=!0,this.depCount-=1,this.depExports[a]=b)},fetch:function(){if(!this.fetched){this.fetched=!0,d.startTime=(new Date).getTime();var a=this.map;return this.shim?(d.makeRequire(this.map,{enableBuildCallback:!0})(this.shim.deps||[],bind(this,function(){return a.prefix?this.callPlugin():this.load()})),void 0):a.prefix?this.callPlugin():this.load()}},load:function(){var a=this.map.url;l[a]||(l[a]=!0,d.load(this.map.id,a))},check:function(){if(this.enabled&&!this.enabling){var a,b,c=this.map.id,e=this.depExports,f=this.exports,g=this.factory;if(this.inited){if(this.error)this.emit("error",this.error);else if(!this.defining){if(this.defining=!0,this.depCount<1&&!this.defined){if(isFunction(g)){if(this.events.error)try{f=d.execCb(c,g,e,f)}catch(i){a=i}else f=d.execCb(c,g,e,f);if(this.map.isDefine&&(b=this.module,b&&void 0!==b.exports&&b.exports!==this.exports?f=b.exports:void 0===f&&this.usingExports&&(f=this.exports)),a)return a.requireMap=this.map,a.requireModules=[this.map.id],a.requireType="define",w(this.error=a)}else f=g;this.exports=f,this.map.isDefine&&!this.ignore&&(k[c]=f,req.onResourceLoad&&req.onResourceLoad(d,this.map,this.depMaps)),delete h[c],this.defined=!0}this.defining=!1,this.defined&&!this.defineEmitted&&(this.defineEmitted=!0,this.emit("defined",this.exports),this.defineEmitComplete=!0)}}else this.fetch()}},callPlugin:function(){var a=this.map,b=a.id,c=t(a.prefix);this.depMaps.push(c),v(c,"defined",bind(this,function(c){var e,f,i,j=this.map.name,k=this.map.parentMap?this.map.parentMap.name:null,l=d.makeRequire(a.parentMap,{enableBuildCallback:!0});return this.map.unnormalized?(c.normalize&&(j=c.normalize(j,function(a){return p(a,k,!0)})||""),f=t(a.prefix+"!"+j,this.map.parentMap),v(f,"defined",bind(this,function(a){this.init([],function(){return a},null,{enabled:!0,ignore:!0})})),i=getOwn(h,f.id),i&&(this.depMaps.push(f),this.events.error&&i.on("error",bind(this,function(a){this.emit("error",a)})),i.enable()),void 0):(e=bind(this,function(a){this.init([],function(){return a},null,{enabled:!0})}),e.error=bind(this,function(a){this.inited=!0,this.error=a,a.requireModules=[b],eachProp(h,function(a){0===a.map.id.indexOf(b+"_unnormalized")&&y(a.map.id)}),w(a)}),e.fromText=bind(this,function(c,f){var h=a.name,i=t(h),j=useInteractive;f&&(c=f),j&&(useInteractive=!1),u(i),hasProp(g.config,b)&&(g.config[h]=g.config[b]);try{req.exec(c)}catch(k){return w(makeError("fromtexteval","fromText eval for "+b+" failed: "+k,k,[b]))}j&&(useInteractive=!0),this.depMaps.push(i),d.completeLoad(h),l([h],e)}),c.load(a.name,l,e,g),void 0)})),d.enable(c,this),this.pluginMaps[c.id]=c},enable:function(){this.enabled=!0,this.enabling=!0,each(this.depMaps,bind(this,function(a,b){var c,f,g;if("string"==typeof a){if(a=t(a,this.map.isDefine?this.map:this.map.parentMap,!1,!this.skipMap),this.depMaps[b]=a,g=getOwn(e,a.id))return this.depExports[b]=g(this),void 0;this.depCount+=1,v(a,"defined",bind(this,function(a){this.defineDep(b,a),this.check()})),this.errback&&v(a,"error",this.errback)}c=a.id,f=h[c],hasProp(e,c)||!f||f.enabled||d.enable(a,this)})),eachProp(this.pluginMaps,bind(this,function(a){var b=getOwn(h,a.id);b&&!b.enabled&&d.enable(a,this)})),this.enabling=!1,this.check()},on:function(a,b){var c=this.events[a];c||(c=this.events[a]=[]),c.push(b)},emit:function(a,b){each(this.events[a],function(a){a(b)}),"error"===a&&delete this.events[a]}},d={config:g,contextName:a,registry:h,defined:k,urlFetched:l,defQueue:j,Module:c,makeModuleMap:t,nextTick:req.nextTick,configure:function(a){a.baseUrl&&"/"!==a.baseUrl.charAt(a.baseUrl.length-1)&&(a.baseUrl+="/");var b=g.pkgs,c=g.shim,e={paths:!0,config:!0,map:!0};eachProp(a,function(a,b){e[b]?"map"===b?mixin(g[b],a,!0,!0):mixin(g[b],a,!0):g[b]=a}),a.shim&&(eachProp(a.shim,function(a,b){isArray(a)&&(a={deps:a}),!a.exports&&!a.init||a.exportsFn||(a.exportsFn=d.makeShimExports(a)),c[b]=a}),g.shim=c),a.packages&&(each(a.packages,function(a){var c;a="string"==typeof a?{name:a}:a,c=a.location,b[a.name]={name:a.name,location:c||a.name,main:(a.main||"main").replace(currDirRegExp,"").replace(jsSuffixRegExp,"")}}),g.pkgs=b),eachProp(h,function(a,b){a.inited||a.map.unnormalized||(a.map=t(b))}),(a.deps||a.callback)&&d.require(a.deps||[],a.callback)},makeShimExports:function(a){function b(){var b;return a.init&&(b=a.init.apply(global,arguments)),b||a.exports&&getGlobal(a.exports)}return b},makeRequire:function(b,c){function f(g,i,j){var l,m,n;return c.enableBuildCallback&&i&&isFunction(i)&&(i.__requireJsBuild=!0),"string"==typeof g?isFunction(i)?w(makeError("requireargs","Invalid require call"),j):b&&hasProp(e,g)?e[g](h[b.id]):req.get?req.get(d,g,b):(m=t(g,b,!1,!0),l=m.id,hasProp(k,l)?k[l]:w(makeError("notloaded",'Module name "'+l+'" has not been loaded yet for context: '+a+(b?"":". Use require([])")))):(E(),d.nextTick(function(){E(),n=u(t(null,b)),n.skipMap=c.skipMap,n.init(g,i,j,{enabled:!0}),A()}),f)}return c=c||{},mixin(f,{isBrowser:isBrowser,toUrl:function(a){var c,e,f=a.lastIndexOf("."),g=a.split("/")[0],h="."===g||".."===g;return-1!==f&&(!h||f>1)&&(c=a.substring(f,a.length),a=a.substring(0,f)),e=d.nameToUrl(p(a,b&&b.id,!0),c||".fake"),c?e:e.substring(0,e.length-5)},defined:function(a){return hasProp(k,t(a,b,!1,!0).id)},specified:function(a){return a=t(a,b,!1,!0).id,hasProp(k,a)||hasProp(h,a)}}),b||(f.undef=function(a){x();var c=t(a,b,!0),d=getOwn(h,a);delete k[a],delete l[c.url],delete i[a],d&&(d.events.defined&&(i[a]=d.events),y(a))}),f},enable:function(a){var b=getOwn(h,a.id);b&&u(a).enable()},completeLoad:function(a){var b,c,d,e=getOwn(g.shim,a)||{},f=e.exports;for(x();j.length;){if(c=j.shift(),null===c[0]){if(c[0]=a,b)break;b=!0}else c[0]===a&&(b=!0);B(c)}if(d=getOwn(h,a),!b&&!hasProp(k,a)&&d&&!d.inited){if(!(!g.enforceDefine||f&&getGlobal(f)))return r(a)?void 0:w(makeError("nodefine","No define call for "+a,null,[a]));B([a,e.deps||[],e.exportsFn])}A()},nameToUrl:function(a,b){var c,d,e,f,h,i,j,k,l;if(req.jsExtRegExp.test(a))k=a+(b||"");else{for(c=g.paths,d=g.pkgs,h=a.split("/"),i=h.length;i>0;i-=1){if(j=h.slice(0,i).join("/"),e=getOwn(d,j),l=getOwn(c,j)){isArray(l)&&(l=l[0]),h.splice(0,i,l);break}if(e){f=a===e.name?e.location+"/"+e.main:e.location,h.splice(0,i,f);break}}k=h.join("/"),k+=b||(/\?/.test(k)?"":".js"),k=("/"===k.charAt(0)||k.match(/^[\w\+\.\-]+:/)?"":g.baseUrl)+k}return g.urlArgs?k+((-1===k.indexOf("?")?"?":"&")+g.urlArgs):k},load:function(a,b){req.load(d,a,b)},execCb:function(a,b,c,d){return b.apply(d,c)},onScriptLoad:function(a){if("load"===a.type||readyRegExp.test((a.currentTarget||a.srcElement).readyState)){interactiveScript=null;var b=D(a);d.completeLoad(b.id)}},onScriptError:function(a){var b=D(a);return r(b.id)?void 0:w(makeError("scripterror","Script error",a,[b.id]))}},d.require=d.makeRequire(),d}function getInteractiveScript(){return interactiveScript&&"interactive"===interactiveScript.readyState?interactiveScript:(eachReverse(scripts(),function(a){return"interactive"===a.readyState?interactiveScript=a:void 0}),interactiveScript)}var req,s,head,baseElement,dataMain,src,interactiveScript,currentlyAddingScript,mainScript,subPath,version="2.1.4",commentRegExp=/(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/gm,cjsRequireRegExp=/[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,jsSuffixRegExp=/\.js$/,currDirRegExp=/^\.\//,op=Object.prototype,ostring=op.toString,hasOwn=op.hasOwnProperty,ap=Array.prototype,apsp=ap.splice,isBrowser=!("undefined"==typeof window||!navigator||!document),isWebWorker=!isBrowser&&"undefined"!=typeof importScripts,readyRegExp=isBrowser&&"PLAYSTATION 3"===navigator.platform?/^complete$/:/^(complete|loaded)$/,defContextName="_",isOpera="undefined"!=typeof opera&&"[object Opera]"===opera.toString(),contexts={},cfg={},globalDefQueue=[],useInteractive=!1;if("undefined"==typeof define){if("undefined"!=typeof requirejs){if(isFunction(requirejs))return;cfg=requirejs,requirejs=void 0}"undefined"==typeof require||isFunction(require)||(cfg=require,require=void 0),req=requirejs=function(a,b,c,d){var e,f,g=defContextName;return isArray(a)||"string"==typeof a||(f=a,isArray(b)?(a=b,b=c,c=d):a=[]),f&&f.context&&(g=f.context),e=getOwn(contexts,g),e||(e=contexts[g]=req.s.newContext(g)),f&&e.configure(f),e.require(a,b,c)},req.config=function(a){return req(a)},req.nextTick="undefined"!=typeof setTimeout?function(a){setTimeout(a,4)}:function(a){a()},require||(require=req),req.version=version,req.jsExtRegExp=/^\/|:|\?|\.js$/,req.isBrowser=isBrowser,s=req.s={contexts:contexts,newContext:newContext},req({}),each(["toUrl","undef","defined","specified"],function(a){req[a]=function(){var b=contexts[defContextName];return b.require[a].apply(b,arguments)}}),isBrowser&&(head=s.head=document.getElementsByTagName("head")[0],baseElement=document.getElementsByTagName("base")[0],baseElement&&(head=s.head=baseElement.parentNode)),req.onError=function(a){throw a},req.load=function(a,b,c){var e,d=a&&a.config||{};return isBrowser?(e=d.xhtml?document.createElementNS("http://www.w3.org/1999/xhtml","html:script"):document.createElement("script"),e.type=d.scriptType||"text/javascript",e.charset="utf-8",e.async=!0,e.setAttribute("data-requirecontext",a.contextName),e.setAttribute("data-requiremodule",b),!e.attachEvent||e.attachEvent.toString&&e.attachEvent.toString().indexOf("[native code")<0||isOpera?(e.addEventListener("load",a.onScriptLoad,!1),e.addEventListener("error",a.onScriptError,!1)):(useInteractive=!0,e.attachEvent("onreadystatechange",a.onScriptLoad)),e.src=c,currentlyAddingScript=e,baseElement?head.insertBefore(e,baseElement):head.appendChild(e),currentlyAddingScript=null,e):(isWebWorker&&(importScripts(c),a.completeLoad(b)),void 0)},isBrowser&&eachReverse(scripts(),function(a){return head||(head=a.parentNode),dataMain=a.getAttribute("data-main"),dataMain?(cfg.baseUrl||(src=dataMain.split("/"),mainScript=src.pop(),subPath=src.length?src.join("/")+"/":"./",cfg.baseUrl=subPath,dataMain=mainScript),dataMain=dataMain.replace(jsSuffixRegExp,""),cfg.deps=cfg.deps?cfg.deps.concat(dataMain):[dataMain],!0):void 0}),define=function(a,b,c){var d,e;"string"!=typeof a&&(c=b,b=a,a=null),isArray(b)||(c=b,b=[]),!b.length&&isFunction(c)&&c.length&&(c.toString().replace(commentRegExp,"").replace(cjsRequireRegExp,function(a,c){b.push(c)}),b=(1===c.length?["require"]:["require","exports","module"]).concat(b)),useInteractive&&(d=currentlyAddingScript||getInteractiveScript(),d&&(a||(a=d.getAttribute("data-requiremodule")),e=contexts[d.getAttribute("data-requirecontext")])),(e?e.defQueue:globalDefQueue).push([a,b,c])},define.amd={jQuery:!0},req.exec=function(text){return eval(text)},req(cfg)}}(this);
\ No newline at end of file
diff --git a/scripts/lib/reqwest.js b/scripts/lib/reqwest.js
index de7d8da..4def3bc 100644
--- a/scripts/lib/reqwest.js
+++ b/scripts/lib/reqwest.js
@@ -1,17 +1,39 @@
+/*!
+ * Reqwest! A general purpose XHR connection manager
+ * license MIT (c) Dustin Diaz 2015
+ * https://github.com/ded/reqwest
+ */
+
!function (name, context, definition) {
if (typeof module != 'undefined' && module.exports) module.exports = definition()
else if (typeof define == 'function' && define.amd) define(definition)
else context[name] = definition()
}('reqwest', this, function () {
- var win = window
- , doc = document
- , twoHundo = /^20\d$/
- , byTag = 'getElementsByTagName'
+ var context = this
+
+ if ('window' in context) {
+ var doc = document
+ , byTag = 'getElementsByTagName'
+ , head = doc[byTag]('head')[0]
+ } else {
+ var XHR2
+ try {
+ // prevent browserify including xhr2
+ var xhr2 = 'xhr2'
+ XHR2 = require(xhr2)
+ } catch (ex) {
+ throw new Error('Peer dependency `xhr2` required! Please npm install xhr2')
+ }
+ }
+
+
+ var httpsRe = /^http/
+ , protocolRe = /(^\w+):\/\//
+ , twoHundo = /^(20\d|1223)$/ //http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
, readyState = 'readyState'
, contentType = 'Content-Type'
, requestedWith = 'X-Requested-With'
- , head = doc[byTag]('head')[0]
, uniqid = 0
, callbackPrefix = 'reqwest_' + (+new Date())
, lastValue // data stored by the most recent JSONP callback
@@ -26,31 +48,33 @@
}
, defaultHeaders = {
- contentType: 'application/x-www-form-urlencoded'
- , requestedWith: xmlHttpRequest
- , accept: {
+ 'contentType': 'application/x-www-form-urlencoded'
+ , 'requestedWith': xmlHttpRequest
+ , 'accept': {
'*': 'text/javascript, text/html, application/xml, text/xml, */*'
- , xml: 'application/xml, text/xml'
- , html: 'text/html'
- , text: 'text/plain'
- , json: 'application/json, text/javascript'
- , js: 'application/javascript, text/javascript'
+ , 'xml': 'application/xml, text/xml'
+ , 'html': 'text/html'
+ , 'text': 'text/plain'
+ , 'json': 'application/json, text/javascript'
+ , 'js': 'application/javascript, text/javascript'
}
}
, xhr = function(o) {
// is it x-domain
- if (o.crossOrigin === true) {
- var xhr = win[xmlHttpRequest] ? new XMLHttpRequest() : null
+ if (o['crossOrigin'] === true) {
+ var xhr = context[xmlHttpRequest] ? new XMLHttpRequest() : null
if (xhr && 'withCredentials' in xhr) {
return xhr
- } else if (win[xDomainRequest]) {
+ } else if (context[xDomainRequest]) {
return new XDomainRequest()
} else {
throw new Error('Browser does not support cross-origin requests')
}
- } else if (win[xmlHttpRequest]) {
+ } else if (context[xmlHttpRequest]) {
return new XMLHttpRequest()
+ } else if (XHR2) {
+ return new XHR2()
} else {
return new ActiveXObject('Microsoft.XMLHTTP')
}
@@ -61,15 +85,21 @@
}
}
+ function succeed(r) {
+ var protocol = protocolRe.exec(r.url)
+ protocol = (protocol && protocol[1]) || context.location.protocol
+ return httpsRe.test(protocol) ? twoHundo.test(r.request.status) : !!r.request.response
+ }
+
function handleReadyState(r, success, error) {
return function () {
// use _aborted to mitigate against IE err c00c023f
// (can't read props on aborted request objects)
if (r._aborted) return error(r.request)
+ if (r._timedOut) return error(r.request, 'Request is aborted: timeout')
if (r.request && r.request[readyState] == 4) {
r.request.onreadystatechange = noop
- if (twoHundo.test(r.request.status))
- success(r.request)
+ if (succeed(r)) success(r.request)
else
error(r.request)
}
@@ -77,23 +107,24 @@
}
function setHeaders(http, o) {
- var headers = o.headers || {}
+ var headers = o['headers'] || {}
, h
- headers.Accept = headers.Accept
- || defaultHeaders.accept[o.type]
- || defaultHeaders.accept['*']
+ headers['Accept'] = headers['Accept']
+ || defaultHeaders['accept'][o['type']]
+ || defaultHeaders['accept']['*']
+ var isAFormData = typeof FormData === 'function' && (o['data'] instanceof FormData);
// breaks cross-origin requests with legacy browsers
- if (!o.crossOrigin && !headers[requestedWith]) headers[requestedWith] = defaultHeaders.requestedWith
- if (!headers[contentType]) headers[contentType] = o.contentType || defaultHeaders.contentType
+ if (!o['crossOrigin'] && !headers[requestedWith]) headers[requestedWith] = defaultHeaders['requestedWith']
+ if (!headers[contentType] && !isAFormData) headers[contentType] = o['contentType'] || defaultHeaders['contentType']
for (h in headers)
headers.hasOwnProperty(h) && 'setRequestHeader' in http && http.setRequestHeader(h, headers[h])
}
function setCredentials(http, o) {
- if (typeof o.withCredentials !== 'undefined' && typeof http.withCredentials !== 'undefined') {
- http.withCredentials = !!o.withCredentials
+ if (typeof o['withCredentials'] !== 'undefined' && typeof http.withCredentials !== 'undefined') {
+ http.withCredentials = !!o['withCredentials']
}
}
@@ -107,9 +138,8 @@
function handleJsonp(o, fn, err, url) {
var reqId = uniqid++
- , cbkey = o.jsonpCallback || 'callback' // the 'callback' key
- , cbval = o.jsonpCallbackName || reqwest.getcallbackPrefix(reqId)
- // , cbval = o.jsonpCallbackName || ('reqwest_' + reqId) // the 'callback' value
+ , cbkey = o['jsonpCallback'] || 'callback' // the 'callback' key
+ , cbval = o['jsonpCallbackName'] || reqwest.getcallbackPrefix(reqId)
, cbreg = new RegExp('((^|\\?|&)' + cbkey + ')=([^&]+)')
, match = url.match(cbreg)
, script = doc.createElement('script')
@@ -126,7 +156,7 @@
url = urlappend(url, cbkey + '=' + cbval) // no callback details, add 'em
}
- win[cbval] = generalCallback
+ context[cbval] = generalCallback
script.type = 'text/javascript'
script.src = url
@@ -135,9 +165,6 @@
// need this for IE due to out-of-order onreadystatechange(), binding script
// execution to an event listener gives us control over when the script
// is executed. See http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
- //
- // if this hack is used in IE10 jsonp callback are never called
- script.event = 'onclick'
script.htmlFor = script.id = '_reqwest_' + reqId
}
@@ -171,29 +198,32 @@
function getRequest(fn, err) {
var o = this.o
- , method = (o.method || 'GET').toUpperCase()
- , url = typeof o === 'string' ? o : o.url
- // convert non-string objects to query-string form unless o.processData is false
- , data = (o.processData !== false && o.data && typeof o.data !== 'string')
- ? reqwest.toQueryString(o.data)
- : (o.data || null)
+ , method = (o['method'] || 'GET').toUpperCase()
+ , url = typeof o === 'string' ? o : o['url']
+ // convert non-string objects to query-string form unless o['processData'] is false
+ , data = (o['processData'] !== false && o['data'] && typeof o['data'] !== 'string')
+ ? reqwest.toQueryString(o['data'])
+ : (o['data'] || null)
, http
, sendWait = false
// if we're working on a GET request and we have data then we should append
// query string to end of URL and not post data
- if ((o.type == 'jsonp' || method == 'GET') && data) {
+ if ((o['type'] == 'jsonp' || method == 'GET') && data) {
url = urlappend(url, data)
data = null
}
- if (o.type == 'jsonp') return handleJsonp(o, fn, err, url)
+ if (o['type'] == 'jsonp') return handleJsonp(o, fn, err, url)
- http = xhr(o)
- http.open(method, url, o.async === false ? false : true)
+ // get the xhr from the factory if passed
+ // if the factory returns null, fall-back to ours
+ http = (o.xhr && o.xhr(o)) || xhr(o)
+
+ http.open(method, url, o['async'] === false ? false : true)
setHeaders(http, o)
setCredentials(http, o)
- if (win[xDomainRequest] && http instanceof win[xDomainRequest]) {
+ if (context[xDomainRequest] && http instanceof context[xDomainRequest]) {
http.onload = fn
http.onerror = err
// NOTE: see
@@ -203,7 +233,7 @@
} else {
http.onreadystatechange = handleReadyState(this, fn, err)
}
- o.before && o.before(http)
+ o['before'] && o['before'](http)
if (sendWait) {
setTimeout(function () {
http.send(data)
@@ -221,14 +251,18 @@
init.apply(this, arguments)
}
- function setType(url) {
- var m = url.match(/\.(json|jsonp|html|xml)(\?|$)/)
- return m ? m[1] : 'js'
+ function setType(header) {
+ // json, javascript, text/plain, text/html, xml
+ if (header === null) return undefined; //In case of no content-type.
+ if (header.match('json')) return 'json'
+ if (header.match('javascript')) return 'js'
+ if (header.match('text')) return 'html'
+ if (header.match('xml')) return 'xml'
}
function init(o, fn) {
- this.url = typeof o == 'string' ? o : o.url
+ this.url = typeof o == 'string' ? o : o['url']
this.timeout = null
// whether request has been fulfilled for purpose
@@ -245,36 +279,35 @@
this._responseArgs = {}
var self = this
- , type = o.type || setType(this.url)
fn = fn || function () {}
- if (o.timeout) {
+ if (o['timeout']) {
this.timeout = setTimeout(function () {
- self.abort()
- }, o.timeout)
+ timedOut()
+ }, o['timeout'])
}
- if (o.success) {
+ if (o['success']) {
this._successHandler = function () {
- o.success.apply(o, arguments)
+ o['success'].apply(o, arguments)
}
}
- if (o.error) {
+ if (o['error']) {
this._errorHandlers.push(function () {
- o.error.apply(o, arguments)
+ o['error'].apply(o, arguments)
})
}
- if (o.complete) {
+ if (o['complete']) {
this._completeHandlers.push(function () {
- o.complete.apply(o, arguments)
+ o['complete'].apply(o, arguments)
})
}
function complete (resp) {
- o.timeout && clearTimeout(self.timeout)
+ o['timeout'] && clearTimeout(self.timeout)
self.timeout = null
while (self._completeHandlers.length > 0) {
self._completeHandlers.shift()(resp)
@@ -282,6 +315,7 @@
}
function success (resp) {
+ var type = o['type'] || resp && setType(resp.getResponseHeader('Content-Type')) // resp can be undefined in IE
resp = (type !== 'jsonp') ? self.request : resp
// use global data filter on response text
var filteredResponse = globalSetupOptions.dataFilter(resp.responseText, type)
@@ -295,7 +329,7 @@
switch (type) {
case 'json':
try {
- resp = win.JSON ? win.JSON.parse(r) : eval('(' + r + ')')
+ resp = context.JSON ? context.JSON.parse(r) : eval('(' + r + ')')
} catch (err) {
return error(resp, 'Could not parse JSON in response', err)
}
@@ -328,6 +362,11 @@
complete(resp)
}
+ function timedOut() {
+ self._timedOut = true
+ self.request.abort()
+ }
+
function error(resp, msg, t) {
resp = self.request
self._responseArgs.resp = resp
@@ -398,6 +437,9 @@
}
return this
}
+ , 'catch': function (fn) {
+ return this.fail(fn)
+ }
}
function reqwest(o, fn) {
@@ -415,8 +457,8 @@
, optCb = function (o) {
// IE gives value="" even where there is no value attribute
// 'specified' ref: http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-862529273
- if (o && !o.disabled)
- cb(n, normalize(o.attributes.value && o.attributes.value.specified ? o.value : o.text))
+ if (o && !o['disabled'])
+ cb(n, normalize(o['attributes']['value'] && o['attributes']['value']['specified'] ? o['value'] : o['text']))
}
, ch, ra, val, i
@@ -523,12 +565,12 @@
}
// If an array was passed in, assume that it is an array of form elements.
if (isArray(o)) {
- for (i = 0; o && i < o.length; i++) add(o[i].name, o[i].value)
+ for (i = 0; o && i < o.length; i++) add(o[i]['name'], o[i]['value'])
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for (prefix in o) {
- buildParams(prefix, o[prefix], traditional, add)
+ if (o.hasOwnProperty(prefix)) buildParams(prefix, o[prefix], traditional, add)
}
}
@@ -569,22 +611,22 @@
// jQuery and Zepto compatibility, differences can be remapped here so you can call
// .ajax.compat(options, callback)
- reqwest.compat = function (o, fn) {
- if (o) {
- o.type && (o.method = o.type) && delete o.type
- o.dataType && (o.type = o.dataType)
- o.jsonpCallback && (o.jsonpCallbackName = o.jsonpCallback) && delete o.jsonpCallback
- o.jsonp && (o.jsonpCallback = o.jsonp)
- }
- return new Reqwest(o, fn)
- }
+ // reqwest.compat = function (o, fn) {
+ // if (o) {
+ // o['type'] && (o['method'] = o['type']) && delete o['type']
+ // o['dataType'] && (o['type'] = o['dataType'])
+ // o['jsonpCallback'] && (o['jsonpCallbackName'] = o['jsonpCallback']) && delete o['jsonpCallback']
+ // o['jsonp'] && (o['jsonpCallback'] = o['jsonp'])
+ // }
+ // return new Reqwest(o, fn)
+ // }
- reqwest.ajaxSetup = function (options) {
- options = options || {}
- for (var k in options) {
- globalSetupOptions[k] = options[k]
- }
- }
+ // reqwest.ajaxSetup = function (options) {
+ // options = options || {}
+ // for (var k in options) {
+ // globalSetupOptions[k] = options[k]
+ // }
+ // }
return reqwest
-});
\ No newline at end of file
+});
diff --git a/scripts/lib/signals-1.0.0.js b/scripts/lib/signals-1.0.0.js
deleted file mode 100755
index 2615d26..0000000
--- a/scripts/lib/signals-1.0.0.js
+++ /dev/null
@@ -1,445 +0,0 @@
-/*jslint onevar:true, undef:true, newcap:true, regexp:true, bitwise:true, maxerr:50, indent:4, white:false, nomen:false, plusplus:false */
-/*global define:false, require:false, exports:false, module:false, signals:false */
-
-/** @license
- * JS Signals
- * Released under the MIT license
- * Author: Miller Medeiros
- * Version: 1.0.0 - Build: 268 (2012/11/29 05:48 PM)
- */
-
-(function(global){
-
- // SignalBinding -------------------------------------------------
- //================================================================
-
- /**
- * Object that represents a binding between a Signal and a listener function.
- * - This is an internal constructor and shouldn't be called by regular users.
- * - inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
- * @author Miller Medeiros
- * @constructor
- * @internal
- * @name SignalBinding
- * @param {Signal} signal Reference to Signal object that listener is currently bound to.
- * @param {Function} listener Handler function bound to the signal.
- * @param {boolean} isOnce If binding should be executed just once.
- * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
- * @param {Number} [priority] The priority level of the event listener. (default = 0).
- */
- function SignalBinding(signal, listener, isOnce, listenerContext, priority) {
-
- /**
- * Handler function bound to the signal.
- * @type Function
- * @private
- */
- this._listener = listener;
-
- /**
- * If binding should be executed just once.
- * @type boolean
- * @private
- */
- this._isOnce = isOnce;
-
- /**
- * Context on which listener will be executed (object that should represent the `this` variable inside listener function).
- * @memberOf SignalBinding.prototype
- * @name context
- * @type Object|undefined|null
- */
- this.context = listenerContext;
-
- /**
- * Reference to Signal object that listener is currently bound to.
- * @type Signal
- * @private
- */
- this._signal = signal;
-
- /**
- * Listener priority
- * @type Number
- * @private
- */
- this._priority = priority || 0;
- }
-
- SignalBinding.prototype = {
-
- /**
- * If binding is active and should be executed.
- * @type boolean
- */
- active : true,
-
- /**
- * Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute`. (curried parameters)
- * @type Array|null
- */
- params : null,
-
- /**
- * Call listener passing arbitrary parameters.
- *
If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.
- * @param {Array} [paramsArr] Array of parameters that should be passed to the listener
- * @return {*} Value returned by the listener.
- */
- execute : function (paramsArr) {
- var handlerReturn, params;
- if (this.active && !!this._listener) {
- params = this.params? this.params.concat(paramsArr) : paramsArr;
- handlerReturn = this._listener.apply(this.context, params);
- if (this._isOnce) {
- this.detach();
- }
- }
- return handlerReturn;
- },
-
- /**
- * Detach binding from signal.
- * - alias to: mySignal.remove(myBinding.getListener());
- * @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
- */
- detach : function () {
- return this.isBound()? this._signal.remove(this._listener, this.context) : null;
- },
-
- /**
- * @return {Boolean} `true` if binding is still bound to the signal and have a listener.
- */
- isBound : function () {
- return (!!this._signal && !!this._listener);
- },
-
- /**
- * @return {boolean} If SignalBinding will only be executed once.
- */
- isOnce : function () {
- return this._isOnce;
- },
-
- /**
- * @return {Function} Handler function bound to the signal.
- */
- getListener : function () {
- return this._listener;
- },
-
- /**
- * @return {Signal} Signal that listener is currently bound to.
- */
- getSignal : function () {
- return this._signal;
- },
-
- /**
- * Delete instance properties
- * @private
- */
- _destroy : function () {
- delete this._signal;
- delete this._listener;
- delete this.context;
- },
-
- /**
- * @return {string} String representation of the object.
- */
- toString : function () {
- return '[SignalBinding isOnce:' + this._isOnce +', isBound:'+ this.isBound() +', active:' + this.active + ']';
- }
-
- };
-
-
-/*global SignalBinding:false*/
-
- // Signal --------------------------------------------------------
- //================================================================
-
- function validateListener(listener, fnName) {
- if (typeof listener !== 'function') {
- throw new Error( 'listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName) );
- }
- }
-
- /**
- * Custom event broadcaster
- * - inspired by Robert Penner's AS3 Signals.
- * @name Signal
- * @author Miller Medeiros
- * @constructor
- */
- function Signal() {
- /**
- * @type Array.
- * @private
- */
- this._bindings = [];
- this._prevParams = null;
-
- // enforce dispatch to aways work on same context (#47)
- var self = this;
- this.dispatch = function(){
- Signal.prototype.dispatch.apply(self, arguments);
- };
- }
-
- Signal.prototype = {
-
- /**
- * Signals Version Number
- * @type String
- * @const
- */
- VERSION : '1.0.0',
-
- /**
- * If Signal should keep record of previously dispatched parameters and
- * automatically execute listener during `add()`/`addOnce()` if Signal was
- * already dispatched before.
- * @type boolean
- */
- memorize : false,
-
- /**
- * @type boolean
- * @private
- */
- _shouldPropagate : true,
-
- /**
- * If Signal is active and should broadcast events.
- *
IMPORTANT: Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.
- * @type boolean
- */
- active : true,
-
- /**
- * @param {Function} listener
- * @param {boolean} isOnce
- * @param {Object} [listenerContext]
- * @param {Number} [priority]
- * @return {SignalBinding}
- * @private
- */
- _registerListener : function (listener, isOnce, listenerContext, priority) {
-
- var prevIndex = this._indexOfListener(listener, listenerContext),
- binding;
-
- if (prevIndex !== -1) {
- binding = this._bindings[prevIndex];
- if (binding.isOnce() !== isOnce) {
- throw new Error('You cannot add'+ (isOnce? '' : 'Once') +'() then add'+ (!isOnce? '' : 'Once') +'() the same listener without removing the relationship first.');
- }
- } else {
- binding = new SignalBinding(this, listener, isOnce, listenerContext, priority);
- this._addBinding(binding);
- }
-
- if(this.memorize && this._prevParams){
- binding.execute(this._prevParams);
- }
-
- return binding;
- },
-
- /**
- * @param {SignalBinding} binding
- * @private
- */
- _addBinding : function (binding) {
- //simplified insertion sort
- var n = this._bindings.length;
- do { --n; } while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
- this._bindings.splice(n + 1, 0, binding);
- },
-
- /**
- * @param {Function} listener
- * @return {number}
- * @private
- */
- _indexOfListener : function (listener, context) {
- var n = this._bindings.length,
- cur;
- while (n--) {
- cur = this._bindings[n];
- if (cur._listener === listener && cur.context === context) {
- return n;
- }
- }
- return -1;
- },
-
- /**
- * Check if listener was attached to Signal.
- * @param {Function} listener
- * @param {Object} [context]
- * @return {boolean} if Signal has the specified listener.
- */
- has : function (listener, context) {
- return this._indexOfListener(listener, context) !== -1;
- },
-
- /**
- * Add a listener to the signal.
- * @param {Function} listener Signal handler function.
- * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
- * @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
- * @return {SignalBinding} An Object representing the binding between the Signal and listener.
- */
- add : function (listener, listenerContext, priority) {
- validateListener(listener, 'add');
- return this._registerListener(listener, false, listenerContext, priority);
- },
-
- /**
- * Add listener to the signal that should be removed after first execution (will be executed only once).
- * @param {Function} listener Signal handler function.
- * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
- * @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
- * @return {SignalBinding} An Object representing the binding between the Signal and listener.
- */
- addOnce : function (listener, listenerContext, priority) {
- validateListener(listener, 'addOnce');
- return this._registerListener(listener, true, listenerContext, priority);
- },
-
- /**
- * Remove a single listener from the dispatch queue.
- * @param {Function} listener Handler function that should be removed.
- * @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
- * @return {Function} Listener handler function.
- */
- remove : function (listener, context) {
- validateListener(listener, 'remove');
-
- var i = this._indexOfListener(listener, context);
- if (i !== -1) {
- this._bindings[i]._destroy(); //no reason to a SignalBinding exist if it isn't attached to a signal
- this._bindings.splice(i, 1);
- }
- return listener;
- },
-
- /**
- * Remove all listeners from the Signal.
- */
- removeAll : function () {
- var n = this._bindings.length;
- while (n--) {
- this._bindings[n]._destroy();
- }
- this._bindings.length = 0;
- },
-
- /**
- * @return {number} Number of listeners attached to the Signal.
- */
- getNumListeners : function () {
- return this._bindings.length;
- },
-
- /**
- * Stop propagation of the event, blocking the dispatch to next listeners on the queue.
- *
IMPORTANT: should be called only during signal dispatch, calling it before/after dispatch won't affect signal broadcast.
- * @see Signal.prototype.disable
- */
- halt : function () {
- this._shouldPropagate = false;
- },
-
- /**
- * Dispatch/Broadcast Signal to all listeners added to the queue.
- * @param {...*} [params] Parameters that should be passed to each handler.
- */
- dispatch : function (params) {
- if (! this.active) {
- return;
- }
-
- var paramsArr = Array.prototype.slice.call(arguments),
- n = this._bindings.length,
- bindings;
-
- if (this.memorize) {
- this._prevParams = paramsArr;
- }
-
- if (! n) {
- //should come after memorize
- return;
- }
-
- bindings = this._bindings.slice(); //clone array in case add/remove items during dispatch
- this._shouldPropagate = true; //in case `halt` was called before dispatch or during the previous dispatch.
-
- //execute all callbacks until end of the list or until a callback returns `false` or stops propagation
- //reverse loop since listeners with higher priority will be added at the end of the list
- do { n--; } while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
- },
-
- /**
- * Forget memorized arguments.
- * @see Signal.memorize
- */
- forget : function(){
- this._prevParams = null;
- },
-
- /**
- * Remove all bindings from signal and destroy any reference to external objects (destroy Signal object).
- *
IMPORTANT: calling any method on the signal instance after calling dispose will throw errors.