1/** 2 * Implements the text modification tools 3 * 4 * @param {string} edid 5 * @constructor 6 */ 7var ToolboxTextTools = function (edid) { 8 9 var textarea = jQuery('#' + edid)[0]; 10 11 12 /** 13 * Sort the current selection 14 * 15 * @param {boolean} reverse sort backwards? 16 * @access public 17 */ 18 function sort(reverse) { 19 var selection = DWgetSelection(textarea); 20 if (!selection.getLength()) { 21 alert(toolbox_lang.notext); 22 return; 23 } 24 25 var text = selection.getText(); 26 text = text.split("\n"); 27 text.sort(alphanumCase); 28 if (reverse) text.reverse(); 29 text = text.join("\n"); 30 31 pasteText(selection, text, {}); 32 } 33 34 /** 35 * indent the current selection by 2 36 * 37 * @param {boolean} reverse unindent instead? 38 * @access public 39 */ 40 function indent(reverse) { 41 var selection = DWgetSelection(textarea); 42 if (!selection.getLength()) { 43 alert(toolbox_lang.notext); 44 return; 45 } 46 47 var text = selection.getText(); 48 text = text.split("\n"); 49 for (var i = 0; i < text.length; i++) { 50 if (reverse) { 51 text[i] = text[i].replace(/^ ?/, ''); 52 } else { 53 text[i] = ' ' + text[i]; 54 } 55 } 56 text = text.join("\n"); 57 58 pasteText(selection, text, {}); 59 } 60 61 /** 62 * alphnumeric comparator 63 * 64 * @see http://www.davekoelle.com/files/alphanum.js 65 * @see http://stackoverflow.com/a/2802489/172068 66 * @access private 67 */ 68 function alphanumCase(a, b) { 69 function chunkify(t) { 70 var tz = []; 71 var x = 0, y = -1, n = 0, i, j; 72 73 while (i = (j = t.charAt(x++)).charCodeAt(0)) { 74 var m = (i == 46 || (i >= 48 && i <= 57)); 75 if (m !== n) { 76 tz[++y] = ""; 77 n = m; 78 } 79 tz[y] += j; 80 } 81 return tz; 82 } 83 84 var aa = chunkify(a.toLowerCase()); 85 var bb = chunkify(b.toLowerCase()); 86 87 for (x = 0; aa[x] && bb[x]; x++) { 88 if (aa[x] !== bb[x]) { 89 var c = Number(aa[x]), d = Number(bb[x]); 90 if (c == aa[x] && d == bb[x]) { 91 return c - d; 92 } else return (aa[x] > bb[x]) ? 1 : -1; 93 } 94 } 95 return aa.length - bb.length; 96 } 97 98 99 // export public methods 100 return { 101 sort: sort, 102 indent: indent 103 } 104}; 105