[ Index ] |
PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10) |
[Summary view] [Print] [Text view]
1 /* 2 --- 3 4 script: Element.Forms.js 5 6 name: Element.Forms 7 8 description: Extends the Element native object to include methods useful in managing inputs. 9 10 license: MIT-style license 11 12 authors: 13 - Aaron Newton 14 15 requires: 16 - Core/Element 17 - /MooTools.More 18 19 provides: [Element.Forms] 20 21 ... 22 */ 23 24 Element.implement({ 25 26 tidy: function(){ 27 this.set('value', this.get('value').tidy()); 28 }, 29 30 getTextInRange: function(start, end){ 31 return this.get('value').substring(start, end); 32 }, 33 34 getSelectedText: function(){ 35 if (this.setSelectionRange) return this.getTextInRange(this.getSelectionStart(), this.getSelectionEnd()); 36 return document.selection.createRange().text; 37 }, 38 39 getSelectedRange: function() { 40 if ($defined(this.selectionStart)) return {start: this.selectionStart, end: this.selectionEnd}; 41 var pos = {start: 0, end: 0}; 42 var range = this.getDocument().selection.createRange(); 43 if (!range || range.parentElement() != this) return pos; 44 var dup = range.duplicate(); 45 if (this.type == 'text') { 46 pos.start = 0 - dup.moveStart('character', -100000); 47 pos.end = pos.start + range.text.length; 48 } else { 49 var value = this.get('value'); 50 var offset = value.length; 51 dup.moveToElementText(this); 52 dup.setEndPoint('StartToEnd', range); 53 if(dup.text.length) offset -= value.match(/[\n\r]*$/)[0].length; 54 pos.end = offset - dup.text.length; 55 dup.setEndPoint('StartToStart', range); 56 pos.start = offset - dup.text.length; 57 } 58 return pos; 59 }, 60 61 getSelectionStart: function(){ 62 return this.getSelectedRange().start; 63 }, 64 65 getSelectionEnd: function(){ 66 return this.getSelectedRange().end; 67 }, 68 69 setCaretPosition: function(pos){ 70 if (pos == 'end') pos = this.get('value').length; 71 this.selectRange(pos, pos); 72 return this; 73 }, 74 75 getCaretPosition: function(){ 76 return this.getSelectedRange().start; 77 }, 78 79 selectRange: function(start, end){ 80 if (this.setSelectionRange) { 81 this.focus(); 82 this.setSelectionRange(start, end); 83 } else { 84 var value = this.get('value'); 85 var diff = value.substr(start, end - start).replace(/\r/g, '').length; 86 start = value.substr(0, start).replace(/\r/g, '').length; 87 var range = this.createTextRange(); 88 range.collapse(true); 89 range.moveEnd('character', start + diff); 90 range.moveStart('character', start); 91 range.select(); 92 } 93 return this; 94 }, 95 96 insertAtCursor: function(value, select){ 97 var pos = this.getSelectedRange(); 98 var text = this.get('value'); 99 this.set('value', text.substring(0, pos.start) + value + text.substring(pos.end, text.length)); 100 if ($pick(select, true)) this.selectRange(pos.start, pos.start + value.length); 101 else this.setCaretPosition(pos.start + value.length); 102 return this; 103 }, 104 105 insertAroundCursor: function(options, select){ 106 options = $extend({ 107 before: '', 108 defaultMiddle: '', 109 after: '' 110 }, options); 111 var value = this.getSelectedText() || options.defaultMiddle; 112 var pos = this.getSelectedRange(); 113 var text = this.get('value'); 114 if (pos.start == pos.end){ 115 this.set('value', text.substring(0, pos.start) + options.before + value + options.after + text.substring(pos.end, text.length)); 116 this.selectRange(pos.start + options.before.length, pos.end + options.before.length + value.length); 117 } else { 118 var current = text.substring(pos.start, pos.end); 119 this.set('value', text.substring(0, pos.start) + options.before + current + options.after + text.substring(pos.end, text.length)); 120 var selStart = pos.start + options.before.length; 121 if ($pick(select, true)) this.selectRange(selStart, selStart + current.length); 122 else this.setCaretPosition(selStart + text.length); 123 } 124 return this; 125 } 126 127 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Nov 16 22:51:00 2010 | Cross-referenced by PHPXref 0.7 |