source: other-projects/nz-flag-design/trunk/design-2d/Original editor.method.ac/editor/extensions/ext-server_opensave.js@ 29468

Last change on this file since 29468 was 29468, checked in by sjs49, 9 years ago

Initial commit for editor.method.ac for flag design

  • Property svn:executable set to *
File size: 4.8 KB
Line 
1/*
2 * ext-server_opensave.js
3 *
4 * Licensed under the Apache License, Version 2
5 *
6 * Copyright(c) 2010 Alexis Deveria
7 *
8 */
9
10methodDraw.addExtension("server_opensave", {
11 callback: function() {
12
13 //var save_svg_action = 'extensions/filesave.php';
14 //var save_png_action = 'extensions/filesave.php';
15
16 // Create upload target (hidden iframe)
17 var target = $('<iframe name="output_frame" />').hide().appendTo('body');
18
19 //methodDraw.setCustomHandlers({
20 // save: function(win, data) {
21 // var svg = "<?xml version=\"1.0\"?>\n" + data;
22 //
23 // var title = svgCanvas.getDocumentTitle();
24 // var filename = title.replace(/[^a-z0-9\.\_\-]+/gi, '_');
25 //
26 // var form = $('<form>').attr({
27 // method: 'post',
28 // action: save_svg_action,
29 // target: 'output_frame'
30 // }) .append('<input type="hidden" name="output_svg" value="' + encodeURI(svg) + '">')
31 // .append('<input type="hidden" name="filename" value="' + filename + '">')
32 // .appendTo('body')
33 // .submit().remove();
34 // },
35 // pngsave: function(win, data) {
36 // var issues = data.issues;
37 //
38 // if(!$('#export_canvas').length) {
39 // $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
40 // }
41 // var c = $('#export_canvas')[0];
42 //
43 // c.width = svgCanvas.contentW;
44 // c.height = svgCanvas.contentH;
45 // canvg(c, data.svg, {renderCallback: function() {
46 // var datauri = c.toDataURL('image/png');
47 //
48 // var uiStrings = methodDraw.uiStrings;
49 // var note = '';
50 //
51 // // Check if there's issues
52 // if(issues.length) {
53 // var pre = "\n \u2022 ";
54 // note += ("\n\n" + pre + issues.join(pre));
55 // }
56 //
57 // if(note.length) {
58 // alert(note);
59 // }
60 //
61 // var title = svgCanvas.getDocumentTitle();
62 // var filename = title.replace(/[^a-z0-9\.\_\-]+/gi, '_');
63 //
64 // var form = $('<form>').attr({
65 // method: 'post',
66 // action: save_png_action,
67 // target: 'output_frame'
68 // }) .append('<input type="hidden" name="output_png" value="' + datauri + '">')
69 // .append('<input type="hidden" name="filename" value="' + filename + '">')
70 // .appendTo('body')
71 // .submit().remove();
72 // }});
73 //
74 //
75 // }
76 //});
77
78 // Do nothing if client support is found
79 if(window.FileReader) return;
80
81 var cancelled = false;
82
83 // Change these to appropriate script file
84 var open_svg_action = 'extensions/fileopen.php?type=load_svg';
85 var import_svg_action = 'extensions/fileopen.php?type=import_svg';
86 var import_img_action = 'extensions/fileopen.php?type=import_img';
87
88 // Set up function for PHP uploader to use
89 methodDraw.processFile = function(str64, type) {
90 if(cancelled) {
91 cancelled = false;
92 return;
93 }
94
95 $('#dialog_box').hide();
96
97 if(type != 'import_img') {
98 var xmlstr = svgCanvas.Utils.decode64(str64);
99 }
100
101 switch ( type ) {
102 case 'load_svg':
103 svgCanvas.clear();
104 svgCanvas.setSvgString(xmlstr);
105 methodDraw.updateCanvas();
106 break;
107 case 'import_svg':
108 svgCanvas.importSvgString(xmlstr);
109 methodDraw.updateCanvas();
110 break;
111 case 'import_img':
112 svgCanvas.setGoodImage(str64);
113 break;
114 }
115 }
116
117 // Create upload form
118 var open_svg_form = $('<form>');
119 open_svg_form.attr({
120 enctype: 'multipart/form-data',
121 method: 'post',
122 action: open_svg_action,
123 target: 'output_frame'
124 });
125
126 // Create import form
127 var import_svg_form = open_svg_form.clone().attr('action', import_svg_action);
128
129 // Create image form
130 var import_img_form = open_svg_form.clone().attr('action', import_img_action);
131
132 // It appears necessory to rebuild this input every time a file is
133 // selected so the same file can be picked and the change event can fire.
134 function rebuildInput(form) {
135 form.empty();
136 var inp = $('<input type="file" name="svg_file">').appendTo(form);
137
138
139 function submit() {
140 // This submits the form, which returns the file data using methodDraw.uploadSVG
141 form.submit();
142
143 rebuildInput(form);
144 $.process_cancel("Uploading...", function() {
145 cancelled = true;
146 $('#dialog_box').hide();
147 });
148 }
149
150 if(form[0] == open_svg_form[0]) {
151 inp.change(function() {
152 // This takes care of the "are you sure" dialog box
153 methodDraw.openPrep(function(ok) {
154 if(!ok) {
155 rebuildInput(form);
156 return;
157 }
158 submit();
159 });
160 });
161 } else {
162 inp.change(function() {
163 // This submits the form, which returns the file data using methodDraw.uploadSVG
164 submit();
165 });
166 }
167 }
168
169 // Create the input elements
170 rebuildInput(open_svg_form);
171 rebuildInput(import_svg_form);
172 rebuildInput(import_img_form);
173
174 // Add forms to buttons
175 $("#tool_open").show().prepend(open_svg_form);
176 $("#tool_import").show().prepend(import_svg_form);
177 $("#tool_image").prepend(import_img_form);
178 }
179});
180
Note: See TracBrowser for help on using the repository browser.