Last active
July 12, 2016 13:11
-
-
Save laymain/73d0bba69e06496dc5dc4472ec24073f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Redmine UI Extensions | |
// @namespace http://laymain.com/ | |
// @version 0.3 | |
// @description Redmine UI Extensions | |
// @author Julien Opoix | |
// @match *://redmine.atinternet.tools/*issues/* | |
// @match *://redmine.intraxiti.com/*issues/* | |
// @downloadURL http://laymain.com/data/redmine.ui.extensions.js | |
// @updateURL http://laymain.com/data/redmine.ui.extensions.js | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// jquery-textrange (https://github.com/dwieeb/jquery-textrange) | |
var browserType,textrange={get:function(e){return _textrange[browserType].get.apply(this,[e])},set:function(e,t){var n,i=parseInt(e),r=parseInt(t);return"undefined"==typeof e?i=0:0>e&&(i=this[0].value.length+i),"undefined"!=typeof t&&(n=t>=0?i+r:this[0].value.length+r),_textrange[browserType].set.apply(this,[i,n]),this},setcursor:function(e){return this.textrange("set",e,0)},replace:function(e){return _textrange[browserType].replace.apply(this,[String(e)]),this},insert:function(e){return this.textrange("replace",e)}},_textrange={xul:{get:function(e){var t={position:this[0].selectionStart,start:this[0].selectionStart,end:this[0].selectionEnd,length:this[0].selectionEnd-this[0].selectionStart,text:this.val().substring(this[0].selectionStart,this[0].selectionEnd)};return"undefined"==typeof e?t:t[e]},set:function(e,t){"undefined"==typeof t&&(t=this[0].value.length),this[0].selectionStart=e,this[0].selectionEnd=t},replace:function(e){var t=this[0].selectionStart,n=this[0].selectionEnd,i=this.val();this.val(i.substring(0,t)+e+i.substring(n,i.length)),this[0].selectionStart=t,this[0].selectionEnd=t+e.length}},msie:{get:function(e){var t=document.selection.createRange();if("undefined"==typeof t){var n={position:0,start:0,end:this.val().length,length:this.val().length,text:this.val()};return"undefined"==typeof e?n:n[e]}var i=0,r=0,s=this[0].value.length,a=this[0].value.replace(/\r\n/g,"\n"),o=this[0].createTextRange(),l=this[0].createTextRange();o.moveToBookmark(t.getBookmark()),l.collapse(!1),-1===o.compareEndPoints("StartToEnd",l)?(i=-o.moveStart("character",-s),i+=a.slice(0,i).split("\n").length-1,-1===o.compareEndPoints("EndToEnd",l)?(r=-o.moveEnd("character",-s),r+=a.slice(0,r).split("\n").length-1):r=s):(i=s,r=s);var n={position:i,start:i,end:r,length:s,text:t.text};return"undefined"==typeof e?n:n[e]},set:function(e,t){var n=this[0].createTextRange();if("undefined"!=typeof n){"undefined"==typeof t&&(t=this[0].value.length);var i=e-(this[0].value.slice(0,e).split("\r\n").length-1),r=t-(this[0].value.slice(0,t).split("\r\n").length-1);n.collapse(!0),n.moveEnd("character",r),n.moveStart("character",i),n.select()}},replace:function(e){document.selection.createRange().text=e}}};$.fn.textrange=function(e){return"undefined"==typeof this[0]?this:("undefined"==typeof browserType&&(browserType="selectionStart"in this[0]?"xul":document.selection?"msie":"unknown"),"unknown"===browserType?this:(document.activeElement!==this[0]&&this[0].focus(),"undefined"==typeof e||"string"!=typeof e?textrange.get.apply(this):"function"==typeof textrange[e]?textrange[e].apply(this,Array.prototype.slice.call(arguments,1)):void $.error("Method "+e+" does not exist in jQuery.textrange")))}; | |
var toolbarInjection = function($toolbar) { | |
// JS toolbar - Collapse block | |
$('<button type="button" style="background-image: url(/images/files/xml.png);" title="SQL"><span>SQL</span></button>') | |
.click(function(){ | |
var $textarea = $(this).closest('* :has(.jstEditor)').find('.jstEditor textarea'); | |
$textarea.textrange('replace', '{{collapse(SQL...)\n<pre><code class="sql">' + $textarea.textrange().text + '\n</code></pre>\n}}'); | |
}).appendTo($toolbar); | |
// JS toolbar - Selection to GRID | |
$('<button type="button" style="background-image: url(/images/report.png);" title="Selection to grid"><span>Selection to grid</span></button>') | |
.click(function(){ | |
var $textarea = $(this).closest('* :has(.jstEditor)').find('.jstEditor textarea'); | |
var content = ''; | |
var lines = $textarea.textrange().text.split('\n'); | |
if (lines.length > 0) { | |
for (var ln = 0; ln < lines.length; ln++) { | |
var columns = lines[ln].split('\t'); | |
if (columns.length > 0) { | |
for (var cn = 0; cn < columns.length; cn++) { | |
content += '|' + (ln === 0 ? '_. ' : '') + columns[cn]; | |
} | |
content += '|\n'; | |
} | |
} | |
} | |
$textarea.textrange('replace', content); | |
}).appendTo($toolbar); | |
}; | |
// Injection | |
toolbarInjection($('.jstElements')); | |
$('#history').on('DOMNodeInserted', 'form .jstb_help', function(){ toolbarInjection($(this).closest('.jstElements')); }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment