Subversion Repositories wimsdev

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /**
  2.  * @author Ryan Johnson <ryan@livepipe.net>
  3.  * @copyright 2007 LivePipe LLC
  4.  * @package Control.TextArea
  5.  * @license MIT
  6.  * @url http://livepipe.net/projects/control_textarea/
  7.  * @version 2.0.0.RC1
  8.  */
  9.  
  10. if(typeof(Control) == 'undefined')
  11.         Control = {};
  12. Control.TextArea = Class.create();
  13. Object.extend(Control.TextArea.prototype,{
  14.         onChangeTimeoutLength: 500,
  15.         element: false,
  16.         onChangeTimeout: false,
  17.         initialize: function(textarea){
  18.                 this.element = $(textarea);
  19.                 $(this.element).observe('keyup',this.doOnChange.bindAsEventListener(this));
  20.                 $(this.element).observe('paste',this.doOnChange.bindAsEventListener(this));
  21.                 $(this.element).observe('input',this.doOnChange.bindAsEventListener(this));
  22.         },
  23.         doOnChange: function(event){
  24.                 if(this.onChangeTimeout)
  25.                         window.clearTimeout(this.onChangeTimeout);
  26.                 this.onChangeTimeout = window.setTimeout(function(){
  27.                         if(this.notify)
  28.                                 this.notify('change',this.getValue());
  29.                 }.bind(this),this.onChangeTimeoutLength);
  30.         },
  31.         getValue: function(){
  32.                 return this.element.value;
  33.         },
  34.         getSelection: function(){
  35.                 if(!!document.selection)
  36.                         return document.selection.createRange().text;
  37.                 else if(!!this.element.setSelectionRange)
  38.                         return this.element.value.substring(this.element.selectionStart,this.element.selectionEnd);
  39.                 else
  40.                         return false;
  41.         },
  42.         replaceSelection: function(text){
  43.                 if(!!document.selection){
  44.                         this.element.focus();
  45.                         var old = document.selection.createRange().text;
  46.                         var range = document.selection.createRange();
  47.                         if(old == '')
  48.                                 this.element.innerHTML += text;
  49.                         else{
  50.                                 range.text = text;
  51.                                 range -= old.length - text.length;
  52.                         }
  53.                 }else if(!!this.element.setSelectionRange){
  54.                         var selection_start = this.element.selectionStart;
  55.                         this.element.value = this.element.value.substring(0,selection_start) + text + this.element.value.substring(this.element.selectionEnd);
  56.                         this.element.setSelectionRange(selection_start + text.length,selection_start + text.length);
  57.                 }
  58.                 this.doOnChange();
  59.                 this.element.focus();
  60.         },
  61.         wrapSelection: function(before,after){
  62.                 this.replaceSelection(before + this.getSelection() + after);
  63.         },
  64.         insertBeforeSelection: function(text){
  65.                 this.replaceSelection(text + this.getSelection());
  66.         },
  67.         insertAfterSelection: function(text){
  68.                 this.replaceSelection(this.getSelection() + text);
  69.         },
  70.         injectEachSelectedLine: function(callback,before,after){
  71.                 this.replaceSelection((before || '') + $A(this.getSelection().split("\n")).inject([],callback).join("\n") + (after || ''));
  72.         },
  73.         insertBeforeEachSelectedLine: function(text,before,after){
  74.                 this.injectEachSelectedLine(function(lines,line){
  75.                         lines.push(text + line);
  76.                         return lines;
  77.                 },before,after);
  78.         }
  79. });
  80. if(typeof(Object.Event) != 'undefined')
  81.         Object.Event.extend(Control.TextArea);
  82.  
  83. Control.TextArea.ToolBar = Class.create();
  84. Object.extend(Control.TextArea.ToolBar.prototype,{
  85.         textarea: false,
  86.         container: false,
  87.         initialize: function(textarea,toolbar){
  88.                 this.textarea = textarea;
  89.                 if(toolbar)
  90.                         this.container = $(toolbar);
  91.                 else{
  92.                         this.container = $(document.createElement('ul'));
  93.                         this.textarea.element.parentNode.insertBefore(this.container,this.textarea.element);
  94.                 }
  95.         },
  96.         attachButton: function(node,callback){
  97.                 node.onclick = function(){return false;}
  98.                 $(node).observe('click',callback.bindAsEventListener(this.textarea));
  99.         },
  100.         addButton: function(link_text,callback,attrs){
  101.                 var li = document.createElement('li');
  102.                 var a = document.createElement('a');
  103.                 //a.className = 'obado';
  104.                 a.href = '#';
  105.                 this.attachButton(a,callback);
  106.                 li.appendChild(a);
  107.                 Object.extend(a,attrs || {});
  108.                 if(link_text){
  109.                         var span = document.createElement('span');
  110.                         span.className = 'obado';
  111.                         span.innerHTML = link_text;
  112.                         a.appendChild(span);
  113.                 }
  114.                 this.container.appendChild(li);
  115.         }
  116. });