Hello to All,

i have started to work on something called "ajax" code editor.
What exactly does it mean?

   1. No need for reloads on editor page (just think about undo redo in
   editor window)
   2. No "terrible loss of code" if you are logged out (application
   timeout... with that "great" redirect to login page...)
   3. speed gain

Current implementation has "save" functionality only and it works just from
firefox (konqueror ignores my jquery code and same for opera...).
Most coding as it would be expected should (and is) done on client side and
will degrade depending if user use javascript at all - no script no ajax
anyway.
All messages in current stage are json passed to client.
Patch against latest SVN from google code

P.S. Comments welcome and needed (to continue on this idea or not)
-- 
"Only two things are infinite, the universe and human stupidity, and I'm not
sure about the former."-Albert Einstein

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

diff '--exclude=*~' '--exclude=.svn' -uNr web2py_orig/applications/admin//controllers/default.py web2py/applications/admin//controllers/default.py
--- web2py_orig/applications/admin//controllers/default.py	2009-02-22 02:22:10.000000000 +0100
+++ web2py/applications/admin//controllers/default.py	2009-02-22 18:33:33.000000000 +0100
@@ -425,6 +425,9 @@
 
 def edit():
     """ File edit handler """
+    # Load json only if it is ajax edited...
+    if request.vars.has_key('from_ajax'):
+        import gluon.contrib.simplejson as json
 
     filename = '/'.join(request.args)
 
@@ -450,7 +453,10 @@
             data1 = open(path, 'r').read()
         except IOError:
             session.flash = 'Invalid action'
-            redirect(URL(r=request, f='site'))
+            if request.vars.has_key('from_ajax'):
+                 return json.dumps({'error': session.flash})
+            else:
+                redirect(URL(r=request, f='site'))
 
         file_hash = md5_hash(data)
         open(path, 'w').write(data)
@@ -461,7 +467,10 @@
             data = open(path, 'r').read()
         except IOError:
             session.flash = 'Invalid action'
-            redirect(URL(r=request, f='site'))
+            if request.vars.has_key('from_ajax'):
+                return json.dumps({'error': session.flash})
+            else:
+                redirect(URL(r=request, f='site'))
 
         file_hash = md5_hash(data)
 
@@ -469,7 +478,10 @@
             session.flash = T('file changed on disk')
             data = request.vars.data.replace('\r\n', '\n').strip() + '\n'
             open(path + '.1', 'w').write(data)
-            redirect(URL(r=request, f='resolve', args=request.args))
+            if request.vars.has_key('from_ajax'):
+                return json.dumps({'error': session.flash, 'resolve': 'true'})
+            else:
+                redirect(URL(r=request, f='resolve', args=request.args))
         elif request.vars.data:
             open(path + '.bak', 'w').write(data)
             data = request.vars.data.replace('\r\n', '\n').strip() + '\n'
@@ -504,15 +516,18 @@
     else:
         (controller, functions) = (None, None)
 
-    return dict(app=request.args[0],
-                filename=filename,
-                filetype=filetype,
-                data=data,
-                edit_controller=edit_controller,
-                file_hash=file_hash,
-                controller=controller,
-                functions=functions)
-
+    if request.vars.has_key('from_ajax'):
+        return json.dumps({'file_hash': file_hash})
+        #return response.json(file_hash) # maybe?
+    else:
+        return dict(app=request.args[0],
+                    filename=filename,
+                    filetype=filetype,
+                    data=data,
+                    edit_controller=edit_controller,
+                    file_hash=file_hash,
+                    controller=controller,
+                    functions=functions)
 
 def resolve():
     """  """
diff '--exclude=*~' '--exclude=.svn' -uNr web2py_orig/applications/admin//static/ajax_editor.js web2py/applications/admin//static/ajax_editor.js
--- web2py_orig/applications/admin//static/ajax_editor.js	1970-01-01 01:00:00.000000000 +0100
+++ web2py/applications/admin//static/ajax_editor.js	2009-02-22 19:03:31.000000000 +0100
@@ -0,0 +1,45 @@
+function prepareDataForSave(name,data) {
+	var obj = new Object();
+	obj.Name = name;
+	obj.Data = data;
+	return obj;
+}
+
+function prepareMultiPartPOST(data) {
+	var boundary = 'sPlItME' + Math.floor(Math.random()*10000);
+	var reqdata = '--' + boundary;
+	//console.log(data.length);
+	for (var i=0;i < data.length;i++)
+	{
+		reqdata += "\r\n" ;
+		reqdata += 'content-disposition: form-data; name="' + data[i].Name + '"';
+		reqdata += "\r\n" ;
+		reqdata +=  data[i].Data;
+		reqdata += "\r\n" ;
+		reqdata += '--' + boundary;
+	}
+	return new Array(reqdata,boundary);
+}
+
+function doClickSave() {
+	var dataForPost = prepareMultiPartPOST(
+			new Array(
+				prepareDataForSave('data', area.textarea.value),
+				prepareDataForSave('file_hash', $("#file_hash").val()),
+				prepareDataForSave('from_ajax','true')
+				)
+			)
+	console.info(area.textarea.value);
+	$.ajax({
+		type: "POST",
+		contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
+		url: self.location.href,
+		dataType: "json",
+		data: dataForPost[0],
+		success: function(json){
+			//console.info( json.file_hash );
+			$("#file_hash").val(json.file_hash);
+			//console.info($('#file_hash').val());
+		}
+	});
+}
diff '--exclude=*~' '--exclude=.svn' -uNr web2py_orig/applications/admin//views/default/edit.html web2py/applications/admin//views/default/edit.html
--- web2py_orig/applications/admin//views/default/edit.html	2009-02-22 02:22:09.000000000 +0100
+++ web2py/applications/admin//views/default/edit.html	2009-02-22 19:02:49.000000000 +0100
@@ -2,7 +2,12 @@
 <script language="Javascript" type="text/javascript" src="{{=URL(r=request,c='static',f='edit_area/edit_area_full.js')}}"></script><script language="Javascript" type="text/javascript">
 editAreaLoader.init({id: "body",start_highlight: true,allow_resize: "both",allow_toggle: true,language: "en",syntax: "{{=filetype}}",replace_tab_by_spaces: 4});
 </script>
+<script language="Javascript" type="text/javascript" src="/{{=request.application}}/static/ajax_editor.js"></script><script language="Javascript" type="text/javascript">
+$(document).ready(function(){
+     $("form").bind("submit", function() { doClickSave();return false; });
+});
 
+</script>
 <h1>Editing file "{{=filename}}"</h1>
 
 {{if functions:}}

Reply via email to