Kaynağa Gözat

Removed global functions

Andrea Condoluci 8 yıl önce
ebeveyn
işleme
c41dbec9b4

+ 70 - 71
src/Common/FileIO/Mxl.ts

@@ -2,77 +2,76 @@ import { IXmlElement } from "./Xml";
 import { Promise } from "es6-promise";
 import JSZip = require("jszip");
 
-// Usage for extractSheetMusicFromMxl:
-// extractSheetFromMxl(" *** binary content *** ").then(
-//   (score: IXmlElement) => {
-//     // Success! use the score here!
-//   },
-//   (error: any) => {
-//     // There was an error.
-//     // Handle it here.
-//   }
-// )
-export function MXLtoIXmlElement(data: string): Promise<IXmlElement> {
-  "use strict";
-  let zip: JSZip.JSZip = new JSZip();
-  // asynchronously load zip file and process it - with Promises
-  return zip.loadAsync(data).then(
-    (_: any) => {
-      return zip.file("META-INF/container.xml").async("string");
-    },
-    (err: any) => {
-      throw err;
+/**
+ * Some helper methods to handle MXL files.
+ */
+export class MXLHelper {
+    /**
+     *
+     * @param data
+     * @returns {Promise<IXmlElement>}
+     * @constructor
+     */
+    public static MXLtoIXmlElement(data: string): Promise<IXmlElement> {
+        let zip: JSZip.JSZip = new JSZip();
+        // asynchronously load zip file and process it - with Promises
+        return zip.loadAsync(data).then(
+            (_: any) => {
+                return zip.file("META-INF/container.xml").async("string");
+            },
+            (err: any) => {
+                throw err;
+            }
+        ).then(
+            (content: string) => {
+                let parser: DOMParser = new DOMParser();
+                let doc: Document = parser.parseFromString(content, "text/xml");
+                let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
+                return zip.file(rootFile).async("string");
+            },
+            (err: any) => {
+                throw err;
+            }
+        ).then(
+            (content: string) => {
+                let parser: DOMParser = new DOMParser();
+                let xml: Document = parser.parseFromString(content, "text/xml");
+                let doc: IXmlElement = new IXmlElement(xml.documentElement);
+                return Promise.resolve(doc);
+            },
+            (err: any) => {
+                throw err;
+            }
+        ).then(
+            (content: IXmlElement) => {
+                return content;
+            },
+            (err: any) => {
+                throw new Error("extractSheetFromMxl: " + err.message);
+            }
+        );
     }
-  ).then(
-    (content: string) => {
-      let parser: DOMParser = new DOMParser();
-      let doc: Document = parser.parseFromString(content, "text/xml");
-      let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
-      return zip.file(rootFile).async("string");
-    },
-    (err: any) => {
-      throw err;
-    }
-  ).then(
-    (content: string) => {
-      let parser: DOMParser = new DOMParser();
-      let xml: Document = parser.parseFromString(content, "text/xml");
-      let doc: IXmlElement = new IXmlElement(xml.documentElement);
-      return Promise.resolve(doc);
-    },
-    (err: any) => {
-      throw err;
-    }
-  ).then(
-    (content: IXmlElement) => {
-      return content;
-    },
-    (err: any) => {
-      throw new Error("extractSheetFromMxl: " + err.message);
-    }
-  );
-}
 
-export function MXLtoXMLstring(data: string): Promise<string> {
-    "use strict";
-    let zip: JSZip.JSZip = new JSZip();
-    // asynchronously load zip file and process it - with Promises
-    return zip.loadAsync(data).then(
-        (_: any) => {
-            return zip.file("META-INF/container.xml").async("string");
-        },
-        (err: any) => {
-            throw err;
-        }
-    ).then(
-        (content: string) => {
-            let parser: DOMParser = new DOMParser();
-            let doc: Document = parser.parseFromString(content, "text/xml");
-            let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
-            return zip.file(rootFile).async("string");
-        },
-        (err: any) => {
-            throw err;
-        }
-    );
+    public static MXLtoXMLstring(data: string): Promise<string> {
+        let zip:  JSZip.JSZip = new JSZip();
+        // asynchronously load zip file and process it - with Promises
+        return zip.loadAsync(data).then(
+            (_: any) => {
+                return zip.file("META-INF/container.xml").async("string");
+            },
+            (err: any) => {
+                throw err;
+            }
+        ).then(
+            (content: string) => {
+                let parser: DOMParser = new DOMParser();
+                let doc: Document = parser.parseFromString(content, "text/xml");
+                let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
+                return zip.file(rootFile).async("string");
+            },
+            (err: any) => {
+                throw err;
+            }
+        );
+    }
 }

+ 1 - 1
src/Common/FileIO/Xml.ts

@@ -1,5 +1,5 @@
 /**
- * IXmlAttribute is just Attr
+ * IXmlAttribute is just the standard Attr
  */
 export type IXmlAttribute = Attr;
 

+ 5 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer.ts

@@ -7,6 +7,11 @@ import {GraphicalLabel} from "../GraphicalLabel";
 import {VexFlowConverter} from "./VexFlowConverter";
 import {VexFlowTextMeasurer} from "./VexFlowTextMeasurer";
 
+/**
+ * This is a global contant which denotes the height in pixels of the space between two lines of the stave
+ * (when zoom = 1.0)
+ * @type number
+ */
 export const unitInPixels: number = 10;
 
 export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {

+ 3 - 0
src/MusicalScore/ScoreIO/InstrumentReader.ts

@@ -43,6 +43,9 @@ import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
 //  }
 //}
 
+/**
+ * To be implemented
+ */
 export type repetitionInstructionReader = any;
 
 /**

+ 6 - 0
src/MusicalScore/ScoreIO/MusicSheetReader.ts

@@ -18,7 +18,13 @@ import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
 import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
 import {Label} from "../Label";
 
+/**
+ * To be implemented
+ */
 type RepetitionInstructionReader = any;
+/**
+ * To be implemented
+ */
 type RepetitionCalculator = any;
 
 export class MusicSheetReader /*implements IMusicSheetReader*/ {

+ 3 - 0
src/MusicalScore/ScoreIO/VoiceGenerator.ts

@@ -28,6 +28,9 @@ import {IXmlAttribute} from "../../Common/FileIO/Xml";
 import {CollectionUtil} from "../../Util/CollectionUtil";
 import Dictionary from "typescript-collections/dist/lib/Dictionary";
 
+/**
+ * To be implemented
+ */
 export type SlurReader = any;
 
 export class VoiceGenerator {

+ 34 - 30
src/OSMD/AJAX.ts

@@ -1,36 +1,40 @@
 import {Promise} from "es6-promise";
 
 /**
- * Retrive the content of the file at _url_
- * @param url
- * @returns {any}
+ * Class with helper methods to handle asynchronous JavaScript requests
  */
-export function ajax(url: string): Promise<string> {
-    "use strict";
-    let xhttp: XMLHttpRequest;
-    if (XMLHttpRequest) {
-        xhttp = new XMLHttpRequest();
-    } else if (ActiveXObject) {
-        // for IE<7
-        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
-    } else {
-        return Promise.reject(new Error("XMLHttp not supported."));
-    }
-    return new Promise((resolve: (value: string) => void, reject: (error: any) => void) => {
-        xhttp.onreadystatechange = () => {
-            if (xhttp.readyState === XMLHttpRequest.DONE) {
-                if (xhttp.status === 200) {
-                    resolve(xhttp.responseText);
-                } else if (xhttp.status === 0 && xhttp.responseText) {
-                    resolve(xhttp.responseText);
-                } else {
-                    //reject(new Error("AJAX error: '" + xhttp.statusText + "'"));
-                    reject(new Error("Could not retrieve requested URL"));
+export class AJAX {
+    /**
+     * Retrieve the content of the file at url
+     * @param url
+     * @returns {any}
+     */
+    public static ajax(url: string): Promise<string> {
+        let xhttp: XMLHttpRequest;
+        if (XMLHttpRequest) {
+            xhttp = new XMLHttpRequest();
+        } else if (ActiveXObject) {
+            // for IE<7
+            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
+        } else {
+            return Promise.reject(new Error("XMLHttp not supported."));
+        }
+        return new Promise((resolve: (value: string) => void, reject: (error: any) => void) => {
+            xhttp.onreadystatechange = () => {
+                if (xhttp.readyState === XMLHttpRequest.DONE) {
+                    if (xhttp.status === 200) {
+                        resolve(xhttp.responseText);
+                    } else if (xhttp.status === 0 && xhttp.responseText) {
+                        resolve(xhttp.responseText);
+                    } else {
+                        //reject(new Error("AJAX error: '" + xhttp.statusText + "'"));
+                        reject(new Error("Could not retrieve requested URL"));
+                    }
                 }
-            }
-        };
-        xhttp.overrideMimeType("text/plain; charset=x-user-defined");
-        xhttp.open("GET", url, true);
-        xhttp.send();
-    });
+            };
+            xhttp.overrideMimeType("text/plain; charset=x-user-defined");
+            xhttp.open("GET", url, true);
+            xhttp.send();
+        });
+    }
 }

+ 47 - 6
src/OSMD/OSMD.ts

@@ -6,10 +6,9 @@ import {MusicSheetCalculator} from "./../MusicalScore/Graphical/MusicSheetCalcul
 import {VexFlowMusicSheetDrawer} from "./../MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer";
 import {MusicSheet} from "./../MusicalScore/MusicSheet";
 import {Cursor} from "./Cursor";
-import {MXLtoXMLstring} from "../Common/FileIO/Mxl";
+import {MXLHelper} from "../Common/FileIO/Mxl";
 import {Promise} from "es6-promise";
-import {handleResize} from "./ResizeHandler";
-import {ajax} from "./AJAX";
+import {AJAX} from "./AJAX";
 import {Logging} from "../Common/Logging";
 import {Fraction} from "../Common/DataObjects/Fraction";
 import {OutlineAndFillStyleEnum} from "../MusicalScore/Graphical/DrawingEnums";
@@ -69,7 +68,7 @@ export class OSMD {
             let self: OSMD = this;
             if (str.substr(0, 4) === "\x50\x4b\x03\x04") {
                 // This is a zip file, unpack it first
-                return MXLtoXMLstring(str).then(
+                return MXLHelper.MXLtoXMLstring(str).then(
                     (str: string) => {
                         return self.load(str);
                     },
@@ -86,7 +85,7 @@ export class OSMD {
             } else if (str.length < 2083) {
                 // Assume now 'str' is a URL
                 // Retrieve the file at the given URL
-                return ajax(str).then(
+                return AJAX.ajax(str).then(
                     (s: string) => { return self.load(s); },
                     (exc: Error) => { throw exc; }
                 );
@@ -172,11 +171,13 @@ export class OSMD {
      */
     private autoResize(): void {
         let self: OSMD = this;
-        handleResize(
+        this.handleResize(
             () => {
                 // empty
             },
             () => {
+                // The following code is probably not needed
+                // (the width should adapt itself to the max allowed)
                 //let width: number = Math.max(
                 //    document.documentElement.clientWidth,
                 //    document.body.scrollWidth,
@@ -189,4 +190,44 @@ export class OSMD {
             }
         );
     }
+
+    /**
+     * Helper function for managing window's onResize events
+     * @param startCallback is the function called when resizing starts
+     * @param endCallback is the function called when resizing (kind-of) ends
+     */
+    private handleResize(startCallback: () => void, endCallback: () => void): void {
+        let rtime: number;
+        let timeout: number = undefined;
+        let delta: number = 200;
+
+        function resizeEnd(): void {
+            timeout = undefined;
+            window.clearTimeout(timeout);
+            if ((new Date()).getTime() - rtime < delta) {
+                timeout = window.setTimeout(resizeEnd, delta);
+            } else {
+                endCallback();
+            }
+        }
+
+        function resizeStart(): void {
+            rtime = (new Date()).getTime();
+            if (!timeout) {
+                startCallback();
+                rtime = (new Date()).getTime();
+                timeout = window.setTimeout(resizeEnd, delta);
+            }
+        }
+
+        if ((<any>window).attachEvent) {
+            // Support IE<9
+            (<any>window).attachEvent("onresize", resizeStart);
+        } else {
+            window.addEventListener("resize", resizeStart);
+        }
+
+        window.setTimeout(startCallback, 0);
+        window.setTimeout(endCallback, 1);
+    }
 }

+ 0 - 40
src/OSMD/ResizeHandler.ts

@@ -1,40 +0,0 @@
-/**
- * Helper function for managing window's onResize events
- * @param startCallback is the function called when resizing starts
- * @param endCallback is the function called when resizing (kind-of) ends
- */
-export function handleResize(startCallback: () => void, endCallback: () => void): void {
-    "use strict";
-    let rtime: number;
-    let timeout: number = undefined;
-    let delta: number = 200;
-
-    function resizeEnd(): void {
-        timeout = undefined;
-        window.clearTimeout(timeout);
-        if ((new Date()).getTime() - rtime < delta) {
-            timeout = window.setTimeout(resizeEnd, delta);
-        } else {
-            endCallback();
-        }
-    }
-
-    function resizeStart(): void {
-        rtime = (new Date()).getTime();
-        if (!timeout) {
-            startCallback();
-            rtime = (new Date()).getTime();
-            timeout = window.setTimeout(resizeEnd, delta);
-        }
-    }
-
-    if ((<any>window).attachEvent) {
-        // Support IE<9
-        (<any>window).attachEvent("onresize", resizeStart);
-    } else {
-        window.addEventListener("resize", resizeStart);
-    }
-
-    window.setTimeout(startCallback, 0);
-    window.setTimeout(endCallback, 1);
-}

+ 3 - 3
test/Common/FileIO/Mxl_Test.ts

@@ -1,6 +1,6 @@
 import { IXmlElement } from "../../../src/Common/FileIO/Xml";
-import { MXLtoIXmlElement } from "../../../src/Common/FileIO/Mxl.ts";
 import { TestUtils } from "../../Util/TestUtils";
+import { MXLHelper } from "../../../src/Common/FileIO/Mxl";
 
 describe("MXL Tests", () => {
   // Generates a test for a mxl file name
@@ -14,7 +14,7 @@ describe("MXL Tests", () => {
       // (with Promises), thus we need a little fix
       // in the end with 'then(null, done)' to
       // make Mocha work asynchronously
-      MXLtoIXmlElement(mxl).then(
+      MXLHelper.MXLtoIXmlElement(mxl).then(
         (score: IXmlElement) => {
           chai.expect(score).to.not.be.undefined;
           chai.expect(score.name).to.equal("score-partwise");
@@ -33,7 +33,7 @@ describe("MXL Tests", () => {
 
   // Test failure
   it("Corrupted file", (done: MochaDone) => {
-    MXLtoIXmlElement("").then(
+    MXLHelper.MXLtoIXmlElement("").then(
       (score: IXmlElement) => {
         chai.expect(score).to.not.be.undefined;
         chai.expect(score.name).to.equal("score-partwise");