From f19fe8fd7a7ec7405272654995aeb07cfdb70c2e Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Fri, 28 Jun 2013 11:00:46 +0200 Subject: [PATCH] Simplify renderDetails(), eliminating repetition of HTML boilerplate. --- experiments/spclient/mkws.js | 49 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/experiments/spclient/mkws.js b/experiments/spclient/mkws.js index 6bc3a30..c97464d 100644 --- a/experiments/spclient/mkws.js +++ b/experiments/spclient/mkws.js @@ -442,30 +442,39 @@ function renderDetails(data, marker) { var details = '
'; if (marker) details += ''; - if (data["md-title"] != undefined) { - details += ''; - } - if (data["md-date"] != undefined) - details += ''; - if (data["md-author"] != undefined) - details += ''; - if (data["md-electronic-url"] != undefined) - details += ''; - if (data["location"][0]["md-subject"] != undefined) - details += ''; - if (data["location"][0]["@name"] != undefined) - details += ''; + + details += renderField("Title", data["md-title"], data["md-title-remainder"], data["md-title-responsibility"]); + details += renderField("Date", data["md-date"]); + details += renderField("Author", data["md-author"]); + details += renderField("URL", data["md-electronic-url"]); + details += renderField("Subject", data["location"][0]["md-subject"]); + details += renderField("Location", data["location"][0]["@name"], data["location"][0]["@id"]); details += '
'+ marker + '
' + M('Title') + ': '+data["md-title"]; - if (data["md-title-remainder"] !== undefined) { - details += ' : ' + data["md-title-remainder"] + ' '; - } - if (data["md-title-responsibility"] !== undefined) { - details += ' '+ data["md-title-responsibility"] +''; - } - details += '
' + M('Date') + ': ' + data["md-date"] + '
' + M('Author') + ': ' + data["md-author"] + '
URL: ' + data["md-electronic-url"] + '' + '
' + M('Subject') + ': ' + data["location"][0]["md-subject"] + '
' + M('Location') + ': ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '
'; + return details; } +function renderField(caption, data, data2, data3) { + if (data === undefined) { + return ""; + } + + if (caption == "URL") { + data = '' + data + ''; + } + + if (data2 != undefined) { + data = data + " (" + data2 + ")"; + } + + if (data3 != undefined) { + data = data + " " + data3 + ""; + } + + return '' + M(caption) + ': ' + data + ''; +} + + /* * All the HTML stuff to render the search forms and * result pages. -- 1.7.10.4