[gitg] Remove templating engine
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gitg] Remove templating engine
- Date: Tue, 1 Jul 2014 09:03:57 +0000 (UTC)
commit bf247ca6e3a12c053a9c2eef3718e2f5dfa62638
Author: Jesse van den Kieboom <jessevdk gnome org>
Date: Tue Jul 1 11:02:37 2014 +0200
Remove templating engine
libgitg/Makefile.am | 1 +
libgitg/gitg-diff-view-request-icon.vala | 118 +++++++++++++++++++
libgitg/gitg-diff-view.vala | 17 +++
libgitg/resources/diff-view.css | 4 +
libgitg/resources/diff-view.html | 29 +++---
libgitg/resources/diff-view.js | 182 +++++++++++++-----------------
6 files changed, 230 insertions(+), 121 deletions(-)
---
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index 213ec22..53e85c3 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -58,6 +58,7 @@ libgitg_libgitg_1_0_la_VALASOURCES = \
libgitg/gitg-commit-list-view.vala \
libgitg/gitg-diff-view.vala \
libgitg/gitg-diff-view-request.vala \
+ libgitg/gitg-diff-view-request-icon.vala \
libgitg/gitg-diff-view-request-resource.vala \
libgitg/gitg-diff-view-request-patch.vala \
libgitg/gitg-diff-view-request-diff.vala \
diff --git a/libgitg/gitg-diff-view-request-icon.vala b/libgitg/gitg-diff-view-request-icon.vala
new file mode 100644
index 0000000..dd8e803
--- /dev/null
+++ b/libgitg/gitg-diff-view-request-icon.vala
@@ -0,0 +1,118 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2014 - Jesse van den Kieboom
+ *
+ * gitg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace Gitg
+{
+ public errordomain DiffViewRequestIconError
+ {
+ ICON_NOT_FOUND
+ }
+
+ class DiffViewRequestIcon : DiffViewRequest
+ {
+ private File? d_icon;
+ private Gtk.IconTheme d_theme;
+
+ public DiffViewRequestIcon(DiffView? view, WebKit.URISchemeRequest request, Soup.URI uri)
+ {
+ base(view, request, uri);
+
+ if (view == null)
+ {
+ d_theme = Gtk.IconTheme.get_default();
+ }
+ else
+ {
+ d_theme = Gtk.IconTheme.get_for_screen(view.get_screen());
+ }
+
+ d_view = null;
+ d_hasView = false;
+ }
+
+ private File ensure_icon() throws DiffViewRequestIconError
+ {
+ if (d_icon != null)
+ {
+ return d_icon;
+ }
+
+ var name = Soup.URI.decode(d_uri.get_path());
+ name = name[1:name.length];
+
+ var sizes = parameter("size");
+
+ int size = 60;
+
+ if (sizes != null)
+ {
+ size = int.parse(sizes);
+ }
+
+ var info = d_theme.lookup_icon(name, size, 0);
+
+ if (info == null)
+ {
+ throw new DiffViewRequestIconError.ICON_NOT_FOUND("icon not found");
+ }
+
+ var path = info.get_filename();
+
+ if (path != null)
+ {
+ d_icon = File.new_for_path(path);
+ }
+ else
+ {
+ throw new DiffViewRequestIconError.ICON_NOT_FOUND("icon not found");
+ }
+
+ return d_icon;
+ }
+
+ public override InputStream? run_async(Cancellable? cancellable) throws GLib.Error
+ {
+ var f = ensure_icon();
+
+ var stream = f.read(cancellable);
+
+ try
+ {
+ var info = f.query_info(FileAttribute.STANDARD_CONTENT_TYPE +
+ "," +
+ FileAttribute.STANDARD_SIZE,
+ 0,
+ cancellable);
+
+ d_size = info.get_size();
+
+ var ctype = info.get_content_type();
+
+ if (ctype != null)
+ {
+ d_mimetype = ContentType.get_mime_type(ctype);
+ }
+ } catch {}
+
+ return stream;
+ }
+ }
+}
+
+// ex:ts=4 noet
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index 9b06b3d..2000e44 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -200,6 +200,8 @@ namespace Gitg
{
case "resource":
return new DiffViewRequestResource(view, request, uri);
+ case "icon":
+ return new DiffViewRequestIcon(view, request, uri);
case "diff":
return new DiffViewRequestDiff(view, request, uri);
case "patch":
@@ -524,6 +526,21 @@ namespace Gitg
});
m.append(item);
+
+ var dbg = Environment.get_variable("GITG_GTK_DIFF_VIEW_DEBUG") != null;
+
+ if (dbg)
+ {
+ var dev = new Gtk.MenuItem.with_label("Developer tools");
+
+ dev.activate.connect(() => {
+ var inspector = get_inspector();
+ inspector.show();
+ });
+
+ m.append(dev);
+ }
+
m.show_all();
m.attach_to_widget(this, null);
diff --git a/libgitg/resources/diff-view.css b/libgitg/resources/diff-view.css
index c724ad4..b3d0f8f 100644
--- a/libgitg/resources/diff-view.css
+++ b/libgitg/resources/diff-view.css
@@ -255,4 +255,8 @@ span.hunk_stats, span.file_path {
color: black;
}
+#diff_header {
+ display: none;
+}
+
/* vi:ts=2:et */
diff --git a/libgitg/resources/diff-view.html b/libgitg/resources/diff-view.html
index 5a0f1c0..977193b 100644
--- a/libgitg/resources/diff-view.html
+++ b/libgitg/resources/diff-view.html
@@ -7,20 +7,6 @@
</head>
<body>
<div id="templates">
- <!-- Commit template -->
- <div class="commit">
- <div class="diff_actions">
- <button class="format_patch">Get patch</button>
- </div>
- <img class="avatar" src="avatar.png" />
- <p>
- <span class="author"></span><br/>
- <span class="date"></span>
- </p>
- <p class="message"></p>
- <div class="sha1"></div>
- </div>
-
<!-- Diff template -->
<div class="file ${FILE_CLASSES}" data-filename="${FILE_FILENAME}">
<table class="file">
@@ -44,7 +30,20 @@
</div>
</div>
<div id="diff">
- <div id="diff_header"></div>
+ <div id="diff_header">
+ <div class="commit">
+ <div class="diff_actions">
+ <button class="format_patch">Get patch</button>
+ </div>
+ <img class="avatar" data-id="avatar" src="gitg-diff:/icon/avatar-default-symbolic?size=50"/>
+ <p>
+ <span class="author" data-id="author"></span><br/>
+ <span class="date" data-id="date"></span>
+ </p>
+ <p class="message" data-id="message"></p>
+ <div class="sha1" data-id="sha1"></div>
+ </div>
+ </div>
<div id="diff_content"></div>
</div>
</body>
diff --git a/libgitg/resources/diff-view.js b/libgitg/resources/diff-view.js
index 7f8f3c5..27aabca 100644
--- a/libgitg/resources/diff-view.js
+++ b/libgitg/resources/diff-view.js
@@ -28,96 +28,101 @@ var settings = {
},
};
+var avatar_cache = {};
+
if ('settings' in params)
{
$.extend(settings, JSON.parse(params.settings));
}
-var templates = {};
+var escapeDiv = document.createElement('div');
+var escapeElement = document.createTextNode('');
+escapeDiv.appendChild(escapeElement);
+
+function html_escape(str)
+{
+ escapeElement.data = str;
+ return escapeDiv.innerHTML;
+}
+
+var commit_elements = null;
+
+function get_commit_elements(content)
+{
+ if (commit_elements != null)
+ {
+ return commit_elements;
+ }
+
+ var elems = content.find('[data-id]');
+
+ commit_elements = {};
-function create_template(name, bindmap)
+ for (var i = 0; i < elems.length; i++)
+ {
+ var elem = $(elems[i]);
+ var name = elem.attr('data-id');
+
+ commit_elements[name] = elem;
+ }
+
+ return commit_elements;
+}
+
+function write_avatar(avatar, commit)
{
- templ = $('#templates').children('.' + name);
+ var h = commit.author.email_md5;
- if (templ.length != 1)
+ if (h in avatar_cache)
{
+ var avc = avatar_cache[h];
+
+ if (avc != null && avatar.attr('src') != avc)
+ {
+ avatar.attr('src', avc);
+ }
+
return;
}
- templ = $(templ[0]);
+ var loader = $('<img/>');
+ var gravatar = 'http://www.gravatar.com/avatar/' + h + '?d=404&s=50';
- props = [];
+ loader.on('load', function () {
+ avatar_cache[h] = gravatar;
- $.each(bindmap, function (key, callback) {
- props.push({
- selector: key,
- callback: callback
- });
+ avatar.attr('src', gravatar);
});
- templates[name] = {
- template: templ,
- props: props,
- execute: function (context) {
- var ret = this.template.clone();
-
- $.each(this.props, function (i, val) {
- ret.find(val.selector).each(function (i, e) {
- var ee = $(e);
-
- retval = val.callback.call(context, ee);
-
- if (typeof(retval) == 'undefined')
- {
- return;
- }
-
- if (retval.nodeType || retval.jquery)
- {
- ee.replace(retval);
- }
-
- if (typeof(retval) == 'string')
- {
- ee.text(retval);
- }
- else if ('text' in retval)
- {
- ee.text(retval.text);
- }
- else if ('html' in retval)
- {
- ee.html(retval.html);
- }
- });
- });
+ loader.on('error', function () {
+ var avc = 'gitg-diff:/icon/avatar-default-symbolic?size=50';
- return ret;
- }
- };
+ avatar_cache[h] = avc;
+ avatar.attr('src', avc);
+ });
- return templates[name];
+ loader.attr('src', gravatar);
}
-function run_template(name, context)
+function write_commit(content, commit)
{
- return templates[name].execute(context);
-}
+ var elems = get_commit_elements(content);
-var escapeDiv = document.createElement('div');
-var escapeElement = document.createTextNode('');
-escapeDiv.appendChild(escapeElement);
+ // Author
+ var name = $('<span/>', {'class': 'author name'}).text(commit.author.name);
+ var a = $('<a/>', {href: 'mailto:' + commit.author.email}).text(commit.author.email);
+ elems.author.html($('<span/>').append(name).append(' <').append(a).append('>'));
-function html_escape(str)
-{
- escapeElement.data = str;
- return escapeDiv.innerHTML;
-}
+ // Date
+ elems.date.text(commit.author.time);
+ // Message
+ elems.message.text(commit.message);
-function write_commit(commit)
-{
- return run_template('commit', commit);
+ // Sha1
+ elems.sha1.text(commit.id);
+
+ write_avatar(elems.avatar, commit);
}
var html_builder_worker = 0;
@@ -431,56 +436,21 @@ function update_diff(id, lsettings)
if ('commit' in j)
{
- $('#diff_header').html(write_commit(j.commit));
+ write_commit($('#diff_header .commit'), j.commit);
+ $('#diff_header').show();
$('.format_patch').click(function() {
xhr_get('patch', {id: j.commit.id});
});
}
+ else
+ {
+ $('#diff_header').hide();
+ }
});
}
addEventListener('DOMContentLoaded', function () {
- create_template("commit", {
- '.author': function () {
- var name = $('<span/>', {'class': 'author name'}).text(this.author.name);
- var a = $('<a/>', {href: 'mailto:' + this.author.email}).text(this.author.email);
-
- return {html: $('<span/>').append(name).append(' <').append(a).append('>')};
- },
- '.date': function () {
- return {text: this.author.time};
- },
- '.subject': function () {
- return this.subject;
- },
- '.message': function () {
- return this.message;
- },
- '.sha1': function () {
- return this.id;
- },
- '.avatar': function (e) {
- var h = this.author.email_md5;
-
- var loader = $('<img/>');
-
- loader.on('error', function () {
- var robosrc = 'http://robohash.org/' + h + '.png?size=50x50';
-
- e.attr('src', robosrc);
- });
-
- var gravatar = 'http://www.gravatar.com/avatar/' + h + '?d=404&s=50';
-
- loader.on('load', function () {
- e.attr('src', gravatar);
- });
-
- loader.attr('src', gravatar);
- },
- });
-
xhr_get('internal', {action: 'loaded'});
}, false);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]