We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4018
    • 1,131 Posts
    Ok, when you create a new document and change the template, the templateWarning javascript function kicks in and refreshes the page to reflect any new content variables associated with that template.? However, the annoying thing about this is that anything that was already typed in any of the General fields as well as the parent ID and a few other page variables get reset or lost.? So...what to do...

    Perhaps the trick is to add in a loop that will add on any form variables to the URL when the page is refreshed.? That way you could assign $_GET statements to the form variables thus keeping you from loosing your input.? Most of the form fields already have $_GET statements assigned to them already.? Changing the templateWarning function a bit would definitely improve the functionality and elliminate some rather annoying behavior.? I’ll play with the function...but it’s been a while since I’ve jacked with any custom javascript functions...might take me a few.? In the meantime, if anyone has an idea on get this to work properly...pleast post it here!? smiley

    L8R!

    Jeff
      Jeff Whitfield

      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
      • 4018
      • 1,131 Posts
      Take a look at the existing function:

      function templateWarning(){
      	var dropTemplate = document.getElementById('template');
      	if (dropTemplate) for (var i=0; i<dropTemplate.length; i++){
      		if (dropTemplate[i].selected){
      			newTemplate = dropTemplate[i].value;
      		}
      	}
      	if (curTemplate == newTemplate){return;}
      	
      	if (confirm('<?=$_lang['tmplvar_change_template_msg']?>')){
      		var redirectURL = "index.php?newtemplate=" + newTemplate + "&a=<?php echo $action; ?>";
      		if (<?php echo $action; ?> == 27){
      			redirectURL += "&id=<?php echo $id; ?>";
      		}
      		documentDirty=false;
      		location.href=redirectURL;
      	}
      	else{
      		dropTemplate[curTemplateIndex].selected = true;
      	}
      }
      


      What I’ve come up with is a few lines that could be inserted just before the "documentDirty=false;" line. Here’s what I’ve played with that appears to work fine:

      		if (document.mutate.stay) for (var i=0; i<document.mutate.stay.length; i++){
      			if (document.mutate.stay[i].checked && document.mutate.stay[i].value!=''){
      				redirectURL += "&stay=" + document.mutate.stay[i].value;
      			}
      		}
      


      This one is for the "After saving:" (stay) variable at the top. The code above could be duplicated to include others like pagetitle, setitle, description, etc. But what I’m wondering is if this is the right approach. Granted, we’re talking about 11 different form fields here that would need to be covered. I think I’m on the right track...but I would love to find a better way to parse out the form fields and find their values with a loop. Hmm...

        Jeff Whitfield

        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
        • 32963
        • 1,732 Posts

        Thanks for the code. I’ll have a look at it. the one problem though is that GET is too small for sending large amounts of data to a page.

        I’ll give it a try later
          xWisdom
          www.xwisdomhtml.com
          The fear of the Lord is the beginning of wisdom:
          MODx Co-Founder - Create and do more with less.
          • 4018
          • 1,131 Posts
          Well...we don’t have to deal with every single one...just the one’s that really need the emphasis. Here’s the variables that I think are pretty important so far:

          stay
          pagetitle
          setitle
          description
          alias
          ta
          introtext
          menuindex
          parent
          id
          a

          And you’re right...probably not a good idea to pass the "sa" content variable or the like on over. My main thing is just getting the other stuff (the small stuff) to stick when you change the template. Here’s a bit of code I’ve been playing with...might be useful:

          function templateWarning(){
          	var dropTemplate = document.getElementById('template');
          	if (dropTemplate) for (var i=0; i<dropTemplate.length; i++){
          		if (dropTemplate[i].selected){
          			newTemplate = dropTemplate[i].value;
          		}
          	}
          	if (curTemplate == newTemplate){return;}
          	
          	if (confirm('<?=$_lang['tmplvar_change_template_msg']?>')){
          		var redirectURL = "index.php?newtemplate=" + newTemplate + "&a=<?php echo $action; ?>";
          		if (<?php echo $action; ?> == 27){
          			redirectURL += "&id=<?php echo $id; ?>";
          		}
          		var theForm = document.mutate		
          		for(i=0; i<theForm.elements.length; i++){
          			if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
          				if((theForm.elements[i].name != "template") && (theForm.elements[i].value != "" && theForm.elements[i].value != null) && (theForm.elements[i].name != "")){
          					redirectURL += "&" + theForm.elements[i].name + "=" + theForm.elements[i].value;
          				}
          			} 
          		 }
          		if (document.mutate.stay) for (var i=0; i<document.mutate.stay.length; i++){
          			if (document.mutate.stay[i].checked && document.mutate.stay[i].value!=''){
          				redirectURL += "&stay=" + document.mutate.stay[i].value;
          			}
          		}
          		documentDirty=false;
          		location.href=redirectURL;
          	}
          	else{
          		dropTemplate[curTemplateIndex].selected = true;
          	}
          }
          


          This will at least give you an idea on where I’m going with it. The idea is that we test all the form variable for their type, check their names or values aren’t empty, exclude the ones we don’t want (like the "sa" content variable and such), and stick them on the end of the redirectURL. The problem is that the tv variable are going to be a pain in the ass with a loop like this...hmm... We might just have to create separate lines for all the variables we want to pass with the new document. That’s the trick though...this change to the function will only effect new documents...not existing ones. So I figure that only the General tab, hidden fields, and other visible "options" should be targets. I don’t think the Page Settings options are that big of a deal.

          Off to play with the code some more! smiley

          Jeff

          Quote from: xwisdom at Jun 23, 2005, 05:44 PM


          Thanks for the code. I’ll have a look at it. the one problem though is that GET is too small for sending large amounts of data to a page.

          I’ll give it a try later
            Jeff Whitfield

            "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
            • 4018
            • 1,131 Posts
            I think I might have it licked. Give me another day and I’ll post the code. smiley Almost there...
              Jeff Whitfield

              "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
              • 4018
              • 1,131 Posts
              Alrighty...think I got it now. I played around with the function and came up with this:

              function templateWarning(){
              	var dropTemplate = document.getElementById('template');
              	if (dropTemplate) for (var i=0; i<dropTemplate.length; i++){
              		if (dropTemplate[i].selected){
              			newTemplate = dropTemplate[i].value;
              		}
              	}
              	if (curTemplate == newTemplate){return;}
              	
              	if (confirm('<?=$_lang['tmplvar_change_template_msg']?>')){
              		var redirectURL = "index.php?newtemplate=" + newTemplate + "&a=<?php echo $action; ?>";
              		if (<?php echo $action; ?> == 27){
              			redirectURL += "&id=<?php echo $id; ?>";
              		}
              		var arrElements = ["stay","pagetitle","setitle","description","alias","introtext","menuindex","parent"];
              		for (x=0; x<arrElements.length; x++){
              			elementName = document.mutate[arrElements[x]];
              			if(elementName.type == undefined){
              				elementType = elementName[0].type;
              			}
              			else{
              				elementType = elementName.type;
              			}
              			switch(elementType) {
              			case 'text':
              				if(elementName.value != ""){
              					redirectURL += "&" + elementName.name + "=" + elementName.value;
              				}
              				break;
              			case 'textarea':
              				if(elementName.value != ""){
              					redirectURL += "&" + elementName.name + "=" + elementName.value;
              				}
              				break;
              			case 'button':
              				if(elementName.value != ""){
              					redirectURL += "&" + elementName.name + "=" + elementName.value;
              				}
              				break;
              			
              			case 'radio':
              				for (y=0; y<elementName.length; y++){
              					if(elementName[y].checked && elementName[y].value != ""){
              						redirectURL += "&" + elementName[y].name + "=" + elementName[y].value;
              					}
              				}
              				break;
              			case 'checkbox':
              				for (y=0; y<elementName.length; y++){
              					if(elementName[y].checked && elementName[y].value != ""){
              						redirectURL += "&" + elementName[y].name + "=" + elementName[y].value;
              					}
              				}
              				break;
              			}
              		} 
              		documentDirty=false;
              		location.href=redirectURL;
              	}
              	else{
              		dropTemplate[curTemplateIndex].selected = true;
              	}
              }
              


              I decided just to create a simple array of the form elements to be kept. Then, test the element to ensure that the type is not undefined (this happens with checkboxes and radio buttons if you don’t use an index). Next, I used a CASE statement to select the proper type, check for empty values, then add the element onto the redirectURL variable. Pretty simple...though it does add a bit to the code.

              Now all that needs to be done is to verify which form elements we wish to keep when a template is changed and mark up the form elements with GET statements. Something like this should do for most of them:

              <input name="pagetitle" type="text" maxlength="100" value="<?php echo $_GET["pagetitle"] <> "" ? $_GET["pagetitle"]:htmlspecialchars(stripslashes($content['pagetitle']));?>" class="inputBox" style="width:300px;" onChange="documentDirty=true;">
              


              This one is, of course, for the pagetitle. The only tricky one will be the Document Parent variable...it gets reset to 0 when you change the template as well...very annoying! I’ll play with the code for that one too. So far, so good....I think! If anyone knows a better, more efficient way of doing this...let me know. But at least you know where I’m going with this. smiley

              L8R!

              Jeff
                Jeff Whitfield

                "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                • 4018
                • 1,131 Posts
                Ok...made a small change to the templateChange function by adding a "hidden" item to the CASE statement. Plus, I marked up some of the form elements to look for a GET variable and I changed the code that looks up the Parent for the document as well. Instead of posting the code, I’ve attached my finished page. Granted, it’s probably not finished...but it’s a good start, I think. smiley

                L8R!

                Jeff
                  Jeff Whitfield

                  "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                • Gotta love Open Source! Great work! I had just made sure I got in the habit of dealing with the template first...well, I was trying to get into the habit of dealing with the template first; certainly got a firm reminder when I forgot.

                  Seems to me this would be a good place for Ajax. Change gets sent to processing script, any errors get returned, but nothing else changes, page doesn’t refresh.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 4018
                    • 1,131 Posts
                    Quote from: sottwell at Jun 24, 2005, 04:32 PM

                    Gotta love Open Source! Great work! I had just made sure I got in the habit of dealing with the template first...well, I was trying to get into the habit of dealing with the template first; certainly got a firm reminder when I forgot.

                    Seems to me this would be a good place for Ajax. Change gets sent to processing script, any errors get returned, but nothing else changes, page doesn’t refresh.

                    You’re right...this might be a good place for something like Ajax. However, first things first...cause changing the behavior of the page means changing alot of things. I think the goal right now should be to stabalize the feature set and get it where it needs to be. Then, after is all said and done, we can work on really improving and fine tuning it. smiley
                      Jeff Whitfield

                      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                      • 4018
                      • 1,131 Posts
                      xWisdom...if you’ve downloaded the zip already...redownload it! I changed some things, fixed a few little bugs...should be pretty good now. Still probably needs some TLC though. wink

                      Jeff
                        Jeff Whitfield

                        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."