/* Part of SWISH Author: Jan Wielemaker E-mail: J.Wielemaker@cs.vu.nl WWW: http://www.swi-prolog.org Copyright (C): 2017, VU University Amsterdam CWI Amsterdam All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** * @fileOverview * Provide the chat window. The communication is handled by chat.js * * @version 0.2.0 * @author Jan Wielemaker, J.Wielemaker@vu.nl * @requires jquery */ define([ "jquery", "form", "cm/lib/codemirror", "utils", "config", "modal", "links", "chat", "backend", "laconic" ], function($, form, CodeMirror, utils, config, modal, links, chat, backend) { (function($) { var pluginName = 'chatroom'; var lasthangoutwarning = 0; /** @lends $.fn.chatroom */ var methods = { /** * {Object} [options] * {String} [options.docid] Document identifier */ _init: function(options) { return this.each(function() { var elem = $(this); var data = { messages: [] }; /* private data */ var btn, send; var close; var text; var hangout = "gitty:"+config.swish.hangout; data.docid = options.docid; elem.data(pluginName, data); /* store with element */ elem.addClass("chatroom each-minute swish-event-receiver"); /* build DOM */ btn = $.el.div({class:"btn-group dropup"}, send = $.el.button({ type:"button", class:"btn btn-primary btn-xs" }, "Send"), $.el.button({ type:"button", class:"btn btn-info btn-xs "+ "dropdown-toggle", 'data-toggle':"dropdown", 'aria-haspopup':true, 'aria-expanded':false }, $.el.span({class:"caret"})), ul = $.el.ul({class:"dropdown-menu pull-right"})); text = $.el.textarea({ placeholder:"Type chat message here ..." }), elem.append($.el.div( {class:"chat-conversation"}, $.el.div({class:"chat-outer-wrapper"}, $.el.div({class:"chat-inner-wrapper"}, $.el.div({class:"chat-content-wrapper"}, $.el.div({class:"chat-stretch"}), $.el.div({class:"chat-content"}))))), close = $.el.span({class:"glyphicon menu glyphicon-remove-circle"}), $.el.div({class:"chat-input"}, $.el.table({class:"chat-input"}, $.el.tr($.el.td({class:"chat-text"}, text), $.el.td({class:"chat-send"}, btn))))); $(send).on("click", function() { elem.chatroom('send'); }); /* event handling */ form.widgets.populateMenu($(btn), elem, { "Include my query": function() { var query = $(".prolog-query-editor").queryEditor('getQuery'); if ( query.trim() != "" ) { this.chatroom('send', {payload: [{type:"query", query:query}]}); } else { modal.alert("Your query editor is empty"); } } }); if ( options.docid != hangout ) { form.widgets.populateMenu($(btn), elem, { "Broadcast to hangout": function() { this.chatroom('send', { broadcast: "gitty:"+config.swish.hangout }); } }); } $(close).on("click", function() { elem.tile('close'); }); if ( options.oneline ) { $(text).keypress(function(ev) { if ( ev.which == 13 ) { elem.chatroom('send'); ev.preventDefault(); return false; } }); } else { $(text).on('keyup', function() { var that = $(this); var h; if ( that.scrollTop() != 0 && (h=that.height()) < 500 ) { h += parseFloat(that.css('line-height')); that.animate({ height: h }, 200, function() { elem.chatroom('scrollToBottom'); }); } }); } if ( options.docid == hangout ) { $(text).focus(function() { if ( $(text).val() == "" ) { var now = new Date().getTime(); if ( now-lasthangoutwarning > 300000 ) { lasthangoutwarning = now; modal.help({file:"hangout.html", notagain:"hangout"}); } } }); } elem.on("click", ".chat-message button", function(ev) { var button = $(ev.target).closest("button"); var val; if ( (val = button.data("commit")) ) { elem.closest(".swish").swish('playFile', val); } else if ( (val = button.data("diff")) ) { elem.chatroom('diff', val); } ev.preventDefault(); return false; }); elem.on("click", ".chat-content a", links.followLink); elem.on("pane.resize", function() { elem.chatroom('scrollToBottom', true); }); elem.on("minute", function() { elem.chatroom('update_time'); }); elem.on("activate-tab", function() { elem.chatroom('read_until'); }); $(text).height(parseFloat($(text).css('line-height'))+5); elem.chatroom('load_from_server'); }); }, close: function() { return this.tile('close'); }, /** * Send a chat message. * @param {Object} [options] * @param {Array} [options.payload] Payloads (queries, etc) * @param {String} [options.docid] Addressed document of not self * @param {String} [options.broadcast] Also broadcast the message * to the indicated document id. * @param {Bool} [options.clear] if `false`, do not clear the * message window after sending. */ send: function(options) { options = options||{}; var data = this.data(pluginName); var msg = {type:"chat-message"}; var ta = this.find("textarea"); msg.text = ta.val().trim(); var payload = options.payload||[]; var has_payload = false; var selection = this.chatroom('storage').storage('getSelection'); var hangout = "gitty:" + config.swish.hangout; if ( selection ) payload.push({type:"selection", selection:selection}); for(var i=0; i 0 ) msg = data.messages[data.messages.length-1]; if ( msg ) { $("#chat").chat('read_until', msg.docid, msg.time); $(".chat-bell").chatbell('read_until', msg.docid, msg.time); } }, /** * Render a chat message. * @param {Object} msg * @param {String} msg.html is the HTML content of the object * @param {String} msg.text is the ext of the object * @param {Object} msg.user Sender description */ render: function(msg) { var muser = msg.user||{}; var suser = config.swish.user||{}; if ( msg.is_self === undefined ) msg.is_self = this.chatroom('is_self', msg); elem = $($.el.div({class:"chat-message"+(msg.is_self ? " self" : ""), 'data-userid':muser.wsid})); if ( !msg.is_self && muser.avatar ) { elem.append(chat.avatar(muser)); } elem.append($.el.span({class:"chat-sender"}, msg.is_self ? "Me" : muser.name)); if ( msg.time ) { var title = new Date(msg.time*1000).toLocaleString(); elem.append($.el.span({class:"chat-time", title:title}, "(", utils.ago(msg.time), ") ")); elem.data('time', msg.time); } if ( msg.payload ) { for(var i=0; i 0 || onlydown !== true ) { wrap.find("div.chat-stretch").height(room > 0 ? room : 0); scroll.scrollTop(height); } }); return this; } }; // methods /******************************* * PAYLOAD HANDLERS * *******************************/ var payload_handlers = { selection: function(selection) { var label = $().storage('getSelectionLabel', selection.selection); var btn = $($.el.button({ class:"btn btn-xs btn-primary" }, label + " ", form.widgets.glyphIcon("eye-open"))); btn.on("click", function(ev) { $(ev.target).chatroom('storage') .storage('restoreSelection', selection.selection); }); this.append(" ", btn, " "); }, query: function(query) { var btn = $($.el.button({ class:"btn btn-xs btn-primary" }, "Query ", form.widgets.glyphIcon("download"))); btn.on("click", function() { var qe = $(".prolog-query-editor"); qe.queryEditor('setQuery', query.query); utils.flash(qe.find(".CodeMirror")); }); sourceToolTip(btn, query.query); this.append(" ", btn, " "); }, update: function(update) { var old, dif, nwe; this.append(" ", $.el.span( {class:"update"}, old = btn("play", "btn-primary", "Open old version"), dif = btn("zoom-in", "btn-info", "View changes"), nwe = btn("play", "btn-primary", "Open new version")), " "); $(old).data('commit', update.previous); $(dif).data('diff', {from:update.previous, to:update.commit, name:update.name}); $(nwe).data('commit', update.commit); }, about: function(about) { var file = about.docid.replace("gitty:", ""); this.append(" ", $.el.a({ href:config.http.locations.web_storage+file, class:"store btn btn-primary btn-xs" }, file), " "); } }; // private functions function sourceToolTip(elem, src) { var pre = $.el.pre({class:"cm-s-prolog"}); CodeMirror.runMode(src, "prolog", pre); elem.attr("title", $.el.div(pre).innerHTML); elem.data("html", true); elem.data("placement", "bottom"); elem.data("trigger", "hover"); elem.tooltip(); } function btn(glyph, type, title) { return form.widgets.glyphIconButton(glyph, {class:"btn-xs "+type, title:title}); } /** * Perform very simple regex based markdown processing */ function markdown(text) { var replace = [ { regex: /[a-z][a-zA-Z0-9_]*\/[0-9]/g, func: function(match) { return '' +match+''; } }, { regex: /[a-zA-Z0-9_-]+\.(pl|swinb)\b/g, func: function(match) { return '' +match+''; } }, { regex: /`(.)`/g, func: function(match, content) { return ''+content+''; } }, { regex: /`([\w\[\{\(][^`]*[\w\]\}\)])`/g, func: function(match, content) { return ''+content+''; } }, { delim: "\\*", a: "\\*\\b", z: "\\b\\*", tag: "b" }, { delim: "__", a: "\\b__", z: "__\\b", tag: "b" }, { delim: "_", a: "\\b_", z: "_\\b", tag: "i" } ]; function wrap(tag) { return function(match, content) { return "<"+tag+">"+content+""; }; } for(var i=0; i * * @class chatroom * @tutorial jquery-doc * @memberOf $.fn * @param {String|Object} [method] Either a method name or the jQuery * plugin initialization object. * @param [...] Zero or more arguments passed to the jQuery `method` */ $.fn.chatroom = function(method) { if ( methods[method] ) { return methods[method] .apply(this, Array.prototype.slice.call(arguments, 1)); } else if ( typeof method === 'object' || !method ) { return methods._init.apply(this, arguments); } else { $.error('Method ' + method + ' does not exist on jQuery.' + pluginName); } }; }(jQuery)); });