Nuestro conocimiento compartido. Nuestro tesoro compartido. Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 var MicroEditor = function() {2  3  var that = this;4  5  this._user_style = null;6  this._has_changed = false;7  this._edit_mode = null;8  9  this.dom = document.createElement('iframe');10  this.dom.classList.add('micro-editor');11  this.dom.src = '/pagina_vacia.html';12  this.dom.addEventListener('load', function(event) {13  that._initialize();14  }, true);15  16  17  18  this.head = null;19  this.body = null;20  this.viewer = null;21  this.editor = null;22  23 };24 25 MicroEditor.prototype.setEditMode = function(mode) {26  this.edit_mode = mode;27  if (mode) {28  this.editor.innerHTML = this.viewer.innerHTML;29  this.viewer.style.display = 'none';30  this.editor.style.display = '';31  } else {32  this.viewer.innerHTML = this.editor.innerHTML;33  this.viewer.style.display = '';34  this.editor.style.display = 'none';35  }36 };37 38 MicroEditor.prototype.setUserStyle = function(href) {39  if (null == this._user_style) {40  this._user_style = document.createElement('link');41  this._user_style.setAttribute('rel', 'stylesheet');42  this._user_style.setAttribute('type', 'text/css');43  this.head.appendChild(this._user_style);44  }45  this._user_style.setAttribute('href', href);46 };47 48 MicroEditor.prototype._getCurrentLayer = function() {49  if (edit_mode) {50  return editor;51  } else {52  return viewer;53  }54 };55 56 MicroEditor.prototype.getContent = function() {57  return _getCurrentLayer().innerHTML;58 };59 60 MicroEditor.prototype.setContent = function(content) {61  _getCurrentLayer().innerHTML = content;62 };63 64 MicroEditor.prototype.focus = function() {65  _getCurrentLayer().focus();66 };67 68 MicroEditor.prototype.hasChanged = function() {69  return has_changed;70 };71 72 MicroEditor.prototype._initialize = function() {73  var that = this;74 75  this.head = this.dom.contentDocument.head;76  this.body = this.dom.contentDocument.body;77  this.body.innerHTML = '';78  79  var style = "display:block; position:absolute; top:0; left:0; bottom:0; right:0;";80 81  this.viewer = document.createElement('div');82  this.viewer.setAttribute('style', style);83  this.viewer.classList.add('c12cdfb87c5e2907332a30083d385076');84  this.viewer.innerHTML = 'VISOR';85  this.body.appendChild(this.viewer);86  87  this.editor = document.createElement('div');88  this.editor.setAttribute('style', style);89  this.editor.classList.add('f84a6e47e2acda0ed3d0182f9258ecbe');90  this.editor.innerHTML = 'EDITOR';91  this.editor.setAttribute('contenteditable', true);92  this.editor.addEventListener('keyup', function(event){93  that._has_changed = true;94  }, true);95  this.body.appendChild(this.editor);96  97  this.setEditMode(true);98 };99 100 101 102 window.addEventListener('load', function(event){103  var me = new MicroEditor();104  105  var environment = document.getElementById('environment');106  environment.appendChild(me.dom);107  108 }, true);
Enlace
El enlace para compartir es: