Skip to content

Instantly share code, notes, and snippets.

@klaftertief
Created March 6, 2011 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaftertief/857392 to your computer and use it in GitHub Desktop.
Save klaftertief/857392 to your computer and use it in GitHub Desktop.
Clickable utilities in the XSLT editor
// Clickable utilities in the XSLT editor
$('#utilities li').click(function(event) {
if ($(event.target).is('a')) return;
var editor = $('textarea.code'),
lines = editor.val().split('\n'),
statement = '<xsl:import href="../utilities/' + $(this).find('a').text() + '"/>',
regexp = '^' + statement.replace('/>', '').replace('../utilities/', '(?:\.\./utilities/)?'),
newLine = '\n',
numberOfNewLines = 1,
i;
if ($(this).hasClass('selected')) {
for (i = 0; i < lines.length; i++) {
if ($.trim(lines[i]).match(regexp) != null) {
(lines[i + 1] === '' && $.trim(lines[i - 1]).substring(0, 11) !== '<xsl:import') ? lines.splice(i, 2) : lines.splice(i, 1);
break;
}
}
editor.val(lines.join(newLine));
$(this).removeClass('selected');
}
else {
for (i = 0; i < lines.length; i++) {
if ($.trim(lines[i]).substring(0, 4) === '<!--' || $.trim(lines[i]).match('^<xsl:(?:import|variable|output|comment|template)')) {
numberOfNewLines = $.trim(lines[i]).substring(0, 11) === '<xsl:import' ? 1 : 2;
if (($('form:first').attr('action').indexOf('blueprints/pages') == -1)) {
// we are inside the page template editor
lines[i] = statement.replace('../utilities/', '') + Array(numberOfNewLines + 1).join(newLine) + lines[i];
}
else {
lines[i] = statement + Array(numberOfNewLines + 1).join(newLine) + lines[i];
}
break;
}
}
editor.val(lines.join(newLine));
$(this).addClass('selected');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment