3 * $Id: Dialogs.js,v 1.5 2013-01-29 17:23:31 gaudenz Exp $
4 * Copyright (c) 2006-2012, JGraph Ltd
7 * Constructs a new dialog.
10 function createCheckbox(value) {
11 var input = document.createElement('input');
12 input.setAttribute('type', 'checkbox');
14 input.setAttribute('checked', true);
19 function createDropdownFromApi(path, value, option_name, option_value, editorUi, paginated) {
20 var req = new XMLHttpRequest();
21 req.open('GET', path, false); // `false` makes the request synchronous
22 req.setRequestHeader('Authorization', 'Bearer ' + editorUi.editor.data.token);
25 if (req.status === 200) {
26 res = JSON.parse(req.response);
28 var input = document.createElement('select');
29 var option = document.createElement('option');
30 option.text = '-- None --';
32 input.appendChild(option);
33 var selectValues = paginated ? res.rows : res;
34 selectValues.forEach(function(elem) {
35 option = document.createElement('option');
36 option.text = elem[option_name];
37 option.value = elem[option_value];
38 option.selected = (elem[option_value] == value);
39 input.appendChild(option);
41 input.className = 'form-control select2';
46 function createGroupedDropdownFromApi(path, value, option_name, option_value, editorUi, paginated, associationField) {
47 var req = new XMLHttpRequest();
48 req.open('GET', path, false); // `false` makes the request synchronous
49 req.setRequestHeader('Authorization', 'Bearer ' + editorUi.editor.data.token);
52 if (req.status === 200) {
53 res = JSON.parse(req.response);
55 var input = document.createElement('select');
56 var option = document.createElement('option');
57 option.text = '-- None --';
59 input.appendChild(option);
60 var selectValues = paginated ? res.rows : res;
63 mainFilter[associationField] = null;
64 var mainValues = _.filter(selectValues, mainFilter);
66 mainValues.forEach(function(elem) {
67 option = document.createElement('option');
68 option.className = 'select-group-father';
69 option.text = elem[option_name].toUpperCase();
70 option.value = elem[option_value];
71 option.selected = (elem[option_value] == value);
72 input.appendChild(option);
73 groupFilter[associationField] = elem[option_value];
74 groupValues = _.filter(selectValues, groupFilter);
75 groupValues.forEach(function(elem) {
76 option = document.createElement('option');
77 option.className = 'select-group-son';
78 option.text = '-' + _.capitalize(elem[option_name]);
79 option.value = elem[option_value];
80 option.selected = (elem[option_value] == value);
81 input.appendChild(option);
84 input.className = 'form-control select2';
89 function createDropdownFromArray(array, value) {
90 var input = document.createElement('select');
92 for (var item in array) {
93 var option = document.createElement('option');
94 option.text = array[item];
96 if (value > 0 || value != '')
97 option.selected = (value === item) ? true : false;
99 input.appendChild(option);
102 input.className = 'form-control select2';
107 function Dialog(editorUi, elt, w, h, modal, closable, onClose) {
110 if (mxClient.IS_IE && document.documentMode != 9) {
117 var left = Math.max(0, Math.round((document.body.scrollWidth - w) / 2));
118 var top = Math.max(0, Math.round((Math.max(document.body.scrollHeight,
119 document.documentElement.scrollHeight) - h) / 3));
121 var div = editorUi.createDiv('geDialog');
122 div.className = 'modal fade in center';
123 div.style.display = 'block';
124 div.style.paddingRight = '12px';
125 // div.style.width = w + 'px';
126 // div.style.height = h + 'px';
127 // div.style.left = left + 'px';
128 // div.style.top = top + 'px';
130 var divModalDialog = editorUi.createDiv('geModalDialog');
131 divModalDialog.className = 'modal-dialog';
133 divModalDialog.appendChild(elt);
134 div.appendChild(divModalDialog);
136 if (this.bg == null) {
137 this.bg = editorUi.createDiv('background');
138 this.bg.className = 'modal-backdrop fade in';
140 if (mxClient.IS_QUIRKS) {
141 new mxDivResizer(this.bg);
146 document.body.appendChild(this.bg);
149 document.body.appendChild(div);
151 this.onDialogClose = onClose;
152 this.container = div;
156 * Removes the dialog from the DOM.
158 Dialog.prototype.close = function() {
159 if (this.onDialogClose != null) {
160 this.onDialogClose();
161 this.onDialogClose = null;
164 this.container.parentNode.removeChild(this.container);
165 this.bg.parentNode.removeChild(this.bg);
169 * Constructs a new open dialog.
171 function ImportDialog(editorUi) {
173 var content = editorUi.createDiv('modal-content');
174 var header = editorUi.createDiv('modal-header');
175 var body = editorUi.createDiv('modal-body');
176 var footer = editorUi.createDiv('modal-footer');
179 var title = editorUi.createHeader('h4');
180 mxUtils.write(title, mxResources.get('import') + ' XML');
182 var x = mxUtils.button('', function() {
183 editorUi.hideDialog();
185 x.className = 'close';
187 header.appendChild(x);
188 header.appendChild(title);
192 var row = editorUi.createDiv('row');
193 var col1 = editorUi.createDiv('col-md-12');
195 var textarea = document.createElement('textarea');
196 textarea.style.width = '100%';
197 textarea.style.height = '374px';
199 var input = document.createElement('input');
201 input.setAttribute('accept', 'text/xml');
203 input.addEventListener('change', function readSingleFile(evt) {
204 //Retrieve the first (and only!) File from the FileList object
205 var f = evt.target.files[0];
208 if (f.type === 'text/xml') {
209 var r = new FileReader();
210 r.onload = function(e) {
211 var contents = e.target.result;
212 mxUtils.write(textarea, contents);
216 alert('Failed to load format file');
219 alert('Failed to load file');
223 col1.appendChild(input);
224 col1.appendChild(textarea);
226 row.appendChild(col1);
227 body.appendChild(row);
231 var save = mxUtils.button(mxResources.get('import'), mxUtils.bind(this,
233 var doc = mxUtils.parseXml(textarea.value);
234 editorUi.editor.setGraphXml(doc.documentElement);
235 editorUi.hideDialog();
237 save.className = 'btn blue';
239 var close = mxUtils.button(mxResources.get('cancel'), function() {
240 editorUi.hideDialog();
242 close.className = 'btn default';
244 footer.appendChild(save);
245 footer.appendChild(close);
249 content.appendChild(header);
250 content.appendChild(body);
251 content.appendChild(footer);
254 this.container = content;
259 * Constructs a new about dialog.
261 function AboutDialog(editorUi) {
262 var content = editorUi.createDiv('modal-content');
263 var header = editorUi.createDiv('modal-header');
264 var body = editorUi.createDiv('modal-body');
265 var footer = editorUi.createDiv('modal-footer');
268 var title = editorUi.createHeader('h4');
269 mxUtils.write(title, mxResources.get('about') + ' Cally Square');
271 var x = mxUtils.button('', function() {
272 editorUi.hideDialog();
274 x.className = 'close';
276 header.appendChild(x);
277 header.appendChild(title);
281 var img = document.createElement('img');
282 img.style.border = '0px';
283 img.setAttribute('width', '176');
284 img.setAttribute('width', '151');
285 img.setAttribute('src', IMAGE_PATH + '/logo.png');
286 body.appendChild(img);
288 mxUtils.write(body, 'Powered by Xenialab ' + mxClient.VERSION);
290 var link = document.createElement('a');
291 link.setAttribute('href', 'http://www.callysquare.com/');
292 link.setAttribute('target', '_blank');
293 mxUtils.write(link, 'www.callysquare.com');
294 body.appendChild(link);
299 var close = mxUtils.button(mxResources.get('close'), function() {
300 editorUi.hideDialog();
302 close.className = 'btn default';
304 footer.appendChild(close);
308 content.appendChild(header);
309 content.appendChild(body);
310 content.appendChild(footer);
313 this.container = content;
317 * Constructs a new save dialog.
319 function SaveDialog(editorUi) {
321 var content = editorUi.createDiv('modal-content');
322 var header = editorUi.createDiv('modal-header');
323 var body = editorUi.createDiv('modal-body');
324 var footer = editorUi.createDiv('modal-footer');
327 var title = editorUi.createHeader('h4');
328 mxUtils.write(title, mxResources.get('saveAs'));
330 var x = mxUtils.button('', function() {
331 editorUi.hideDialog();
333 x.className = 'close';
335 header.appendChild(x);
336 header.appendChild(title);
342 var value = editorUi.editor.getOrCreateFilename();
344 var row = editorUi.createDiv('row');
345 var col1 = editorUi.createDiv('col-md-4');
346 var col2 = editorUi.createDiv('col-md-8');
348 var label = document.createElement('label');
349 label.className = 'control-label pull-right';
350 mxUtils.write(label, mxResources.get(name));
351 col1.appendChild(label);
353 var select = document.createElement('input');
354 select.setAttribute('value', value + '_copy');
355 select.setAttribute('id', id)
356 select.className = 'form-control';
357 col2.appendChild(select);
359 row.appendChild(col1);
360 row.appendChild(col2);
361 body.appendChild(row);
365 var save = mxUtils.button(mxResources.get('save'), function() {
366 editorUi.saveAs(select.value);
367 editorUi.hideDialog();
369 save.className = 'btn blue';
371 var close = mxUtils.button(mxResources.get('cancel'), function() {
372 editorUi.hideDialog();
374 close.className = 'btn default';
376 footer.appendChild(save);
377 footer.appendChild(close);
381 content.appendChild(header);
382 content.appendChild(body);
383 content.appendChild(footer);
386 this.container = content;
387 //nameInput.setAttribute('value', editorUi.editor.getOrCreateFilename());
391 * Constructs a new save dialog.
393 function NewDialog(editorUi) {
395 var content = editorUi.createDiv('modal-content');
396 var header = editorUi.createDiv('modal-header');
397 var body = editorUi.createDiv('modal-body');
398 var footer = editorUi.createDiv('modal-footer');
401 var title = editorUi.createHeader('h4');
402 mxUtils.write(title, mxResources.get('new'));
404 var x = mxUtils.button('', function() {
405 editorUi.hideDialog();
407 x.className = 'close';
409 header.appendChild(x);
410 header.appendChild(title);
416 var value = editorUi.editor.getOrCreateFilename();
418 var row = editorUi.createDiv('row');
419 var col1 = editorUi.createDiv('col-md-4');
420 var col2 = editorUi.createDiv('col-md-8');
422 var label = document.createElement('label');
423 label.className = 'control-label pull-right';
424 mxUtils.write(label, mxResources.get(name));
425 col1.appendChild(label);
427 var select = document.createElement('input');
428 select.setAttribute('value', value + '_new');
429 select.setAttribute('id', id)
430 select.className = 'form-control';
431 col2.appendChild(select);
433 row.appendChild(col1);
434 row.appendChild(col2);
435 body.appendChild(row);
439 var save = mxUtils.button(mxResources.get('new'), function() {
440 editorUi.new(select.value);
441 editorUi.hideDialog();
443 save.className = 'btn blue';
445 var close = mxUtils.button(mxResources.get('cancel'), function() {
446 editorUi.hideDialog();
448 close.className = 'btn default';
450 footer.appendChild(save);
451 footer.appendChild(close);
455 content.appendChild(header);
456 content.appendChild(body);
457 content.appendChild(footer);
460 this.container = content;
461 //nameInput.setAttribute('value', editorUi.editor.getOrCreateFilename());
465 * Constructs a new save dialog.
467 function VariableDialog(editorUi) {
469 var content = editorUi.createDiv('modal-content');
470 var header = editorUi.createDiv('modal-header');
471 var body = editorUi.createDiv('modal-body');
472 var footer = editorUi.createDiv('modal-footer');
475 var title = editorUi.createHeader('h4');
476 mxUtils.write(title, mxResources.get('variable'));
478 var x = mxUtils.button('', function() {
479 editorUi.hideDialog();
481 x.className = 'close';
483 header.appendChild(x);
484 header.appendChild(title);
490 var value = editorUi.editor.getOrCreateFilename();
492 var row = editorUi.createDiv('row');
493 var col1 = editorUi.createDiv('col-md-4');
494 var col2 = editorUi.createDiv('col-md-8');
496 var label = document.createElement('label');
497 label.className = 'control-label pull-right';
498 mxUtils.write(label, mxResources.get(name));
499 col1.appendChild(label);
501 var select = document.createElement('input');
502 select.setAttribute('value', 'variable name');
503 select.setAttribute('id', id)
504 select.className = 'form-control';
505 col2.appendChild(select);
507 row.appendChild(col1);
508 row.appendChild(col2);
509 body.appendChild(row);
513 var save = mxUtils.button(mxResources.get('new'), function() {
514 editorUi.variable(select.value);
515 editorUi.hideDialog();
517 save.className = 'btn blue';
519 var close = mxUtils.button(mxResources.get('cancel'), function() {
520 editorUi.hideDialog();
522 close.className = 'btn default';
524 footer.appendChild(save);
525 footer.appendChild(close);
529 content.appendChild(header);
530 content.appendChild(body);
531 content.appendChild(footer);
534 this.container = content;
535 //nameInput.setAttribute('value', editorUi.editor.getOrCreateFilename());
539 * Constructs a new save dialog.
541 function OpenDialog(editorUi) {
543 var content = editorUi.createDiv('modal-content');
544 var header = editorUi.createDiv('modal-header');
545 var body = editorUi.createDiv('modal-body');
546 var footer = editorUi.createDiv('modal-footer');
549 var title = editorUi.createHeader('h4');
550 mxUtils.write(title, mxResources.get('open'));
552 var x = mxUtils.button('', function() {
553 editorUi.hideDialog();
555 x.className = 'close';
557 header.appendChild(x);
558 header.appendChild(title);
562 var row = editorUi.createDiv('row');
563 var col1 = editorUi.createDiv('col-md-4');
564 var col2 = editorUi.createDiv('col-md-8');
566 var label = document.createElement('label');
567 label.className = 'control-label pull-right';
568 mxUtils.write(label, mxResources.get('name'));
569 col1.appendChild(label);
571 var req = new XMLHttpRequest();
572 req.open('GET', '/api/square/projects', false); // `false` makes the request synchronous
573 req.setRequestHeader('Authorization', 'Bearer ' + editorUi.editor.data.token);
576 if (req.status === 200) {
577 res = JSON.parse(req.response);
580 var select = document.createElement('select');
582 for (var j = 0; j < res.length; j++) {
583 var option = document.createElement('option');
584 option.text = res[j].name;
585 option.value = res[j].id;
586 select.appendChild(option);
588 select.className = 'form-control';
589 col2.appendChild(select);
591 row.appendChild(col1);
592 row.appendChild(col2);
593 body.appendChild(row);
597 var save = mxUtils.button(mxResources.get('open'), function() {
599 console.log(select.value);
600 window.open('square/project/' + select.value, '_blank');
601 editorUi.hideDialog();
603 save.className = 'btn blue';
605 var close = mxUtils.button(mxResources.get('cancel'), function() {
606 editorUi.hideDialog();
608 close.className = 'btn default';
610 footer.appendChild(save);
611 footer.appendChild(close);
615 content.appendChild(header);
616 content.appendChild(body);
617 content.appendChild(footer);
620 this.container = content;
621 //nameInput.setAttribute('value', editorUi.editor.getOrCreateFilename());
626 * Constructs a new edit file dialog.
628 function EditFileDialog(editorUi) {
630 var content = editorUi.createDiv('modal-content');
631 var header = editorUi.createDiv('modal-header');
632 var body = editorUi.createDiv('modal-body');
633 var footer = editorUi.createDiv('modal-footer');
636 var title = editorUi.createHeader('h4');
637 mxUtils.write(title, mxResources.get('edit'));
639 var x = mxUtils.button('', function() {
640 editorUi.hideDialog();
642 x.className = 'close';
644 header.appendChild(x);
645 header.appendChild(title);
649 var row = editorUi.createDiv('row');
650 var col1 = editorUi.createDiv('col-md-12');
652 var textarea = document.createElement('textarea');
653 textarea.style.width = '100%';
654 textarea.style.height = '374px';
655 textarea.value = mxUtils.getPrettyXml(editorUi.editor.getGraphXml());
657 // Enables dropping files
659 function handleDrop(evt) {
660 evt.stopPropagation();
661 evt.preventDefault();
663 if (evt.dataTransfer.files.length > 0) {
664 var file = evt.dataTransfer.files[0];
666 var reader = new FileReader();
667 reader.onload = function(e) {
668 textarea.value = e.target.result;
670 reader.readAsText(file);
674 function handleDragOver(evt) {
675 evt.stopPropagation();
676 evt.preventDefault();
679 // Setup the dnd listeners.
680 textarea.addEventListener('dragover', handleDragOver, false);
681 textarea.addEventListener('drop', handleDrop, false);
683 col1.appendChild(textarea);
685 row.appendChild(col1);
686 body.appendChild(row);
690 var save = mxUtils.button(mxResources.get('save'), function() {
691 var doc = mxUtils.parseXml(textarea.value);
692 editorUi.editor.setGraphXml(doc.documentElement);
693 editorUi.hideDialog();
695 save.className = 'btn blue';
697 var close = mxUtils.button(mxResources.get('cancel'), function() {
698 editorUi.hideDialog();
700 close.className = 'btn default';
702 footer.appendChild(save);
703 footer.appendChild(close);
707 content.appendChild(header);
708 content.appendChild(body);
709 content.appendChild(footer);
712 this.container = content;
716 * Constructs a new export dialog.
718 function ExportDialog(editorUi) {
719 var content = editorUi.createDiv('modal-content');
720 var header = editorUi.createDiv('modal-header');
721 var body = editorUi.createDiv('modal-body form');
722 var footer = editorUi.createDiv('modal-footer');
725 var title = editorUi.createHeader('h4');
726 mxUtils.write(title, mxResources.get('export') + ' XML');
728 var x = mxUtils.button('', function() {
729 editorUi.hideDialog();
731 x.className = 'close';
733 header.appendChild(x);
734 header.appendChild(title);
738 var form = editorUi.createDiv('form-horizontal form-row-seperated');
739 var group = editorUi.createDiv('form-group last');
741 var label = document.createElement('label');
742 label.className = 'col-sm-4 control-label';
743 mxUtils.write(label, mxResources.get('filename'));
745 var input = document.createElement('input');
746 input.setAttribute('value', editorUi.editor.getOrCreateFilename());
747 input.className = 'form-control';
749 var div = editorUi.createDiv('col-sm-8');
750 div.appendChild(input);
752 group.appendChild(label);
753 group.appendChild(div);
754 form.appendChild(group);
756 body.appendChild(form);
760 var save = mxUtils.button(mxResources.get('export'), mxUtils.bind(this,
762 editorUi.save(false);
764 var xml = encodeURIComponent(mxUtils.getXml(editorUi.editor.getGraphXml()));
765 new mxXmlRequest(SAVE_URL + editorUi.editor.data.id + '/download',
766 'filename=' + input.value, 'GET').simulate(document, "_blank");
767 editorUi.hideDialog();
769 save.className = 'btn blue';
771 var close = mxUtils.button(mxResources.get('cancel'), function() {
772 editorUi.hideDialog();
774 close.className = 'btn default';
776 footer.appendChild(save);
777 footer.appendChild(close);
781 content.appendChild(header);
782 content.appendChild(body);
783 content.appendChild(footer);
786 this.container = content;
791 * Constructs a new general dialog.
793 function GeneralDialog(editorUi, cell) {
794 var graph = editorUi.editor.graph;
796 var content = editorUi.createDiv('modal-content');
797 var header = editorUi.createDiv('modal-header');
798 var body = editorUi.createDiv('modal-body form modal-body-scroll');
799 var footer = editorUi.createDiv('modal-footer');
802 var title = editorUi.createHeader('h4');
803 mxUtils.write(title, mxResources.get('edit') + ' ' + mxResources.get(cell.value
806 var x = mxUtils.button('', function() {
807 editorUi.hideDialog();
809 x.className = 'close';
811 header.appendChild(x);
812 header.appendChild(title);
816 var length = cell.value.attributes.length;
817 var form = editorUi.createDiv('form-horizontal form-row-seperated');
819 for (var i = 0; i < length; i++) {
820 var name = cell.value.attributes[i].name;
822 var value = cell.value.attributes[i].value;
824 var group = editorUi.createDiv((i == length - 1) ? 'form-group last' :
827 var label = document.createElement('label');
828 label.className = 'col-sm-4 control-label';
829 mxUtils.write(label, mxResources.get(name));
830 group.appendChild(label);
836 input = createDropdownFromApi('/api/agents', value, 'name', 'id', editorUi, true);
839 input = createDropdownFromApi('/api/voice/queues', value, 'name', 'name', editorUi, true);
842 input = createDropdownFromApi('/api/trunks', value, 'name', 'id', editorUi, true);
845 input = createDropdownFromApi('/api/square/variables', value, 'name', 'id', editorUi, true); //mettere true dopo la pagination
848 input = createDropdownFromArray(ISPEECHASRMODEL, value);
850 case 'ispeech_asr_language':
851 input = createDropdownFromArray(ISPEECHASRLANG, value);
853 case 'ispeech_tts_language':
854 input = createDropdownFromArray(ISPEECHLANG, value);
856 case 'google_tts_language':
857 input = createDropdownFromArray(GOOGLETTSLANG, value);
860 input = createGroupedDropdownFromApi('/api/intervals/all', value, 'name', 'id', editorUi, false, 'IntervalId');
863 input = createDropdownFromApi('/api/square/projects', value, 'name', 'id', editorUi, true);
866 input = createDropdownFromApi('/api/square/odbc', value, 'name', 'id', editorUi, true);
869 input = createDropdownFromApi('/api/uploads', value, 'display_name', 'id', editorUi, false);
877 input = document.createElement('input');
878 input.setAttribute('type', 'number');
879 input.setAttribute('min', 0);
880 input.setAttribute('max', 1000);
881 input.setAttribute('value', value);
882 input.className = 'form-control';
889 input = document.createElement('textarea');
890 input.innerHTML = value;
891 input.className = 'form-control';
894 input = document.createElement('input');
895 input.setAttribute('value', value);
896 input.className = 'form-control';
900 input.setAttribute('id', id)
902 var div = editorUi.createDiv('col-sm-8');
903 div.appendChild(input);
906 if (mxResources.get('help_' + name)) {
907 var help = editorUi.createDiv('p');
908 help.className = 'help-block';
909 mxUtils.write(help, mxResources.get('help_' + name));
910 div.appendChild(help);
913 group.appendChild(div);
914 form.appendChild(group);
916 body.appendChild(form);
920 var save = mxUtils.button(mxResources.get('save'), mxUtils.bind(this,
922 for (var i = 0; i < cell.value.attributes.length; i++) {
923 var id = '_' + cell.value.attributes[i].name;
924 var name = cell.value.attributes[i].name;
925 console.log(name, document.getElementById(id).value);
926 cell.setAttribute(name, document.getElementById(id).value);
929 editorUi.hideDialog();
931 save.className = 'btn blue';
933 var close = mxUtils.button(mxResources.get('cancel'), function() {
934 editorUi.hideDialog();
936 close.className = 'btn default';
938 footer.appendChild(save);
939 footer.appendChild(close);
943 content.appendChild(header);
944 content.appendChild(body);
945 content.appendChild(footer);
948 this.container = content;