/* Part of SWISH Author: Jan Wielemaker E-mail: J.Wielemaker@cs.vu.nl WWW: http://www.swi-prolog.org Copyright (C): 2014-2018, 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 * Dialog components to interact with the gitty store. * * @version 0.2.0 * @author Jan Wielemaker, J.Wielemaker@vu.nl * @requires jquery */ define([ "jquery", "config", "form", "modal", "backend", "laconic" ], function($, config, form, modal, backend) { (function($) { var pluginName = 'gitty'; /** @lends $.fn.gitty */ var methods = { /** * @param {Object} options * @param {Object.meta} provides the gitty meta-data */ _init: function(options) { return this.each(function() { var elem = $(this); var data = elem.data(pluginName)||{}; var meta = options.meta; var tabs; data.commits = []; data.commits[meta.commit] = meta; data.commit = meta.commit; data.editor = options.editor; function tab(label, active, id, disabled) { var attrs = {role:"presentation"}; var classes = []; if ( active ) classes.push("active"); if ( disabled ) classes.push("disabled"); if ( classes != [] ) attrs.class = classes.join(" "); var elem = $.el.li(attrs, $.el.a({href:"#"+id, 'data-toggle':"tab"}, label)); return elem; } henabled = !Boolean(meta.previous); tabs = $($.el.div({class:"tab-content"})); elem.append($.el.ul( {class:"nav nav-tabs"}, tab("Meta data", true, "gitty-meta-data"), tab("History", false, "gitty-history", henabled), tab("Changes", false, "gitty-diff", henabled))); elem.append(tabs); /* meta-data tab */ tabs.append($.el.div({ class:"tab-pane fade in active gitty-meta-data", id:"gitty-meta-data"})); elem.find('[href="#gitty-meta-data"]').on("show.bs.tab", function(ev) { elem.gitty('showMetaData'); }); /* history tab */ tabs.append($.el.div({ class:"tab-pane fade gitty-history", id:"gitty-history"})); elem.find('[href="#gitty-history"]').on("show.bs.tab", function(ev) { elem.gitty('showHistory'); }); /* diff/changes tab */ tabs.append($.el.div({ class:"tab-pane fade gitty-diff", id:"gitty-diff"})); elem.find('[href="#gitty-diff"]').on("show.bs.tab", function(ev) { elem.gitty('showDiff'); }); elem.data(pluginName, data); elem.gitty('showMetaData'); }); }, /** * @param is the gitty meta-object * @return {DOM} node holding the title */ title: function(meta) { var title = $.el.span("File ", $.el.span({class:"filename"}, meta.name)); if ( meta.symbolic != "HEAD" && meta.commit ) $(title).append("@", $.el.span({class:"sha1 abbrev"}, meta.commit.substring(0,7))); return title; }, /******************************* * META DATA * *******************************/ /** * Show meta data for the current version. If this is the HEAD, * allow updating the meta-data */ showMetaData: function() { return this.each(function() { var elem = $(this); var data = elem.data(pluginName); var tab = elem.find(".gitty-meta-data"); var formel; var meta = data.commits[data.commit]; if ( data.metaData == data.commit ) return; data.metaData = data.commit; tab.html(""); formel = $.el.form({class:"form-horizontal"}, form.fields.fileName(meta.name, meta.public, meta.example, true), // disabled form.fields.title(meta.title), form.fields.author(meta.author), form.fields.date(meta.time, "Date", "date"), form.fields.tags(meta.tags)); if ( meta.symbolic == "HEAD" ) { $(formel).append( form.fields.buttons( { label: "Update meta data", action: function(ev, newMetaData) { data.editor.storage('save', newMetaData, "only-meta-data"); return false; } })); } tab.append(formel); }); }, /******************************* * COMMIT LOG * *******************************/ /** * Fill the commit log tab */ showHistory: function(options) { return this.each(function() { var elem = $(this); var data = elem.data(pluginName); var tab = elem.find(".gitty-history"); var meta = data.commits[data.commit]; var playButton; if ( data.history ) return; options = options||{}; if ( !options.depth ) options.depth = 100; tab.html(""); tab.append($.el.div({class:"gitty-history-table"}, $.el.table( { class:"table table-striped table-condensed gitty-history", 'data-click-to-select':true, 'data-single-select':true }, $.el.tr($.el.th("Comment"), $.el.th("Date"), $.el.th("User"), $.el.th("Changed")), $.el.tbody()))); playButton = form.widgets.glyphIconButton( "play", { title:"Open the highlighted version in SWISH", class:"btn-primary" }); tab.append(playButton); $(playButton).on("click", function(ev) { var row = elem.find("tr.success"); if ( row.length == 1 ) { var commit = row.data('commit'); if ( data.commits[commit].symbolic == "HEAD" ) file = data.commits[commit].name; else file = commit; elem.parents(".swish").swish('playFile', file); $("#ajaxModal").modal('hide'); } return false; }); var url = config.http.locations.web_storage + encodeURI(meta.name); backend.ajax( { url: url, contentType: "application/json", type: "GET", data: { format: "history", depth: options.depth, /* might skip last */ to: data.commit }, success: function(reply) { elem.gitty('fillHistoryTable', reply); data.history = data.commit; }, error: function(jqXHDR) { modal.ajaxError(jqXHR); } }); }); }, /** * Fill the history table */ fillHistoryTable: function(historyobj) { var gitty = this; var data = this.data(pluginName); var table = this.find(".table.gitty-history tbody"); var history = historyobj.history ? historyobj.history : historyobj; for(var i=0; i * * @class gitty * @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.gitty = 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)); /** * Diff meta data * @returns {Object|null}, where object holds `author`, `title` and/or * `tags` */ function diffMeta(m1, m2) { var diff = {}; function diffAttr(a) { if ( (m1[a] || m2[a]) && m1[a] != m2[a] ) diff[a] = {from: m1[a], to: m2[a]}; } diffAttr("author"); diffAttr("title"); diffAttr("data"); diffAttr("public"); diffAttr("example"); diffAttr("name"); if ( (d=diffTags(m1.tags, m2.tags)) ) diff.tags = d; return $.isEmptyObject(diff) ? null : diff; } function reduceMeta(meta, old) { var r = {}; for( var k in meta ) { if ( meta.hasOwnProperty(k) ) { switch(typeof(meta[k])) { case "object": if ( $.isArray(meta[k]) ) { if ( !diffTags(meta[k], old[k]) ) continue; } break; case "string": case "boolean": if ( old[k] == meta[k] ) continue; } r[k] = meta[k]; } } return r; } /** * Diff two tag arrays (arrays of strings) * @returns {Object|null}, where object.added is an array with new * tags and object.deleted contains deleted tags. */ function diffTags(t1, t2) { var d, diff = {}; t1 = t1||[]; t2 = t2||[]; function added(t1, t2) { var a = []; for(var i=0; i 0 ) diff.added = d; if ( (d=added(t2,t1)).length > 0 ) diff.deleted = d; return $.isEmptyObject(diff) ? null : diff; } return { diffMeta: diffMeta, reduceMeta: reduceMeta, diffTags: diffTags }; });