Snapshot Dynamic Page

    Table of contents
    1. 1. What is it?
    2. 2. The Code

    What is it?

    The snapshot template code below is designed to grab the current page's content, and create a subpage directly below it with all content (including dynamic content such as database calls) saved statically. This allows users to easily create point-in-time reports that can be collaborated around.

    The code below must be saved by a user with UNSAFECONTENT permissions into the Template: namespace, with an appropriate title. The template can then simply be called on any page. This template requires the dekiapi extension to be installed.

     

    The Code

    /*
    
    This template will take a snapshot the current page, and create a page directly below it using that
    snapshot. The page is titled as "CURRENT_PAGE_NAME - CURRENT_DATE_AND_TIME"
    
    */
    
    // CALL NEEDED EXTENSIONS
    dekiapi();
    
    <div id="newpagediv">
        // SET UP THE PLEASE WAIT DIV, BE SURE TO HIDE IT ON LOAD IN THE CSS
        <div id="waitdiv">
            <img src="http://developer.mindtouch.com/@api/deki/files/4851/=ajax-loader.gif" id="waitgif"/>
    	<br />
    	"Please wait...";
        </div>
        <div id="creatediv">
            // CREATE HIDDEN VARIABLES FOR THE CTOR TO USE
    	<input type="hidden" value=(page.path) id="ppath"/>
    	<input type="hidden" value=(site.uri) id="siteuri"/>
            <input type="hidden" value=(page.title .. ' - ' .. date.now) id="pname" />
    	<input type="button" value="Snapshot this page" ctor="
                var working = false;
    	    when($this.click){
                    // MAKE SURE WE'RE OK TO PROCEED, THEN SHOW THE WAIT DIV, AND
                    // PUBLISH A MESSAGE ON THE NEWPAGE CHANNEL WITH THE VARIABLES DEFINED ABOVE
                    if (working == false){
                        working = true;
    	    	    #creatediv.hide();
    	    	    #waitdiv.show();
    	    	    @newpage({siteuri: #siteuri.val(), pname: #pname.val(), ppath: #ppath.val()});                                
    	        };
                }
    
    	    when(@newpage) {
                    // DEFINE THE PAGE PATH, THEN GET THE PAGE API SO WE CAN CREATE THE PAGE
    	        var page_path = @newpage.ppath + '/' + @newpage.pname; 
                    var site_api = {{site.api}};
                    var page_api = site_api + '/pages/=' + Deki.url.encode(Deki.url.encode(page_path));
    	        MindTouch.Web.Get(page_api, null, function(xhr){
                        // SUCCESSFULLY DID THE WEB.GET... LET'S CHECK TO MAKE SURE THE 
                        // PAGE DOESN'T ALREADY EXIST - THIS IS UNLIKELY GIVEN THE 
                        // UNIQUE TITLE, BUT BETTER SAFE THAN SORRY
    		    if (xhr.status == '200'){                                
    		        alert('That page already exists. Please choose a different name.');
    		        #creatediv.show();
    		        #waitdiv.hide();
    		        #pname.val('Enter ' + {{string.tolower(buttonname)}} + ' name');
                            working = false;
    		    };
    		    if (xhr.status == '404'){   
                            // PAGE DOESN'T EXIST, SO LET'S GET THE API FOR THE CURRENT PAGE
                            var cpagepath = @newpage.ppath;
                            var template_api = site_api + '/pages/=' + Deki.url.encode(Deki.url.encode(cpagepath));
                            // ATTEMPT TO CREATE THE PAGE IN SAVE MODE
                            MindTouch.Deki.ReadPageContents(template_api, { mode: 'view' },
    				    function(xhr) {
    
    				        // lookup body[not(target)]
    				        var contents = $(xhr.xml).find('body:not([target])').text();
    				        // use as content on UpdatePage
    				        var api_params = {
    				            mode: 'edit'
    				        };
    				        MindTouch.Deki.UpdatePageContents(page_api, contents, api_params, 
                                                function(){
                                                    // SUCCESS! LET'S GO TO THAT PAGE NOW
                                                    @loadnewpage({siteuri: @newpage.siteuri, ppath: @newpage.ppath, pname: @newpage.pname}); 
                                                },
                            		    function(){
                                                    // FAIL! LET THE USER KNOW SOMETHING BROKE
    		                                alert('Page create failed. Please try again.');
                                                    working = false; 
    				            });
                                        }
    			    );
                        }   
                    }, 
    	        function(){
                        // THE WEB.GET HAS FAILED. LET THE USER KNOW
    		    alert('Page read failed. Status: ' + xhr.statusText);    
                        working = false;				       
                    });    
    	    };
        
    	    when(@loadnewpage) {
                    // LOAD THE NEW PAGE IN THE CURRENT BROWSER WINDOW
    	        var newpath = (@loadnewpage.siteuri + @loadnewpage.ppath + '/' + @loadnewpage.pname);
    	        window.location = (newpath);
    	    };
            "/> 
        </div>
    </div>
    
    <style type="text/css">"
    
    #waitdiv {
        display: none;
        text-align: center;
    }
    
    #creatediv {
        text-align: center;
    }
    
    "</style>
    Tag page
    Viewing 1 of 1 comments: view all
    This is great stuff!
    Posted 10:37, 9 Jul 2010
    Viewing 1 of 1 comments: view all
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by