source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/applications/admin/static/js/ajax_editor.js

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 8.9 KB
Line 
1var template_js = '<p class="repo-name">{{{a_tag}}}</p><small>{{address}}</small>';
2
3function prepareDataForSave(name, data) {
4  var obj = new Object();
5  obj.Name = name;
6  obj.Data = data;
7  return obj;
8}
9
10function prepareMultiPartPOST(data) {
11  // var boundary = 'sPlItME' + Math.floor(Math.random()*10000);
12  var boundary = '' + Math.floor(Math.random() * 10000);
13  var reqdata = '--' + boundary + '\r\n';
14  //console.log(data.length);
15  for(var i = 0; i < data.length; i++) {
16    reqdata += 'content-disposition: form-data; name="';
17    reqdata += data[i].Name + '"';
18    reqdata += "\r\n\r\n";
19    reqdata += data[i].Data;
20    reqdata += "\r\n";
21    reqdata += '--' + boundary + '\r\n';
22  }
23  return new Array(reqdata, boundary);
24}
25
26function on_error() {
27  $("input[name='saved_on']").attr('style', 'background-color:red');
28  $("input[name='saved_on']").val('communication error');
29}
30
31function doHighlight(highlight) {
32  // Put the cursor at the offending line:
33  editor.setCursor({
34    line: highlight.lineno - 1,
35    ch: highlight.offset + 1
36  });
37}
38
39
40function doClickSave() {
41  var currentTabID = '#' + $('#edit_placeholder div.tab-pane.active').attr('id');
42  var editor = $(currentTabID + ' textarea').data('editor');
43  var data = editor.getValue();
44  var dataForPost = prepareMultiPartPOST(new Array(
45    prepareDataForSave('data', data),
46    prepareDataForSave('file_hash',
47      $(currentTabID + " input[name='file_hash']").val()),
48    prepareDataForSave('saved_on',
49      $(currentTabID + " input[name='saved_on']").val()),
50    prepareDataForSave('saved_on',
51      $(currentTabID + " input[name='saved_on']").val()),
52    prepareDataForSave('from_ajax', 'true')));
53  // console.info(area.textarea.value);
54  $(currentTabID + " input[name='saved_on']").attr('style',
55    'background-color:yellow');
56  $(currentTabID + " input[name='saved_on']").val('saving now...')
57  currentUrl = $(currentTabID + ' form').attr('action');
58  $.ajax({
59    type: "POST",
60    contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
61    url: currentUrl,
62    dataType: "json",
63    data: dataForPost[0],
64    timeout: 5000,
65    beforeSend: function (xhr) {
66      xhr.setRequestHeader('web2py-component-location',
67        document.location);
68      xhr.setRequestHeader('web2py-component-element',
69        'doClickSave');
70    },
71    success: function (json, text, xhr) {
72      $(editor).data('saved', true); // Set as saved
73      editor.on("change", store_changes_function); // Re-enable change watcher
74      // reenable disabled submit button
75      var t = $("input[name='save']");
76      t.attr('class', '');
77      t.attr('disabled', '');
78          var flash = xhr.getResponseHeader('web2py-component-flash');
79      if(flash) {
80        $('.w2p_flash').html(decodeURIComponent(flash))
81          .append('<a href="#" class="close">&times;</a>')
82          .slideDown();
83      } else $('.w2p_flash').hide();
84      try {
85        if(json.error) {
86          window.location.href = json.redirect;
87        } else {
88          // console.info( json.file_hash );
89          $(currentTabID + " input[name='file_hash']").val(json.file_hash);
90          $(currentTabID + " input[name='saved_on']").val(json.saved_on);
91          if(json.highlight) {
92            doHighlight(json.highlight);
93          } else {
94            $(currentTabID + " input[name='saved_on']").attr('style', 'background-color:#99FF99');
95            //$(".flash").delay(1000).fadeOut('slow');
96          }
97          // console.info($("input[name='file_hash']").val());
98          var output = '<b>exposes:</b> ';
99          for(var i in json.functions) {
100            output += ' <a target="_blank" href="/' + json.application + '/' + json.controller + '/' + json.functions[i] + '">' + json.functions[i] + '</a>,';
101          }
102          if(output != '<b>exposes:</b> ') {
103            $(currentTabID + " .exposed").html(output.substring(0, output.length - 1));
104          }
105        }
106      } catch(e) {
107        on_error();
108      }
109    },
110    error: function (json) {
111      on_error();
112    }
113  });
114  return false;
115}
116
117function getActiveEditor() {
118  var currentTabID = '#' + $('#edit_placeholder div.tab-pane.active').attr('id');
119  var editor = $(currentTabID + ' textarea').data('editor');
120  return editor;
121}
122
123function getSelectionRange() {
124  var editor = getActiveEditor();
125  var sel = {};
126  sel['start'] = editor.getCursor(true).line;
127  sel['end'] = editor.getCursor(false).line;
128  sel['data'] = '';
129  return sel;
130}
131
132function doToggleBreakpoint(filename, url, sel) {
133  var editor = getActiveEditor();
134  if(sel == null) {
135    // use cursor position to determine the breakpoint line
136    // (gutter already tell us the selected line)
137    sel = getSelectionRange();
138  }
139  var dataForPost = prepareMultiPartPOST(new Array(
140    prepareDataForSave('filename', filename),
141    prepareDataForSave('sel_start', sel["start"]),
142    prepareDataForSave('sel_end', sel["end"]),
143    prepareDataForSave('data', sel['data'])));
144  $.ajax({
145    type: "POST",
146    contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
147    url: url,
148    dataType: "json",
149    data: dataForPost[0],
150    timeout: 5000,
151    beforeSend: function (xhr) {
152      xhr.setRequestHeader('web2py-component-location',
153        document.location);
154      xhr.setRequestHeader('web2py-component-element',
155        'doSetBreakpoint');
156    },
157    success: function (json, text, xhr) {
158      // show flash message (if any)
159      var flash = xhr.getResponseHeader('web2py-component-flash');
160      if(flash) {
161        $('.w2p_flash').html(decodeURIComponent(flash))
162          .append('<a href="#" class="close">&times;</a>')
163          .slideDown();
164      } else $('.w2p_flash').hide();
165      try {
166        if(json.error) {
167          window.location.href = json.redirect;
168        } else {
169          if(json.ok == true) {
170            // mark the breakpoint if ok=True
171            editor.setGutterMarker(json.lineno - 1, "breakpoints", makeMarker());
172          } else if(json.ok == false) {
173            // remove mark if ok=False
174            editor.setGutterMarker(json.lineno - 1, "breakpoints", null);
175          }
176        }
177      } catch(e) {
178        on_error();
179      }
180    },
181    error: function (json) {
182      on_error();
183    }
184  });
185  return false;
186}
187
188// on load, update all breakpoints markers:
189
190function doListBreakpoints(filename, url, editor) {
191  var dataForPost = prepareMultiPartPOST(new Array(
192    prepareDataForSave('filename', filename)
193  ));
194  $.ajax({
195    type: "POST",
196    contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
197    url: url,
198    dataType: "json",
199    data: dataForPost[0],
200    timeout: 5000,
201    beforeSend: function (xhr) {
202      xhr.setRequestHeader('web2py-component-location',
203        document.location);
204      xhr.setRequestHeader('web2py-component-element',
205        'doListBreakpoints');
206    },
207    success: function (json, text, xhr) {
208      try {
209        if(json.error) {
210          window.location.href = json.redirect;
211        } else {
212          var editor = getActiveEditor();
213          for(i in json.breakpoints) {
214            lineno = json.breakpoints[i];
215            // mark the breakpoint if ok=True
216            editor.setGutterMarker(lineno - 1, "breakpoints", makeMarker());
217          }
218        }
219      } catch(e) {
220        on_error();
221      }
222    },
223    error: function (json) {
224      on_error();
225    }
226  });
227  return false;
228}
229
230function makeMarker() {
231  var marker = document.createElement("div");
232  marker.style.color = "#822";
233  marker.innerHTML = "●";
234  marker.className = "breakpoint";
235  return marker;
236}
237
238
239function keepalive(url) {
240  $.ajax({
241    type: "GET",
242    url: url,
243    timeout: 1000,
244    success: function () {},
245    error: function (x) {
246      on_error();
247    }
248  });
249}
250
251function load_file(url, lineno) {
252  $.getJSON(url, function (json) {
253      if(typeof (json['plain_html']) !== undefined) {
254        if($('#' + json['id']).length === 0 || json['force'] === true) {
255          // Create a tab and put the code in it
256          var tab_header = '<li><a title="'+ json['filename'] +'" data-path="' + json['filename'] + '" href="#' + json['id'] + '" data-toggle="tab"><button type="button" class="close">&times;</button>' + json['realfilename'] + '</a></li>';
257          var tab_body = '<div id="' + json['id'] + '" class="tab-pane fade in " >' + json['plain_html'] + '</div>';
258          if(json['force'] === false) {
259            $('#myTabContent').append($(tab_body)); // First load the body
260            $('#filesTab').append($(tab_header));   // Then load the header which trigger the shown event
261          } else {
262            $('#' + json['id']).html($(tab_body));
263          }
264        }
265        $("a[href='#" + json['id'] + "']").trigger('click', lineno);
266      }
267  }).fail(function() {
268      on_error();
269  });
270  return false;
271}
272
273function set_font(editor, incr) {
274  var fontSize = '';
275  if(incr !== 0) {
276    fontSize = parseInt($(editor.getWrapperElement()).css('font-size'));
277    fontSize = fontSize + incr + "px";
278  }
279  $(editor.getWrapperElement()).css('font-size', fontSize);
280  editor.refresh();
281}
282
283
Note: See TracBrowser for help on using the repository browser.