Search:
Template:WikiForum > Controls > NewPageButton

NewPageButton

    Table of contents
    No headers
    /*
    This template will take prompt the user to type in a title and then create a new page with that title when a button is pressed
    
    Parameters:
        targetpath - the location to create the new page under
        sourcepage - the name of a page to copy the contents to the new page
        buttontxt - the text string to display on the button
    
    To Do:
        Better Error handling for missing page title
        Better Error handling for special characters in page title
        Better Error handling for duplicate page title
        Better Error handling for page title over max length
        Add code to only display if the user has rights to create a new page
    
    Credits:
        RobertM - code to create a new page from http://developer.mindtouch.com/User:robertm/Snapshot_Dynamic_Page
        DocDuck - Input box html from http://forums.developer.mindtouch.com/showthread.php?t=6673
    */
    
     var par = {
        targetpath: ( $0??$targetpath??page.path.."/Forum Topics"),
        sourcepage: ( $1??$sourcepage??"Template:WikiForum/pages/NewForumPost"),
        buttontxt: ( $2??$buttontxt??"Start a New Thread")
     };
    
    // 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=(par.targetpath) id="topicpath"/>
    	<input type="hidden" value=(par.sourcepage) id="ppath"/>
    	<input type="hidden" value=(site.uri) id="siteuri"/>
            <input type="hidden" value=(page.title .. ' - ' .. date.now) id="oldpname" />
            <input type="text" id="pname" name="pname" value="" size="35"/>
    	<input type="button" value=(par.buttontxt) 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(), topicpath: #topicpath.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.topicpath + '/' + @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 cpagepath = @newpage.topicpath;
                            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.topicpath, 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: left;
    }
    
    "</style>
    
    
    

    Files (0)

     

    Comments (0)

    You must login to post a comment.