/* global $ */ // $Id$ // JavaScript glue for jQuery in Tiki // // Tiki 6 - $ is now initialised in jquery.js // but let's keep $jq available too for legacy custom code var legacyLoad = jQuery.fn.load; jQuery.fn.load = function (url, _data, _complete) { var element = this; element.show(); element.tikiModal(tr('Loading...')); if (typeof _data === "function") { _complete = _data; _data = ""; } var complete = function (responseText, textStatus, jqXHR) { element.tikiModal(); if (textStatus === 'error') { element.html('

" + msg + tr("Tip: Leave the first line as it is, starting with \";note:\". This is required") + "
"; $block.prepend($(msg)); $('textarea', this) .val(';note:' + annotation + "\n\n").focus(); $('form', this).submit(function () { $.post($(this).attr('action'), $(this).serialize(), function () { $block.dialog('destroy'); // update the comments list editlink.closest('.comment-container').reload(); }); return false; }); $block.dialog({ modal: true, width: 500, height: 400 }); }); } ) .appendTo(document.body); $(this).mouseup(function( e ) { var range; if( window.getSelection && window.getSelection().rangeCount ) { range = window.getSelection().getRangeAt(0); } else if( window.selection ) { range = window.selection.getRangeAt(0); } if( range ) { var str = $.trim( range.toString() ); if( str.length && -1 === str.indexOf( "\n" ) ) { annote.attr('annotation', str); annote.fadeIn(100).position( { of: e, at: 'bottom left', my: 'top left', offset: '20 20' } ); } else { if (annote.css("display") != "none") { annote.fadeOut(100); } if ($("form.comments").css("display") == "none") { $("form.comments").show(); } if (hiddenParents) { hiddenParents.hide(); hiddenParents = null; } } } }); }; $.fn.browse_tree = function () { this.each(function () { $('.treenode:not(.done)', this) .addClass('done') .each(function () { if (getCookie($('ul:first', this).attr('data-id'), $('ul:first', this).attr('data-prefix')) !== 'o') { $('ul:first', this).css('display', 'none'); } var $placeholder = $('span.ui-icon:first:not(.control)', this); if ($('ul:first', this).length) { var dir = $('ul:first', this).css('display') === 'block' ? 's' : 'e'; if ($placeholder.length) { $placeholder.replaceWith(''); } else { $(this).prepend(''); } } else { if ($placeholder.length) { $placeholder.replaceWith(''); } else { $(this).prepend(''); } } if ($('div.checkbox', this).length) { $('div.checkbox', this).css("margin-left", "16px"); } }); $('.flipper:not(.done)') .addClass('done') .css('cursor', 'pointer') .click(function () { var body = $(this).parent().find('ul:first'); if ('block' === body.css('display')) { $(this).removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e'); body.hide('fast'); setCookie(body.data("id"), "", body.data("prefix")); } else { $(this).removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s'); body.show('fast'); setCookie(body.data("id"), "o", body.data("prefix")); } }); }); return this; }; $.initTrees = function () { $(".tree.root:not(.init)").browse_tree().addClass("init"); }; setTimeout($.initTrees, 100); var fancy_filter_create_token = function(value, label) { var close, token; console.log(); close = $('') .click(function () { var ed = $(this).parent().parent(); $(this).parent().remove(); ed.change(); return false; }); token = $('') .attr('data-value', value) .text(label) .attr('contenteditable', false) .disableSelection() .append(close); return token[0]; }; var fancy_filter_build_init = function(editable, str, options) { if (str === '') { str = ' '; } editable.html(str.replace(/(\d+)/g, '$1')); if (options && options.map) { editable.find('span').each(function () { var val = $(this).text(); $(this).replaceWith(fancy_filter_create_token(val, JSON.parse(options.map)[val] ? JSON.parse(options.map)[val] : val)); }); } }; $.fn.fancy_filter = function (operation, options) { this.each(function () { switch (operation) { case 'init': var editable = $(''), input = this; if (editable[0].contentEditable !== null) { fancy_filter_build_init(editable, $(this).val(), options); editable.attr('contenteditable', true); $(this).after(editable).hide(); } editable .keyup(function() { $(this).change(); $(this).mouseup(); }) .change(function () { $(input).val($('') .html(editable.html()) .find('span').each(function() { $(this).replaceWith(' ' + $(this).attr('data-value') + ' '); }) .end().text().replace(/\s+/g, ' ')); }) .mouseup(function () { input.lastRange = window.getSelection().getRangeAt(0); }); break; case 'add': var editable = $(this).next(); editable.find("span[data-value='"+ options.token +"']").remove(); var node = fancy_filter_create_token(options.token, options.label); editable.append(node); editable.change(); break; } }); return this; }; $.fn.drawGraph = function () { this.each(function () { var $this = $(this); var width = $this.width(); var height = $this.height() ? $this.height() : Math.ceil( width * 9 / 16 ); var nodes = $this.data('graph-nodes'); var edges = $this.data('graph-edges'); var g = new Graph; $.each(nodes, function (k, i) { g.addNode(i); }); $.each(edges, function (k, i) { var style = { directed: true }; if( i.preserve ) { style.color = 'red'; } g.addEdge( i.from, i.to, style ); }); var layouter = new Graph.Layout.Spring(g); layouter.layout(); var renderer = new Graph.Renderer.Raphael($this.attr('id'), g, width, height ); renderer.draw(); }); return this; }; /** * Handle textarea and input text selections * Code from: * * jQuery Autocomplete plugin 1.1 * Copyright (c) 2009 Jörn Zaefferer * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Now deprecated and replaced in Tiki 7 by jquery-ui autocomplete */ $.fn.selection = function(start, end) { if (start !== undefined) { if (end === undefined) { end = start; } return this.each(function() { if( this.selectionStart) { this.selectionStart = start; this.selectionEnd = end; } else if( this.setSelectionRange ){ this.setSelectionRange(start, end); } else if( this.createTextRange ){ var selRange = this.createTextRange(); if (start == end) { selRange.move("character", start); selRange.select(); } else { selRange.collapse(true); selRange.moveStart("character", start); selRange.moveEnd("character", end - start); // moveEnd is relative selRange.select(); } } }); } var field = this[0]; if( field.selectionStart !== undefined) { return { start: field.selectionStart, end: field.selectionEnd } } else if ( field.createTextRange ) { // from http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/ // The current selection var range = document.selection.createRange(); // We'll use this as a 'dummy' var stored_range = range.duplicate(); // Select all text stored_range.moveToElementText( field ); // Now move 'dummy' end point to end point of original range stored_range.setEndPoint( 'EndToEnd', range ); // Now we can calculate start and end points var textProperty = range.htmlText ? "htmlText" : "text"; // behaviour changed in IE10 (approx) so htmlText has unix line-ends which works (not 100% sure why) var selectionStart = stored_range[textProperty].length - range[textProperty].length; var selectionEnd = selectionStart + range[textProperty].length; return { start: selectionStart, end: selectionEnd } } }; $.fn.comment_toggle = function () { this.each(function () { var $target = $(this.hash); $target.hide(); $(this).click(function () { if ($target.is(':visible')) { $target.hide(function () { $(this).empty(); }); } else { $target.comment_load($(this).attr('href')); } return false; }); if (location.search.indexOf("comzone=show") > -1 || location.hash.match(/threadId=?(\d+)/)) { var comButton = this; setTimeout(function() { $(comButton).click(); }, 500); } }); return this; }; $.fn.comment_load = function (url) { var $top = $('#top'); $('.note-list', $top).remove(); this.each(function () { var comment_container = this; if (! comment_container.reload) { comment_container.reload = function () { $(comment_container).empty().comment_load(url); }; } $(this).addClass('comment-container'); $(this).load(url, function (response, status) { $(this).show(); if (jqueryTiki.useInlineComment && ! jqueryTiki.useInlineAnnotations) { $('.comment.inline dt:contains("note")', this) .closest('.comment') .addnotes($top); $top.noteeditor($('.comment-form:last a', comment_container), '#note-editor-comment'); } $('.button.comment-form.autoshow a').addClass('autoshown').click().removeClass('autoshown'); // allow autoshowing of comment forms through autoshow css class var match = location.hash.match(/threadId=?(\d+)/); if (match) { var $tab = $(this).parents(".tab-pane"), $tabContent = $(this).parents(".tab-content"); // if we're in an inactive tab then show the right one for this comment threadId if ($tab.length && ! $tab.is(".active")) { $tabContent.find(".tab-pane").each(function (index) { if (this === $tab[0]) { $(".nav-tabs li:nth(" + index + ") a", $tabContent.parent()).tab("show"); } }) } var $comment = $(".comment[data-comment-thread-id=" + match[1] + "]"); var top = $comment.offset().top; $('html, body').animate({ scrollTop: top },{ duration: 2000, complete: function() { $comment.addClass("comment-highlight"); } }); } }); }); return this; }; $(document).on('click', '.comment-form.buttons > a.btn', function () { var comment_container = $(this).closest('.comment-container, .ui-dialog-content')[0]; $('.comment-form form:not(.commentRatingForm)', comment_container).each(function() { // remove other forms apart from the ratings vote form var $p = $(this).parent(); $p.empty().addClass('button').addClass('buttons').append($p.data('previous')); }); if (!$(this).hasClass('autoshown')) { $(".comment").each(function () { $("article > *:not(ol)", this).each(function () { $(this).css("opacity", 0.6); }); }); } $(this).parents('.comment:first').find("*").css("opacity", 1); var $formContainer = null; if ($(this).data('target')) { $formContainer = $($(this).data('target')); } else { $formContainer = $(this).parents('.buttons'); } $(this).parents('.buttons').data('previous', $(this).siblings().addBack()).empty().removeClass('buttons').removeClass('button'); // Update buttons if loaded as a modal $('.modal.fade.show').trigger('tiki.modal.redraw'); $formContainer.load($(this).attr('href'), function () { var form = $('form', this).submit(function () { var errors, current = this; $(current).tikiModal(tr("Saving...")); //Synchronize textarea and codemirror before comment is posted if (typeof syntaxHighlighter.sync === 'function') { syntaxHighlighter.sync($(current).find("textarea.wikiedit")); } $.post($(current).attr('action'), $(current).serialize(), function (data, st) { $(current).tikiModal(); if (data.threadId) { let threadId = data.threadId; $(current).closest('.comment-container').reload(); $('span.count_comments').each(function () { var action = $(current).attr('action').match(/tiki-comment-(\w*)/), count = parseInt($(this).text()); if (action) { //noinspection FallThroughInSwitchStatementJS switch (action[1]) { case "remove": count--; break; case "post": count++; // fall through to adjust threadId if necessary case "edit": case "moderate": location.hash = location.hash.replace(/threadId=\d+/, "threadId=" + threadId); break; } $(this).text(count); } }); if (data.feedback && data.feedback[0]) { alert(data.feedback.join("\n")); } } else { errors = $('ol.errors', form).empty(); if (!errors.length) { $(':submit', current).after(errors = $('