andrew4582

inline-edit.js

Mar 13th, 2011
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.     inlineEdit_init();
  3. });
  4.  
  5. //find all span tags with class inline-edit and id as fieldname parsed to update script. add onclick function
  6. function inlineEdit_init(){
  7.     $("div.inline-edit").each(function() {
  8.        
  9.         $(this).data("in-update",false);
  10.         $(this).click(function() {
  11.            
  12.             inlineEdit($(this),"textarea");
  13.         });
  14.         $(this).css("cursor","pointer");
  15.         $(this).attr("title","Click to edit!");
  16.     });
  17.  
  18.     $("span.inline-edit").each(function() {
  19.        
  20.         $(this).data("in-update",false);
  21.         $(this).click(function() {
  22.            
  23.             inlineEdit($(this),"input");
  24.         });
  25.         $(this).css("cursor","pointer");
  26.         $(this).attr("title","Click to edit!");
  27.     });    
  28. }
  29.  
  30. //edit field created
  31. function inlineEdit(actual,eletype) {
  32.    
  33.     var inupdate = $(actual).data("in-update");
  34.    
  35.     if(inupdate)
  36.         return;
  37.    
  38.     $(actual).data("in-update",true);
  39.  
  40.     try {
  41.  
  42.         var width = $(actual).outerWidth() + 5;
  43.         var height = $(actual).outerHeight() + 2;
  44.    
  45.         if(width < 60) width = 100;
  46.  
  47.         if(eletype == "textarea"){
  48.        
  49.             if(height < 50) height = 50;
  50.    
  51.             var html = "<textarea name=\"textarea\" id=\""+ $(actual).attr("id") +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onblur=\"return inlineEdit_Blur(this,'" + $(actual).attr("id") + "');\">" + $(actual).html() + "</textarea>";
  52.        
  53.             $(actual).html(html);
  54.             $(actual).find("textarea").focus()
  55.             $(actual).find("textarea").select();
  56.         }
  57.         else if(eletype == "input"){
  58.             if(width < 100)
  59.                 width = 150;
  60.  
  61.             var inputHtml = $(actual).html();
  62.             var html = "<input id=\""+ $(actual).attr("id") +"_field\" style=\"width: " + width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + inputHtml + "\" onblur=\"return inlineEdit_Blur(this,'" + $(actual).attr("id")+ "');\" />";
  63.        
  64.             $(actual).html(html);
  65.             $(actual).find("input").select();    
  66.         }
  67.        
  68.     } catch(e){
  69.          $(actual).data("in-update",false);
  70.     }
  71. }
  72. function inlineEdit_Blur(editele,idfld) {
  73.        
  74.     var actual = document.getElementById(idfld);
  75.     try {
  76.         if (editele.value == "") {
  77.             editele.value="[None]";
  78.         }
  79.         $(actual).html(editele.value);
  80.         return editele.value;
  81.     }
  82.     finally{
  83.         $(actual).data("in-update",false);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment