Was this page helpful?

Code

    Table of contents
    1. 1. Custom CSS
    <h1>Ajax Forum Using Deki Pages for data storage</h1>
    <form method="post" id="dform">
    <input type="hidden" id="path" name="path" value="{{web.UriEncode(web.UriEncode(page.path..'/data'))}}" /> 
    <span>Response</span>:<br/><textarea name="response">
    </textarea>
    <a class="submit" href="#" onclick="sendform();return false;">Submit</a>
    </form>
    
    <script type="text/javascript">/*<![CDATA[*/ 
    	var dpath = DekiWiki.$("#path").val();
    	var dapi = "/@api/deki/pages/=";
    	var dparams = "/contents?abort=never";
    	var dget = dapi+dpath;
    	var dsection = "";
    	var dtime;
    	var drevision;
    	
    DekiWiki.$('body').ready(function() {
    	retrieveform(dapi+dpath+dparams);
    });
    
    function sendform(formid,sortloc) {
    	
    	DekiWiki.$.ajax({
    		type: "GET",
    		url: dget,
    		async: false,
    		dataType: "xml",
    		success: function(xml) {
    				datetimeraw = DekiWiki.$("date\\.edited", xml).text();
    				datetimeraw =  datetimeraw.replace('T','');
    				datetimeraw =  datetimeraw.replace('Z','');
    				
    				dateraw = datetimeraw.substr(0,10);
    				timeraw = datetimeraw.substr(10);
    				
    				datefix = dateraw.replace(/-/g,'');
    				timefix = timeraw.replace(/:/g,'');
    				
    				drevision = datefix + timefix;
    				dtime = ("&edittime=" + datefix + timefix);
    				dsection = "&section=0";
    				
    		},
    		error: function(msg) {
    				drevision = drettime();
    		}
    	});
    	
    	if (sortloc) {
    		drevision = sortloc;
    	}
    	
    	
    	var ddata = "<div class='item' id='" + drevision + "'><span class='name'>{{save:web.link(user.uri,user.name)}}<\/span>";
    	
    	if (!formid) {
    		formid="dform";	
    	}
    	
    	DekiWiki.$("#" + formid + " > :input").each(function(i){
    		if (DekiWiki.$(this).attr("type") != 'hidden') {
    			ddata += "<div class='" + DekiWiki.$(this).attr('name') +"'>" + DekiWiki.$(this).val()+ "<\/div>";
    		}
    	});
    	
    	ddata += "<\/div>";
    	
    	var duri = dapi+dpath+dparams+dtime+dsection;
    	
    	DekiWiki.$.ajax({
    	   type: "POST",
    	   url: duri,
    	   data: ddata,
    	   success: function(msg){
    	    	retrieveform(dapi+dpath+dparams)
    	   }, 
    	   error: function(msg) {
    			console.log( msg.responseText ); 
    	   }
    	 });
    	
    
    
    } 
    
    function fixDT(dtstr) {
    	if (String(dtstr).length==1) {
    		return ("0" + String(dtstr))
    	} else {
    		return String(dtstr)
    	}
    }
    
    function drettime() {
    	var d = new Date();
    	var yr = fixDT(d.getFullYear());
    	var mt = fixDT(d.getMonth()+1);
    	var dy = fixDT(d.getDate());
    	var hr = fixDT(d.getHours());
    	var mn = fixDT(d.getMinutes());
    	var se = fixDT(d.getSeconds());
    	return (yr + mt + dy + hr + mn + se);
    }
    
    function replyformsubmit(itemid) {
    	
    	var rstamp = drettime();
    	
    	sendform("f-" + itemid, (itemid + "-" + rstamp));
    	replyformhide(itemid);
    	
    }
    
    function retrieveform(pgPath) {
    	DekiWiki.$.ajax({
    	type: "GET",
    	url: pgPath,
    	async: false,
    	dataType: "xml",
    	success: function(xml) {
    		DekiWiki.$(xml).find('content').each(function(){
    			var rawhtml = DekiWiki.$("body[target!=toc]", this).text();
    			
    			DekiWiki.$("#sortoutput").html('');
    			DekiWiki.$("#formoutput").html('');
    			DekiWiki.$("#formoutput").html(unescape(rawhtml));
    			
    			var dallarray = new Array();
    			var dtopics = new Array();
    			var dsort = new Array();
    			
    			DekiWiki.$('div.item[id]').each(function(){ 
    				dallarray.push(DekiWiki.$(this).attr('id'));	
    			});
    			
    			
    			
    			for (i=0;i<dallarray.length;i++) {
    				if(String(dallarray[i]).indexOf("-")==-1) {
    					dtopics.push(dallarray[i]);
    				}
    			}
    			
    			dtopics.sort();
    			dtopics.reverse();
    			
    			
    			for (i=0;i<dtopics.length;i++) {
    				
    				dsort.push(dtopics[i]);
    				
    				for (k=0;k<dallarray.length;k++) {
    					var dlen = String(dtopics[i]).length+1;
    					if (String(dallarray[k]).substr(0,dlen)==(String(dtopics[i])+'-')) {
    						dsort.push(dallarray[k]);
    					}
    				}
    			}
    
    
    
    			var dsorthtml="";
    			for(i=0;i<dsort.length;i++) {
    				var rsplit = String(dsort[i]).split('-');
    				var rlevel = rsplit.length;
    				dsorthtml += "<div class='item level-" + rlevel + "' id='" + dsort[i] + "'>";
    				dsorthtml += DekiWiki.$("div#"+dsort[i]).html();
    				dsorthtml += "<form id='f-" + dsort[i] + "' class='dform'>";
    				dsorthtml += "<textarea class='dresponse'><\/textarea>";
    				dsorthtml += "<\/form>";
    				if (rlevel <=3) {
    					dsorthtml += "<a class='dreply' id='r-" + dsort[i] + "' onclick='replyform(\"" + dsort[i] + "\")\;return false'>reply<\/a>";
    					dsorthtml += "<a class='dcancel' id='c-" + dsort[i] + "' onclick='replyformhide(\"" + dsort[i] + "\")\;return false'>cancel<\/a>";
    					dsorthtml += "<a class='dcancel' id='s-" + dsort[i] + "' onclick='replyformsubmit(\"" + dsort[i] + "\")\;return false'>submit<\/a>";
    				}
    				dsorthtml += "<\/div>";
    			}
    			
    			DekiWiki.$("#sortoutput").html(dsorthtml);
    		});
    	}
    	});
    }
    
    function replyform(itemid) {
    	DekiWiki.$("#f-" + itemid).slideDown("fast");
    	DekiWiki.$("#c-" + itemid).show();
    	DekiWiki.$("#s-" + itemid).show();
    	DekiWiki.$("#r-" + itemid).hide();
    }
    
    function replyformhide(itemid) {
    	DekiWiki.$("#f-" + itemid).slideUp("fast");
    	DekiWiki.$("#c-" + itemid).hide();
    	DekiWiki.$("#s-" + itemid).hide();
    	DekiWiki.$("#r-" + itemid).show();
    }
    
    
    
    /*]]>*/</script>
    
    <div id="formoutput" style="display:none;">
    &nbsp;&nbsp;
    </div>
    
    <div id="sortoutput">
    &nbsp;&nbsp;
    </div>

    Custom CSS

    Also add this css to the control panel > custom css.

    #sortoutput {
    	max-height:400px;
    	overflow-y:auto;
    	padding-right:10px;
    }
    
    #sortoutput div.item {
    	padding:5px;
    	border-bottom:1px dotted #ccc;
    	font-size:11px;
    	overflow:hidden;
    }
    
    #sortoutput div.item div {
    	margin-bottom:2px;
    }
    
    #sortoutput div.name {
    	font-weight:bold;
    }
    
    #sortoutput a.dcancel,
    #sortoutput a.dreply {
    	font-weight:bold;
    	float:right;
    	cursor:pointer;
    	margin-left:20px;
    }
    
    #sortoutput .dcancel,
    #sortoutput .dform {
    	display:none;
    }
    #sortoutput .level-1 {
    	
    }
    #sortoutput .level-2 {
    	margin-left:30px;
    }
    #sortoutput .level-3 {
    	margin-left:60px;
    }
    #sortoutput .level-4 {
    	margin-left:90px;
    }
    #sortoutput .level-5 {
    	margin-left:120px;
    }
    #sortoutput .level-6 {
    	margin-left:150px;
    }
    #sortoutput .level-7 {
    	margin-left:180px;
    }
    #sortoutput .level-8 {
    	margin-left:210px;
    }
    #sortoutput .level-9 {
    	margin-left:240px;
    }
    #sortoutput .level-10 {
    	margin-left:270px;
    }
    #dform a {
    	cursor:pointer;
    	float:right;
    }
    #dform {
    	overflow:hidden;
    	padding-bottom:20px;
    	-moz-border-radius:5px;
    	padding:5px;
    	background-color:#efefef;
    }
    #dform textarea {
    	margin-bottom:5px;
    }
    
    #sortoutput textarea,
    #dform textarea {
    	width:95%;
    -moz-border-radius:5px;
    }
    Was this page helpful?
    Tag page
    Viewing 2 of 2 comments: view all
    Damien - are there other options for "section=0" on line 42? Could this be used to overwrite just one section on a page?
    Posted 09:19, 15 Mar 2009
    lktest:

    The section is based upon the number of heading tags on the page that are (1) after the title heading tag and (2) have the same heading tag number. For example, if you had a page named "foo" and there were 3 sections defined with the <h3> tag, using section=0 would access the first section, section=1 would access the second, etc.
    Posted 09:07, 16 Mar 2009
    Viewing 2 of 2 comments: view all
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by