-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreajax.js
176 lines (131 loc) · 4.72 KB
/
scoreajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
goog.provide('ScoreLibrary.MusicXMLLoader');
goog.require('ScoreLibrary');
goog.require('ScoreLibrary.Inflater');
goog.require('ScoreLibrary.MusicXMLMIMETypes');
/**
* @constructor
*/
ScoreLibrary.MusicXMLLoader =
function(file_or_ref, context, successCallback, errorCallback) {
this.musicxml_ref =
(typeof file_or_ref === 'object' ?
file_or_ref.name : file_or_ref);
this.want_compressed =
(/.*\.mxl$/.test(this.musicxml_ref) ? true : false);
this.context = context;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
};
ScoreLibrary.MusicXMLLoader.prototype.getMIMEType = function() {
var mime_types = ScoreLibrary.MusicXMLMIMETypes;
return (this.want_compressed ? mime_types.MIME_MXL : mime_types.MIME_XML);
};
ScoreLibrary.MusicXMLLoader.prototype.getDataType = function() {
return (this.want_compressed ? 'mxl' : 'xml');
};
ScoreLibrary.MusicXMLLoader.prototype.callbackSuccess = function(result) {
if (this.want_compressed) {
new ScoreLibrary.Inflater(
result,
this.context,
this.successCallback,
this.errorCallback);
}
else {
this.successCallback.call(this.context, result);
}
};
/**
* @constructor
* @extends {ScoreLibrary.MusicXMLLoader}
*/
ScoreLibrary.FileMusicXML =
function(musicxml_file, context, successCallback, errorCallback) {
var supperclass = ScoreLibrary.FileMusicXML.supperclass;
supperclass.constructor.call(
this, musicxml_file, context, successCallback, errorCallback);
this.musicxml_file = musicxml_file;
this.load();
};
ScoreLibrary.inherited(ScoreLibrary.FileMusicXML, ScoreLibrary.MusicXMLLoader);
ScoreLibrary.FileMusicXML.prototype.load = function() {
var reader = new FileReader();
var loader = this;
reader.onload = function(event) {
loader.callbackSuccess(event.target.result);
};
reader.onerror = loader.callbackError;
reader.readAsBinaryString(this.musicxml_file);
};
ScoreLibrary.FileMusicXML.prototype.callbackError =
function(errorThrown) {
this.errorCallback.call(this.context, errorThrown);
};
/**
* @constructor
* @extends {ScoreLibrary.MusicXMLLoader}
*/
ScoreLibrary.AjaxMusicXML =
function(musicxml_ref, context, successCallback, errorCallback) {
var supperclass = ScoreLibrary.AjaxMusicXML.supperclass;
supperclass.constructor.call(
this, musicxml_ref, context, successCallback, errorCallback);
this.load();
};
ScoreLibrary.inherited(ScoreLibrary.AjaxMusicXML, ScoreLibrary.MusicXMLLoader);
ScoreLibrary.AjaxMusicXML.prototype.load = function() {
$.ajax({
'url': this.musicxml_ref,
'type': 'GET',
'mimeType': this.getMIMEType(),
'dataType': this.getDataType(),
'context': this,
'success': this.callbackSuccess,
'error': this.callbackError
});
};
ScoreLibrary.AjaxMusicXML.prototype.callbackError =
function(jqXHR, textStatus, errorThrown) {
this.errorCallback.call(this.context, errorThrown);
};
(ScoreLibrary.AjaxMusicXML.initialize = function() {
$.ajaxSetup({
'contents': {
'xml': /.*musicxml\+xml$/,
'mxl': /.*musicxml$/
},
'converters': {
'text mxl': true
}
});
$.ajaxPrefilter(
'mxl', function(options, originalOptions, jqXHR) {
options['mimeType'] = 'text/plain; charset=x-user-defined';
});
})();
ScoreLibrary.MusicXMLLoader.create =
function(file_or_ref, context, successCallback, errorCallback) {
return (typeof file_or_ref === 'object' ?
new ScoreLibrary.FileMusicXML(
file_or_ref, context, successCallback, errorCallback) :
new ScoreLibrary.AjaxMusicXML(
file_or_ref, context, successCallback, errorCallback));
};
/**
* @author XiongWenjie <[email protected]>
* @license This file is part of
* score-library <http://www.musicxml-viewer.com>.
* score-library is free software:
* you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* score-library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with score-library.
* If not, see <http://www.gnu.org/licenses>.
*/