-
Notifications
You must be signed in to change notification settings - Fork 72
/
jquery.xmleditor.js
5395 lines (4717 loc) · 168 KB
/
jquery.xmleditor.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(function($){
/*
Copyright 2008 The University of North Carolina at Chapel Hill
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* jQuery xml Editor
*
* Dependencies:
* jquery 1.7+
* jquery.ui 1.9+
* ajax ace editor
* jquery.autosize.js (optional)
*
* @author Ben Pennell
*/
// Selector and class name constants
var menuContainerClass = "xml_menu_container";
var menuHeaderClass = "menu_header";
var menuColumnClass = "xml_menu_column";
var menuContentClass = 'menu_content';
var menuExpandDuration = 180;
var xmlElementClass = 'xml_element';
var xmlTextClass = 'xml_text_node';
var xmlNodeClass = 'xml_node';
var topLevelContainerClass = 'top_level_element_group';
var elementRootPrefix = "root_element_";
var elementPrefix = "xml_element_";
var childrenContainerSelector = " > .xml_children";
var childrenContainerClass = "xml_children";
var attributeContainerClass = "attribute_container";
var attributesContainerSelector = " > .xml_attrs";
var attributesContainerClass = "xml_attrs";
var xmlMenuHeaderPrefix = "xml_header_item_";
var xmlEditorContainerClass = "xml_editor_container";
var xmlWorkAreaContainerClass = "xml_work_area";
var addTopMenuClass = "add_top_menu";
var addAttrMenuClass = "add_attribute_menu";
var addElementMenuClass = "add_element_menu";
var addNodeMenuClass = "add_node_menu";
var xmlMenuBarClass = "xml_menu_bar";
var submitButtonClass = "send_xml";
var submissionStatusClass = "xml_submit_status";
var xmlContentClass = "xml_content";
var editorTabAreaClass = "xml_tab_area";
var problemsPanelClass = "xml_problems_panel";
var guiContentClass = "gui_content";
var textContentClass = "text_content";
var editorHeaderClass = "xml_editor_header";
// Returns the local name of a node. Needed for older versions of ie
var localName = function(node) {
var localName = node.localName;
if (localName) return localName;
var index = node.nodeName.indexOf(':');
if (index == -1) return node.nodeName;
return node.nodeName.substring(index + 1);
};
$.widget( "xml.xmlEditor", {
options: {
// Schema object to be used
schema: null,
// Whether or not to attempt to load the schema in a worker thread, if available
loadSchemaAsychronously: true,
// Path to directory containing cycle.js, needed for loadSchemaAsychronously
libPath: null,
// Document retrieval and upload parameters
ajaxOptions : {
xmlUploadPath: null,
xmlRetrievalPath: null,
xmlRetrievalParams : null
},
// User set default template settings
templateOptions : {
templatePath : false,
templates : [],
cancelFunction : false
},
// Function triggered after uploading XML document, to interpret if the response was successful or not. If upload failed, an error message should be returned.
submitResponseHandler : null,
// Function triggered after uploading XML document, if an error occurs. Gives full text of error, instead of a boilerplate "500 server error" message.
submitErrorHandler : null,
submitButtonConfigs : null,
// Event function trigger after an xml element is update via the gui
elementUpdated : undefined,
// Title for the document, displayed in the header
documentTitle : null,
addTopMenuHeaderText : 'Add Top Element',
addAttrMenuHeaderText : 'Add Attribute',
addElementMenuHeaderText : 'Add Subelement',
xmlEditorLabel : 'XML',
textEditorLabel : 'Text',
// Set to false to get rid of the
enableDocumentStatusPanel : true,
documentStatusPanelDomId : "<div>",
confirmExitWhenUnsubmitted : true,
enableGUIKeybindings : true,
floatingMenu : true,
// Requires jquery.autosize, defaults to false if plugin isn't detected
expandingTextAreas: true,
// Pretty formatting of XML output. Requires vkbeauty.js, defaults to false is not available
prettyXML : true,
// Number of history states held for the undo feature
undoHistorySize: 20,
// Object containing additional entries to add to the header menu
menuEntries: undefined,
enforceOccurs: false,
prependNewElements: false,
autocomplete: true,
targetNS: null
},
_create: function() {
var self = this;
var schema = this.options.schema;
var libPath = this.options.libPath;
this.instanceNumber = $("xml-xmlEditor").length;
// Tree of xml element types
this.schemaTree = null;
// State of the XML document
this.xmlState = null;
// Container for the entire editor
this.xmlEditorContainer = null;
// Container for the subeditors
this.xmlWorkAreaContainer = null;
// Tabbed container for differentiating between specific subeditors
this.xmlTabContainer = null;
// Header container for the menu and top level info
this.editorHeader = null;
// Panel for displaying errors
this.problemsPanel = null;
// GUI Editor object
this.guiEditor = null;
// Text Editor object
this.textEditor = null;
// Currently active editor
this.activeEditor = null;
// History manager for undo/redo
this.undoHistory = null;
// Top level menu bar object
this.menuBar = null;
// Element modification object
this.modifyMenu = null;
// Flag indicating if the editor was initialized on a text area that will need to be updated
this.isTextAreaEditor = false;
var url = document.location.href;
var index = url.lastIndexOf("/");
if (index != -1)
this.baseUrl = url.substring(0, index + 1);
// Detect optional features
if (!$.isFunction($.fn.autosize))
this.options.expandingTextAreas = false;
if (typeof(this.options.schema) != 'function') {
// Turn relative paths into absolute paths for the sake of web workers
var path_regx = /^https?:\/\/.*?\//;
var matches = this.baseUrl.match(path_regx);
// Turn relative paths into absolute paths for the sake of web workers
if (libPath) {
// Check for trailing slash. Add if needed, otherwise libPath breaks
libPath = (/\/$/.test(libPath)) ? libPath : libPath + '/';
if (!path_regx.test(libPath)) {
if (libPath.indexOf('/') === 0) {
this.libPath = matches[0] + libPath.substr(1, libPath.length);
} else {
this.libPath = this.baseUrl + libPath;
}
} else {
this.libPath = libPath;
}
} else {
this.libPath = this.baseUrl + "lib/";
}
if ((typeof schema == 'string' || typeof schema instanceof String)) {
// if http(s) just return the schema untouched
if (!path_regx.test(schema)) {
if (schema.indexOf('/') === 0) {
// Relative to root
this.options.schema = matches[0] + schema.substr(1, schema.length);
} else {
// Relative to current path
this.options.schema = this.baseUrl + schema;
}
}
}
}
// Load the schema
this.loadSchema(this.options.schema);
},
_init: function() {
if (this.options.submitButtonConfigs) {
// User provided button configuration
this.submitButtonConfigs = this.options.submitButtonConfigs;
} else {
// Simple button configuration, generate defaults
var exporting = !this.options.ajaxOptions.xmlUploadPath;
// Either an upload button or an export button
this.submitButtonConfigs = [{
url : this.options.ajaxOptions.xmlUploadPath,
label : exporting? "Export" : "Submit changes",
onSubmit : exporting? this.exportXML : null,
disabled : typeof(Blob) === undefined && exporting
}];
}
if (this.options.submitErrorHandler == null) {
this.options.submitErrorHandler = function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
};
}
// Retrieve the local xml content before we start populating the editor.
var localXMLContent = null;
if (this.element.is("textarea")) {
// Editor initialized on a text area. Move text area out of the way and hide it
// so that it can be updated with document changes.
this.isTextAreaEditor = true;
// Capture the existing value in the text area in case we are using a local document
// Retrieving text instead of val because of firefox inconsistencies
localXMLContent = this.element.text();
this.xmlEditorContainer = $("<div/>").attr('class', xmlEditorContainerClass);
$(this.element)
.before(this.xmlEditorContainer)
.hide();
} else {
if (this.element.children().length > 0) {
// If the content is embedded as part of the html, retrieve it as such.
// Note, this is case insensitive and can leave to problems
localXMLContent = this.element.html();
} else {
// Content is probably XML encoded
localXMLContent = this.element.text();
}
// Clear out the contents of the element being initialized on
this.element.text("");
// Add the editor into the dom
this.xmlEditorContainer = $("<div/>").attr('class', xmlEditorContainerClass).appendTo(this.element);
}
this.xmlState = null;
this.xmlWorkAreaContainer = null;
this.xmlTabContainer = null;
this.editorHeader = null;
this.problemsPanel = null;
this.guiEditor = new GUIEditor(this);
this.textEditor = new TextEditor(this);
this.activeEditor = this.guiEditor;
var self = this;
this.menuBar = new MenuBar(this);
this.menuBar.updateFunctions.push(this.refreshMenuUndo);
this.menuBar.updateFunctions.push(this.refreshMenuSelected);
if (this.options.menuEntries) {
$.each(this.options.menuEntries, function() {
self.menuBar.addEntry(this);
});
}
this.modifyMenu = new ModifyMenuPanel(this);
if (this.options.confirmExitWhenUnsubmitted) {
$(window).bind('beforeunload', function(e) {
if (self.xmlState != null && self.xmlState.isChanged()) {
return "The document contains unsaved changes.";
}
});
}
// Load any external vocabularies
this.loadVocabularies(this.options.vocabularyConfigs);
// Start loading the document for editing
this.loadDocument(this.options.ajaxOptions, localXMLContent);
},
// Load the schema object
loadSchema: function(schema) {
var self = this;
// If the schema is a function, execute it to get the schema from it.
if (jQuery.isFunction(schema)) {
this.schema = schema.apply();
self._schemaReady();
} else {
// Load schema in separate thread for browsers tha support it. IE10 blocked to avoid security error
if (this.options.loadSchemaAsychronously && !window.MSBlobBuilder
&& typeof(window.URL) !== "undefined" && typeof(Worker) !== "undefined" && typeof(Blob) !== "undefined") {
var blob = new Blob([
"self.onmessage = function(e) {" +
"importScripts(e.data.libPath + 'cycle.js');" +
"var schema;" +
"if (typeof e.data.schema == 'string' || typeof e.data.schema instanceof String) {" +
" var xmlhttp = new XMLHttpRequest();" +
" xmlhttp.onreadystatechange = function() {" +
" if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" +
" schema = eval('(' + xmlhttp.responseText + ')');" +
" self.postMessage(JSON.retrocycle(schema));" +
" }" +
" };" +
" xmlhttp.open('GET', e.data.schema, true);" +
" xmlhttp.send();" +
"} else {" +
" schema = JSON.retrocycle(e.data.schema);" +
" self.postMessage(schema);" +
"}" +
"}"
], { type: "text/javascript" });
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(e) {
self.schema = e.data;
self._schemaReady();
worker.terminate();
};
worker.onerror = function(e) {
self.schema = JSON.retrocycle(schema);
self._schemaReady();
};
worker.postMessage({'schema' : schema, 'libPath' : this.libPath});
} else {
// Fallback with synchronous retrocycling
if (typeof schema == 'string' || typeof schema instanceof String) {
$.ajax({
url : schema,
async : self.options.loadSchemaAsynchronously,
dataType : 'json',
success : function(data) {
self.schema = JSON.retrocycle(data);
self._schemaReady();
}
});
} else {
self.schema = JSON.retrocycle(schema);
self._schemaReady();
}
}
}
},
// Load the XML document for editing
loadDocument: function(ajaxOptions, localXMLContent) {
var self = this;
if (ajaxOptions != null && ajaxOptions.xmlRetrievalPath != null) {
// Load document from the specified path
$.ajax({
type : "GET",
url : ajaxOptions.xmlRetrievalPath,
data : (ajaxOptions.xmlRetrievalParams),
dataType : "text",
success : function(data) {
if ($(data).children().length) {
self._documentReady(data);
} else if (self.options.templateOptions.templatePath) {
// Document path didn't retrieve anything
self._templating();
} else {
console.error("Could not specified document and no fallback provided, cannot start.");
}
}
});
} else if ($.trim(localXMLContent)) {
// Use local content embedded in starting element next
this._documentReady(localXMLContent);
} else if (this.options.templateOptions.templatePath) {
// Fall back to templating if it was specified
this._templating();
} else {
console.error("No starting document");
}
},
loadVocabularies : function(vocabularyConfigs) {
var self = this;
this.loadingVocabs = 0;
if (!this.options.vocabularyConfigs || !this.options.vocabularyConfigs.vocabularies) {
return;
}
$.each(this.options.vocabularyConfigs.vocabularies, function(vocabName, vocabInfo) {
if ("url" in vocabInfo) {
self.loadingVocabs++;
}
});
$.each(this.options.vocabularyConfigs.vocabularies, function(vocabName, vocabInfo) {
if ("url" in vocabInfo) {
$.ajax({
url : vocabInfo["url"],
type : "GET",
dataType : "json"
}).done(function(data){
vocabInfo.values = data;
self.loadingVocabs--;
if (self.loadingVocabs == 0) {
self._everythingReady();
}
});
}
});
},
_templating : function() {
var self = this;
self.template = new XMLTemplates(self);
self.template.createChooseTemplate();
},
// XML Document loaded event
_documentReady : function(xmlString) {
var self = this;
this.xmlState = new DocumentState(xmlString, this);
this.xmlState.extractNamespacePrefixes();
this.undoHistory = new UndoHistory(this.xmlState, this);
this.undoHistory.setStateChangeEvent(function() {
self.refreshDisplay();
});
this._everythingReady();
},
// Schema object loaded event
_schemaReady : function() {
if (!this.options.targetNS) {
this.targetNS = this.schema.namespace;
}
this.schemaTree = new SchemaTree(this.schema);
this.schemaTree.build();
this._everythingReady();
},
// Performs initialization of editor after rejoining document and schema loading workflows
// to support asychronous/multithreaded loading
_everythingReady : function() {
// Join back up asynchronous loading of document and schema
if (!this.schemaTree || !this.xmlState || this.loadingVocabs != 0)
return;
this.targetPrefix = this.xmlState.namespaces.getNamespacePrefix(this.options.targetNS);
this.constructEditor();
this.refreshDisplay();
this.activeEditor.selectRoot();
// Capture baseline undo state
this.undoHistory.captureSnapshot();
},
// Construct user interface components of the editor
constructEditor: function() {
// Work Area
this.xmlWorkAreaContainer = $("<div/>").attr('class', xmlWorkAreaContainerClass).appendTo(this.xmlEditorContainer);
// Menu bar
var editorHeaderBacking = $("<div/>").addClass(editorHeaderClass + "_backing").appendTo(this.xmlWorkAreaContainer);
this.editorHeader = $("<div/>").attr('class', editorHeaderClass).appendTo(this.xmlWorkAreaContainer);
if (this.options.documentTitle != null)
$("<h2/>").html("Editing Description: " + this.options.documentTitle).appendTo(this.editorHeader);
this.menuBar.render(this.editorHeader);
editorHeaderBacking.height(this.editorHeader.outerHeight());
// Create grouping of header elements that need to be positioned together
this.editorHeaderGroup = this.editorHeader.add(editorHeaderBacking);
this.xmlTabContainer = $("<div/>").attr("class", editorTabAreaClass).css("padding-top", this.editorHeader.height() + "px").appendTo(this.xmlWorkAreaContainer);
this.problemsPanel = $("<pre/>").attr('class', problemsPanelClass).hide().appendTo(this.xmlTabContainer);
this.guiEditor.initialize(this.xmlTabContainer);
this.modeChange(0);
var self = this;
$(window).resize($.proxy(this.resize, this));
this.modifyMenu.initialize(this.xmlEditorContainer);
this.modifyMenu.addMenu(addElementMenuClass, this.options.addElementMenuHeaderText,
true, false, true);
this.modifyMenu.addAttributeMenu(addAttrMenuClass, this.options.addAttrMenuHeaderText,
true, false, true);
this.modifyMenu.addNodeMenu(addNodeMenuClass, "Add Nodes", true, false);
this.addTopLevelMenu = this.modifyMenu.addMenu(addTopMenuClass, this.options.addTopMenuHeaderText,
true, true, false, function(target) {
var selectedElement = self.guiEditor.selectedElement;
if (!selectedElement || selectedElement.length == 0 || selectedElement.isRootElement)
return null;
var currentElement = selectedElement;
while (currentElement.parentElement != null) {
if (currentElement.parentElement.isRootElement)
break;
currentElement = currentElement.parentElement;
}
if (currentElement != null)
return currentElement;
return null;
}).populate(this.guiEditor.rootElement);
this.setEnableKeybindings(this.options.enableGUIKeybindings);
if (this.options.floatingMenu) {
$(window).bind('scroll', $.proxy(this.modifyMenu.setMenuPosition, this.modifyMenu));
}
this.ready = true;
},
// Resize event for refreshing menu and editor sizes
resize: function () {
this.xmlTabContainer.width(this.xmlEditorContainer.outerWidth() - this.modifyMenu.menuColumn.outerWidth());
if (this.activeEditor != null){
this.activeEditor.resize();
}
this.editorHeader.width(this.xmlTabContainer.width());
if (this.options.floatingMenu) {
this.modifyMenu.setMenuPosition();
}
},
// Event which triggers the creation of a new child element, as defined by an instigator such as a menu
addChildElementCallback: function (instigator, relativeTo, prepend) {
if ($(instigator).hasClass("disabled"))
return;
var xmlElement = $(instigator).data("xml").target;
var objectType = $(instigator).data("xml").objectType;
this.addChildElement(xmlElement, objectType, relativeTo, prepend);
},
addChildElement: function(parentElement, newElementDefinition, relativeTo, prepend) {
if (!parentElement.allowChildren)
return null;
// If in the text editor view, synchronous the text to the xml model and ensure wellformedness
if (this.textEditor.active) {
if (this.xmlState.changesNotSynced()) {
try {
this.setXMLFromEditor();
} catch (e) {
this.addProblem("Unable to add element, please fix existing XML syntax first.", e);
return;
}
}
}
var objectType;
if (typeof newElementDefinition == 'string' || newElementDefinition instanceof String) {
var defs = parentElement.objectType.elements;
for (var index in defs) {
var definition = defs[index];
var elementName = this.xmlState.getNamespacePrefix(definition.namespace)
+ definition.localName;
if (elementName == newElementDefinition) {
objectType = definition;
break;
}
}
// No matching child definition and parent doesn't allow "any", so can't add child
if (!objectType && !parentElement.objectType.any) {
return "Could not add child " + newElementDefinition + ", it is not a valid child of " + parentElement.objectType.localName;
}
} else {
objectType = newElementDefinition;
}
// Create the new element as a child of its parent
var newElement;
if (objectType) {
// Determine if it is valid to add this child element to the given parent element
if (!parentElement.childCanBeAdded(objectType))
return;
// Add the namespace of the new element to the root if it is not already present
this.xmlState.addNamespace(objectType);
newElement = parentElement.addElement(objectType, relativeTo, prepend);
} else {
var nameParts = newElementDefinition.split(":");
this.xmlState.addNamespace(nameParts.length > 1? nameParts[0] : "");
newElement = parentElement.addNonschemaElement(newElementDefinition, relativeTo, prepend);
}
if (newElement == null) {
return "Failed to add child of type " + newElementDefinition;
}
// Trigger post element creation event in the currently active editor to handle UI updates
this.activeEditor.addElementEvent(parentElement, newElement);
return newElement;
},
// Event which adds an attribute to an element, as defined by an instigator such as a menu
addAttributeButtonCallback: function(instigator) {
if ($(instigator).hasClass("disabled"))
return;
// Create attribute on the targeted parent, and add its namespace if missing
var data = $(instigator).data('xml');
return this.addAttribute(data.target, data.objectType, instigator);
},
addAttribute: function(xmlElement, attrDefinition, instigator) {
// Synchronize xml document if there are unsynchronized changes in the text editor
if (this.xmlState.changesNotSynced()) {
try {
this.setXMLFromEditor();
} catch (e) {
alert(e.message);
return;
}
}
var objectType;
if ($.type(attrDefinition) === "object") {
objectType = attrDefinition;
} else {
var defs = xmlElement.objectType.attributes;
for (var index in defs) {
var definition = defs[index];
var attrName = this.xmlState.getNamespacePrefix(definition.namespace)
+ definition.localName;
if (attrName == attrDefinition) {
objectType = definition;
break;
}
}
if (!objectType && !xmlElement.objectType.anyAttribute) {
return "Could not add attribute " + attrDefinition + ", it is not a valid for element " + xmlElement.objectType.localName;
}
}
var newAttr;
if (objectType) {
this.xmlState.addNamespace(objectType);
newAttr = xmlElement.addAttribute(objectType);
} else {
var nameParts = attrDefinition.split(":");
if (nameParts.length > 1)
this.xmlState.addNamespace(nameParts[0]);
newAttr = xmlElement.addAttribute({
attribute : true,
localName : attrDefinition
});
//newAttr = xmlElement.addNonschemaAttribute(newElementDefinition);
}
// Inform the active editor of the newly added attribute
this.activeEditor.addAttributeEvent(xmlElement, newAttr, $(instigator));
return newAttr;
},
addNodeCallback: function(instigator, nodeType, prepend) {
if ($(instigator).hasClass("disabled"))
return;
var data = $(instigator).data('xml');
this.addNode(data.target, nodeType, prepend);
},
addNode: function(parentElement, nodeType, prepend, relativeTo) {
// Synchronize xml document if there are unsynchronized changes in the text editor
if (this.xmlState.changesNotSynced()) {
try {
this.setXMLFromEditor();
} catch (e) {
alert(e.message);
return;
}
}
if (!(parentElement instanceof XMLElement)) {
return;
}
// Create node on the targeted parent
var nodeObject = parentElement.addNode(nodeType, prepend, relativeTo);
// Inform the active editor of the newly added attribute
if (nodeObject) {
this.guiEditor.selectNode(nodeObject);
this.activeEditor.addNodeEvent(parentElement, nodeObject);
nodeObject.focus();
}
},
// Adds an element stub either as a child of an element which allows children, or
// as a sibling
addNextElement : function(xmlElement, prepend) {
if (xmlElement.allowChildren) {
this.addNode(xmlElement, "element", prepend);
} else {
this.addNode(xmlElement.parentElement, "element", prepend, xmlElement);
}
},
// Triggered when a document has been loaded or reloaded
documentLoadedEvent : function(newDocument) {
if (this.guiEditor != null && this.guiEditor.rootElement != null)
this.guiEditor.rootElement.xmlNode = newDocument.children().first();
if (this.problemsPanel != null)
this.clearProblemPanel();
},
// Switch the currently active editor to the editor identified
modeChange: function(mode) {
// Can't change mode to current mode
if ((mode == 0 && this.guiEditor.active) || (mode == 1 && this.textEditor.active))
return this;
if (mode == 0) {
$("*:focus").blur();
$(".xml_editor_container *:focus").blur();
if (this.textEditor.isInitialized() && this.xmlState.isChanged()) {
// Try to reconstruct the xml object before changing tabs.
this.setXMLFromEditor();
this.undoHistory.captureSnapshot();
}
}
$(".active_mode_tab").removeClass("active_mode_tab");
this.modifyMenu.clearContextualMenus();
if (this.activeEditor != null) {
this.activeEditor.deactivate();
}
if (mode == 0) {
this.activeEditor = this.guiEditor;
$("#" + xmlMenuHeaderPrefix + this.options.xmlEditorLabel.replace(/ /g, "_")).addClass("active_mode_tab");
} else {
this.activeEditor = this.textEditor;
$("#" + xmlMenuHeaderPrefix + this.options.textEditorLabel.replace(/ /g, "_")).addClass("active_mode_tab");
}
this.activeEditor.activate();
if (this.ready)
this.resize();
return this;
},
refreshDisplay: function() {
if (this.activeEditor == null)
return;
this.activeEditor.refreshDisplay();
if (this.options.floatingMenu) {
this.modifyMenu.setMenuPosition();
}
this.xmlWorkAreaContainer.width(this.xmlEditorContainer.outerWidth() - this.modifyMenu.menuColumn.outerWidth());
},
setTextArea : function(xmlString) {
if (this.isTextAreaEditor)
this.element.val(xmlString);
},
// Refresh the state of the XML document from the contents of text editor
setXMLFromEditor: function() {
var xmlString = this.textEditor.aceEditor.getValue();
this.xmlState.setXMLFromString(xmlString);
if (this.guiEditor.isActive) {
this.guiEditor.setRootElement(this.xmlState.xml.children()[0]);
this.addTopLevelMenu.populate(this.guiEditor.rootElement)
}
},
getXMLString: function() {
if (this.textEditor.active) {
var xmlString = this.textEditor.aceEditor.getValue();
try {
this.xmlState.setXMLFromString(xmlString);
} catch (e) {
// Ignore error, continue to return last GUI value instead
}
}
return this.xml2Str(this.xmlState.xml);
},
getText: function() {
if (this.textEditor.active) {
return this.textEditor.aceEditor.getValue();
}
return this.xml2Str(this.xmlState.xml);
},
// Callback for submit button pressing. Performs a submit function and then uploads the
// document to the provided URL, if configured to do either
submitXML: function(config) {
if (config.onSubmit) {
config.onSubmit.call(this, config);
}
if (config.url) {
this.uploadXML(config);
}
},
// Export the contents of the editor as text to a file, as supported by browsers
exportXML: function() {
if (typeof(Blob) === "undefined") {
this.addProblem("Browser does not support saving files via this editor. To save, copy and paste the document from the Text view.");
return false;
}
var exportDialog = $("<form><input type='text' class='xml_export_filename' placeholder='file.xml'/><input type='submit' value='Export'/></form>")
.dialog({modal: true, dialogClass: 'xml_dialog', resizable : false, title: 'Enter file name', height: 80});
var self = this;
exportDialog.submit(function(){
if (self.textEditor.active) {
try {
self.setXMLFromEditor();
} catch (e) {
self.xmlState.setDocumentHasChanged(true);
$("." + submissionStatusClass).html("Failed to save<br/>See errors at top").css("background-color", "#ffbbbb").animate({backgroundColor: "#ffffff"}, 1000);
self.addProblem("Cannot save due to invalid xml", e);
return false;
}
}
var xmlString = self.xml2Str(self.xmlState.xml);
var blob = new Blob([xmlString], { type: "text/xml" });
var url = URL.createObjectURL(blob);
exportDialog.dialog('option', 'title', '');
var fileName = exportDialog.find('input[type="text"]').val();
if (!fileName)
fileName = "file.xml";
var download = $('<a>Download ' + fileName + '</a>').attr("href", url);
download.attr("download", fileName);
exportDialog.empty().append(download);
return false;
});
},
// Upload the contents of the editor to a path
uploadXML: function(config) {
if (!config || !config.url) {
if (this.submitButtonConfigs.length > 0 && this.submitButtonConfigs[0].url) {
config = this.submitButtonConfigs[0];
} else {
this.addProblem("Cannot submit because no post Options");
return;
}
}
if (this.textEditor.active) {
try {
this.setXMLFromEditor();
} catch (e) {
this.xmlState.setDocumentHasChanged(true);
$("." + submissionStatusClass).html("Failed to submit<br/>See errors at top").css("background-color", "#ffbbbb").animate({backgroundColor: "#ffffff"}, 1000);
this.addProblem("Cannot submit due to invalid xml", e);
return false;
}
}
// convert XML DOM to string
var xmlString = this.xml2Str(this.xmlState.xml);
$("." + submissionStatusClass).html("Submitting...");
var self = this;
$.ajax({
url : config.url,
contentType : "application/xml",
type : "POST",
data : xmlString,
success : function(response) {
// Process the response from the server using the provided response handler
// If the result of the handler evaluates true, then it is assumed to be an error
var outcome = config.responseHandler(response);
if (!outcome) {
self.xmlState.changesCommittedEvent();
self.clearProblemPanel();
} else {
self.xmlState.syncedChangeEvent();
$("." + submissionStatusClass).html("Failed to submit<br/>See errors at top").css("background-color", "#ffbbbb").animate({backgroundColor: "#ffffff"}, 1000);
self.addProblem("Failed to submit xml document", outcome);
}
},
error : function(jqXHR, exception) {
if (config.errorHandler) {
config.errorHandler(jqXHR, exception);
return;
}
self.options.submitErrorHandler(jqXHR, exception);
}
});
},
// Default server submission response parser, mostly for reference.
swordSubmitResponseHandler: function(response) {
var responseObject = $(response);
if (responseObject.length > 0 && localName(responseObject[responseObject.length - 1]) == "sword:error") {
return responseObject.find("atom\\:summary").html();
}
return false;
},
// Serializes the provided xml node into a string
xml2Str: function(xmlNodeObject) {
if (xmlNodeObject == null)
xmlNodeObject = this.xmlState.xml;
var xmlNode = (xmlNodeObject instanceof jQuery? xmlNodeObject[0]: xmlNodeObject);
var xmlStr = "";
if (this.options.prettyXML) {
return formatXML(xmlNode);
} else {
try {
// Gecko-based browsers, Safari, Opera.
return (new XMLSerializer()).serializeToString(xmlNode);
} catch (e) {
try {
// Internet Explorer.
return xmlNode.xml;
} catch (e) {
this.addProblem('Xmlserializer not supported', e);
return false;
}
}
}
},
// Add a error/problem message to the error display
addProblem: function(message, problem) {
this.problemsPanel.html(message + "<br/>");
if (problem !== undefined) {
if (problem.substring) {
this.problemsPanel.append(problem.replace(/</g, "<").replace(/>/g, ">"));
} else {
this.problemsPanel.append(problem.message.replace(/</g, "<").replace(/>/g, ">"));
}
}
console.error(problem);
this.refreshProblemPanel();
},
// Clear the listing of errors
clearProblemPanel: function() {
this.problemsPanel.hide();
},
// Update whether or not the error panel is displayed
refreshProblemPanel: function() {
if (this.problemsPanel.html() == "") {
this.problemsPanel.hide("fast");
} else {
this.problemsPanel.show("fast");
}
},
// Performs an equality comparison between two nodes, returning true if they match namespace uri and element name