Hi,

Whenever Sam does get back to us and some patches start being applied,
it seems that many people here have ideas for what could be changed
and we can have tickets and patches waiting.

I think the insertion stuff should be changed and here is my
suggestion. I'm interested to see what other people have for this
awkaward spot in Prototype.js.

Currently Prototype.js needs to construct an object for each insertion
and insertion occurs during this construction. An unusual time to make
an operation outside the object.

new Insertion.Before(element, content);

With the code below to actually make an insertion no object
construction is needed.

Element.insertBefore.insert(element, content);

- Peter




Element.Inserter = function(adjacency) {
 this.adjacency = adjacency;
};

Element.Inserter.prototype.insert = function(element, content) {
 this.element = (typeof element === "string") ?
                document.getElementById(element) : element;
 this.content = Element.stripScripts(content);

 if (this.element.insertAdjacentHTML) {
   try {
     this.element.insertAdjacentHTML(this.adjacency, this.content);
   } catch (e) {
     var tagName = this.element.tagName.toLowerCase();
     if (tagName === 'tbody' || tagName === 'tr') {
       this.insertContent(this.contentFromAnonymousTable());
     } else {
       throw e;
     }
   }
 } else {
   this.range = this.element.ownerDocument.createRange();
   this.initRange();
   this.insertContent([this.range.createContextualFragment(this.content)]);
 }

 Element.insertScripts(this.element);
};

Element.Inserter.prototype.contentFromAnonymousTable = function() {
 var div = document.createElement('div');
 div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>';
 return div.childNodes[0].childNodes[0].childNodes;
};

Element.insertBefore = new Element.Inserter('beforeBegin');

Element.insertBefore.initRange = function() {
 this.range.setStartBefore(this.element);
};

Element.insertBefore.insertContent = function(fragments) {
 for(var i=0;i<fragments.length;i++){
   this.element.parentNode.insertBefore(fragments[i], this.element);
 }
};
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to