topical media & game development

talk show tell print

professional-web-01-v1-editInPlace.js / js



  
  
  function initEditInPlace() {
  
            YAHOO.buzzWatch.editInPlace = new YAHOO.editInPlace();           
  }
  
  YAHOO.editInPlace = function() {
  
          this.tooltipText = "Click to edit and then click out of the box to save." ;
          this.editableElements = YAHOO.util.Dom.getElementsByClassName('editableInPlace');
          this.tooltips = new Array();
          for (i in this.editableElements) {
                  var element=this.editableElements[i];
                  var id=element.id;
                  this.tooltips.push(
                          new YAHOO.widget.Tooltip(id+"ToolTip", 
                          {  
                              context: element,
                              width: "200px",
                              height: "30px",  
                              text: this.tooltipText, 
                              showDelay:500 
                      } )
              );
              //YAHOO.util.Event.addListener(this.editableElements, "click", this.edit, this);
              YAHOO.util.Event.addListener(document, "click", this.checkEdits, this);
              
          }
  }
  
  YAHOO.editInPlace.prototype.set = function(element, value) {
          if (typeof element == "string")
                  element = document.getElementById(element);
          setValue(element, value);
          var textArea = document.getElementById(element.id + 'TextArea');
          if (textArea != undefined) 
                  textArea.value = value;
  }
  
  YAHOO.editInPlace.prototype.get = function(element) {
          if (typeof element == "string")
                  element = document.getElementById(element);
          var textArea = document.getElementById(element.id + 'TextArea');
          if (textArea != undefined) 
                  return textArea.value;
          return element.firstChild.nodeValue;
  }
  
  YAHOO.editInPlace.prototype.edit = function(target) {
          if (target.style['display'] == 'none') 
                  return;
          var textArea;
          var region = YAHOO.util.Dom.getRegion(target);
          target.style['display'] = 'none';
          textArea = document.getElementById(target.id + 'TextArea');
          if (textArea == undefined) {
                  textArea = document.createElement("textarea");
                  textArea.id = target.id + 'TextArea';
                  setValue(textArea, target.firstChild.nodeValue);
                  target.parentNode.insertBefore(textArea, target);
          }
          textArea.cols=10;
          textArea.rows=1;
          textArea.style['display'] = 'block';
          var targetWidth = region.right - region.left;
          var targetHeight = region.botton - region.top;
          var newRegion;
          do {
                  textArea.cols++;
                  newRegion = YAHOO.util.Dom.getRegion(textArea);
          } while (targetWidth >= newRegion.right - newRegion.left)
          do {
                  textArea.rows++;
                  newRegion = YAHOO.util.Dom.getRegion(textArea);
          } while (targetHeight >= newRegion.bottom - newRegion.top)
          textArea.focus();
          textArea.select();
          YAHOO.buzzWatch.OverlayManager.blurAll();
  }        
  
  YAHOO.editInPlace.prototype.checkEdits = function(e, me) {
          var target;
          if (e.srcElement != undefined)
                  target=e.srcElement;
          else
                  target=e.target;
          for (i in me.editableElements) {
                  var element=me.editableElements[i];
                  if (element.style['display'] == 'none' && element !== target) {
                          var textArea = document.getElementById(element.id + 'TextArea');
                          if (textArea !== target) {
                                  setValue(element, textArea.value);
                                  textArea.style['display'] = 'none';
                                  element.style['display'] = 'block';
                          }
                  }
          }
          if (YAHOO.util.Dom.hasClass(target, 'editableInPlace')){
                  me.edit(target);
          }
  }        
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.