From a0e2db29b065c64776423a3ae5a8c4c1e24fb582 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Wed, 9 Jul 2014 11:04:00 -0400 Subject: [PATCH] Add Swag and OpenPlans handlebars helpers - nlToBr for user input values by default --- .../jstemplates/place-detail-survey.html | 2 +- src/sa_web/jstemplates/place-detail.html | 2 +- src/sa_web/static/libs/handlebars-helpers.js | 72 +++++++++++++++++++ src/sa_web/static/libs/swag.min.js | 6 ++ src/sa_web/templates/base.html | 3 + 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/sa_web/static/libs/handlebars-helpers.js create mode 100644 src/sa_web/static/libs/swag.min.js diff --git a/src/sa_web/jstemplates/place-detail-survey.html b/src/sa_web/jstemplates/place-detail-survey.html index ef6223d88..b6f696291 100644 --- a/src/sa_web/jstemplates/place-detail-survey.html +++ b/src/sa_web/jstemplates/place-detail-survey.html @@ -31,7 +31,7 @@

{{#items}}
{{ label }} -

{{ value }}

+

{{nlToBr value }}

{{/items}} diff --git a/src/sa_web/jstemplates/place-detail.html b/src/sa_web/jstemplates/place-detail.html index 6ed6aae64..f402cef08 100644 --- a/src/sa_web/jstemplates/place-detail.html +++ b/src/sa_web/jstemplates/place-detail.html @@ -44,7 +44,7 @@

{{#if name }}{{ name }}{{^}}{{>location-string location }}{{/if}}

{{#each_place_item "submitter_name" "name" "location_type"}}
{{ label }} -

{{ value }}

+

{{nlToBr value }}

{{/each_place_item }} diff --git a/src/sa_web/static/libs/handlebars-helpers.js b/src/sa_web/static/libs/handlebars-helpers.js new file mode 100644 index 000000000..67ace6af1 --- /dev/null +++ b/src/sa_web/static/libs/handlebars-helpers.js @@ -0,0 +1,72 @@ +/*globals Handlebars, moment, jQuery */ + +(function($) { + // Get the current url + Handlebars.registerHelper('windowLocation', function(place_id) { + return window.location; + }); + + // Change new lines to
tags. This one is better than Swag. + Handlebars.registerHelper('nlToBr', function(str) { + if (str) { + str = Handlebars.Utils.escapeExpression(str); + return new Handlebars.SafeString(str.replace(/\r?\n|\r/g, '
')); + } else { + return str; + } + }); + + // Date and time ------------------------------------------------------------ + Handlebars.registerHelper('formatDateTime', function(datetime, format) { + if (datetime) { + return moment(datetime).format(format); + } + return ''; + }); + + Handlebars.registerHelper('fromNow', function(datetime) { + if (datetime) { + return moment(datetime).fromNow(); + } + return ''; + }); + + // Iteration ---------------------------------------------------------------- + Handlebars.registerHelper('times', function(n, options) { + var accum = '', i; + for(i = 0; i < n; ++i){ + accum += options.fn(i); + } + return accum; + }); + + Handlebars.registerHelper('range', function(from, to, options) { + var accum = '', i; + for(i = from; i < to; i++){ + accum += options.fn(i); + } + return accum; + }); + + // HTML --------------------------------------------------------------------- + Handlebars.registerHelper('select', function(value, options) { + var $el = $('
').html(options.fn(this)), + selectValue = function(v) { + $el.find('[value="'+v+'"]').attr({ + checked: 'checked', + selected: 'selected' + }); + }; + + if ($.isArray(value)) { + jQuery.each(function(i, v) { + selectValue(v); + }); + } else { + selectValue(value); + } + + return $el.html(); + }); + +}(jQuery)); \ No newline at end of file diff --git a/src/sa_web/static/libs/swag.min.js b/src/sa_web/static/libs/swag.min.js new file mode 100644 index 000000000..81a6376d3 --- /dev/null +++ b/src/sa_web/static/libs/swag.min.js @@ -0,0 +1,6 @@ +/* + Swag v0.6.1 + Copyright 2012 Elving Rodriguez + Available under MIT license +*/ +!function(){var e,r,n,t,a=[].indexOf||function(e){for(var r=0,n=this.length;n>r;r++)if(r in this&&this[r]===e)return r;return-1};"undefined"!=typeof window&&null!==window?window.Swag=n={}:"undefined"!=typeof module&&null!==module&&(module.exports=n={}),n.helpers={},n.addHelper=function(e,r,a){return null==a&&(a=[]),a instanceof Array||(a=[a]),n.helpers[e]=function(){var n,i,u,s,o;for(t.verify(e,arguments,a),i=Array.prototype.slice.apply(arguments),u=[],s=0,o=i.length;o>s;s++)n=i[s],t.isHandlebarsSpecific(n)||(n=t.result(n)),u.push(n);return r.apply(this,u)}},n.registerHelpers=function(e){var r,t,a,i;e?n.Handlebars=e:"undefined"!=typeof window&&null!==window?n.Handlebars=null!=window.Ember?Ember.Handlebars:window.Handlebars:"undefined"!=typeof module&&null!==module&&(n.Handlebars=require("handlebars")),n.registerHelper=function(e,r){return"undefined"!=typeof window&&null!==window&&window.Ember?n.Handlebars.helper(e,r):n.Handlebars.registerHelper(e,r)},a=n.helpers,i=[];for(t in a)r=a[t],i.push(n.registerHelper(t,r));return i},n.Config={partialsPath:"",precompiledTemplates:!0},t={},t.isHandlebarsSpecific=function(e){return e&&null!=e.fn||e&&null!=e.hash},t.isUndefined=function(e){return void 0===e||null===e||t.isHandlebarsSpecific(e)},t.safeString=function(e){return new n.Handlebars.SafeString(e)},t.trim=function(e){var r;return r=/\S/.test(" ")?/^[\s\xA0]+|[\s\xA0]+$/g:/^\s+|\s+$/g,e.toString().replace(r,"")},t.isFunc=function(e){return"function"==typeof e},t.isString=function(e){return"string"==typeof e},t.result=function(e){return t.isFunc(e)?e():e},t.err=function(e){throw new Error(e)},t.verify=function(e,r,n){var a,i,u,s,o,d;for(null==n&&(n=[]),r=Array.prototype.slice.apply(r).slice(0,n.length),d=[],i=s=0,o=r.length;o>s;i=++s)a=r[i],u="{{"+e+"}} requires "+n.length+" arguments "+n.join(", ")+".",n[i].indexOf("safe:")>-1?t.isHandlebarsSpecific(a)?d.push(t.err(u)):d.push(void 0):t.isUndefined(a)?d.push(t.err(u)):d.push(void 0);return d},n.addHelper("lowercase",function(e){return e.toLowerCase()},"string"),n.addHelper("uppercase",function(e){return e.toUpperCase()},"string"),n.addHelper("capitalizeFirst",function(e){return e.charAt(0).toUpperCase()+e.slice(1)},"string"),n.addHelper("capitalizeEach",function(e){return e.replace(/\w\S*/g,function(e){return e.charAt(0).toUpperCase()+e.substr(1)})},"string"),n.addHelper("titleize",function(e){var r,n,t,a;return n=e.replace(/[ \-_]+/g," "),a=n.match(/\w+/g)||[],r=function(e){return e.charAt(0).toUpperCase()+e.slice(1)},function(){var e,n,i;for(i=[],e=0,n=a.length;n>e;e++)t=a[e],i.push(r(t));return i}().join(" ")},"string"),n.addHelper("sentence",function(e){return e.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g,function(e){return e.charAt(0).toUpperCase()+e.substr(1).toLowerCase()})},"string"),n.addHelper("reverse",function(e){return e.split("").reverse().join("")},"string"),n.addHelper("truncate",function(e,r,n){return t.isUndefined(n)&&(n=""),e.length>r?e.substring(0,r-n.length)+n:e},["string","number"]),n.addHelper("center",function(e,r){var n,a;for(r=t.result(r),a="",n=0;r>n;)a+=" ",n++;return""+a+e+a},"string"),n.addHelper("newLineToBr",function(e){return e.replace(/\r?\n|\r/g,"
")},"string"),n.addHelper("sanitize",function(e,r){return t.isUndefined(r)&&(r="-"),e.replace(/[^a-z0-9]/gi,r)},"string"),n.addHelper("first",function(e,r){return t.isUndefined(r)||(r=parseFloat(r)),t.isUndefined(r)?e[0]:e.slice(0,r)},"array"),n.addHelper("withFirst",function(e,r,n){var a,i;if(t.isUndefined(r)||(r=parseFloat(r)),t.isUndefined(r))return n=r,n.fn(e[0]);e=e.slice(0,r),i="";for(a in e)i+=n.fn(e[a]);return i},"array"),n.addHelper("last",function(e,r){return t.isUndefined(r)||(r=parseFloat(r)),t.isUndefined(r)?e[e.length-1]:e.slice(-r)},"array"),n.addHelper("withLast",function(e,r,n){var a,i;if(t.isUndefined(r)||(r=parseFloat(r)),t.isUndefined(r))return n=r,n.fn(e[e.length-1]);e=e.slice(-r),i="";for(a in e)i+=n.fn(e[a]);return i},"array"),n.addHelper("after",function(e,r){return t.isUndefined(r)||(r=parseFloat(r)),e.slice(r)},["array","number"]),n.addHelper("withAfter",function(e,r,n){var a,i;t.isUndefined(r)||(r=parseFloat(r)),e=e.slice(r),i="";for(a in e)i+=n.fn(e[a]);return i},["array","number"]),n.addHelper("before",function(e,r){return t.isUndefined(r)||(r=parseFloat(r)),e.slice(0,-r)},["array","number"]),n.addHelper("withBefore",function(e,r,n){var a,i;t.isUndefined(r)||(r=parseFloat(r)),e=e.slice(0,-r),i="";for(a in e)i+=n.fn(e[a]);return i},["array","number"]),n.addHelper("join",function(e,r){return e.join(t.isUndefined(r)?" ":r)},"array"),n.addHelper("sort",function(e,r){return t.isUndefined(r)?e.sort():e.sort(function(e,n){return e[r]>n[r]})},"array"),n.addHelper("withSort",function(e,r,n){var a,i,u,s;if(i="",t.isUndefined(r))for(n=r,e=e.sort(),u=0,s=e.length;s>u;u++)a=e[u],i+=n.fn(a);else{e=e.sort(function(e,n){return e[r]>n[r]});for(a in e)i+=n.fn(e[a])}return i},"array"),n.addHelper("length",function(e){return e.length},"array"),n.addHelper("lengthEqual",function(e,r,n){return t.isUndefined(r)||(r=parseFloat(r)),e.length===r?n.fn(this):n.inverse(this)},["array","number"]),n.addHelper("empty",function(e,r){return!e||e.length<=0?r.fn(this):r.inverse(this)},"safe:array"),n.addHelper("any",function(e,r){return e&&e.length>0?r.fn(this):r.inverse(this)},"safe:array"),n.addHelper("inArray",function(e,r,n){return a.call(e,r)>=0?n.fn(this):n.inverse(this)},["array","string|number"]),n.addHelper("eachIndex",function(e,r){var n,t,a,i,u;for(t="",n=i=0,u=e.length;u>i;n=++i)a=e[n],t+=r.fn({item:a,index:n});return t},"array"),n.addHelper("eachProperty",function(e,r){var n,t,a;t="";for(n in e)a=e[n],t+=r.fn({key:n,value:a});return t},"object"),n.addHelper("add",function(e,r){return e=parseFloat(e),r=parseFloat(r),e+r},["number","number"]),n.addHelper("subtract",function(e,r){return e=parseFloat(e),r=parseFloat(r),e-r},["number","number"]),n.addHelper("divide",function(e,r){return e=parseFloat(e),r=parseFloat(r),e/r},["number","number"]),n.addHelper("multiply",function(e,r){return e=parseFloat(e),r=parseFloat(r),e*r},["number","number"]),n.addHelper("floor",function(e){return e=parseFloat(e),Math.floor(e)},"number"),n.addHelper("ceil",function(e){return e=parseFloat(e),Math.ceil(e)},"number"),n.addHelper("round",function(e){return e=parseFloat(e),Math.round(e)},"number"),n.addHelper("toFixed",function(e,r){return e=parseFloat(e),r=t.isUndefined(r)?0:r,e.toFixed(r)},"number"),n.addHelper("toPrecision",function(e,r){return e=parseFloat(e),r=t.isUndefined(r)?1:r,e.toPrecision(r)},"number"),n.addHelper("toExponential",function(e,r){return e=parseFloat(e),r=t.isUndefined(r)?0:r,e.toExponential(r)},"number"),n.addHelper("toInt",function(e){return parseInt(e,10)},"number"),n.addHelper("toFloat",function(e){return parseFloat(e)},"number"),n.addHelper("digitGrouping",function(e,r){return e=parseFloat(e),r=t.isUndefined(r)?",":r,e.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g,"$1"+r)},"number"),n.addHelper("is",function(e,r,n){return e&&e===r?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("isnt",function(e,r,n){return e&&e===r?n.inverse(this):n.fn(this)},["safe:string|number","safe:string|number"]),n.addHelper("gt",function(e,r,n){return e>r?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("gte",function(e,r,n){return e>=r?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("lt",function(e,r,n){return r>e?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("lte",function(e,r,n){return r>=e?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("or",function(e,r,n){return e||r?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),n.addHelper("and",function(e,r,n){return e&&r?n.fn(this):n.inverse(this)},["safe:string|number","safe:string|number"]),e={},e.padNumber=function(e,r,n){var t,a;if("undefined"==typeof n&&(n="0"),t=r-String(e).length,a="",t>0)for(;t--;)a+=n;return a+e},e.dayOfYear=function(e){var r;return r=new Date(e.getFullYear(),0,1),Math.ceil((e-r)/864e5)},e.weekOfYear=function(e){var r;return r=new Date(e.getFullYear(),0,1),Math.ceil(((e-r)/864e5+r.getDay()+1)/7)},e.isoWeekOfYear=function(e){var r,n,t,a;return a=new Date(e.valueOf()),n=(e.getDay()+6)%7,a.setDate(a.getDate()-n+3),t=new Date(a.getFullYear(),0,4),r=(a-t)/864e5,1+Math.ceil(r/7)},e.tweleveHour=function(e){return e.getHours()>12?e.getHours()-12:e.getHours()},e.timeZoneOffset=function(r){var n,t;return n=-r.getTimezoneOffset()/60,t=e.padNumber(Math.abs(n),4),(n>0?"+":"-")+t},e.format=function(r,n){return n.replace(e.formats,function(n,t){switch(t){case"a":return e.abbreviatedWeekdays[r.getDay()];case"A":return e.fullWeekdays[r.getDay()];case"b":return e.abbreviatedMonths[r.getMonth()];case"B":return e.fullMonths[r.getMonth()];case"c":return r.toLocaleString();case"C":return Math.round(r.getFullYear()/100);case"d":return e.padNumber(r.getDate(),2);case"D":return e.format(r,"%m/%d/%y");case"e":return e.padNumber(r.getDate(),2," ");case"F":return e.format(r,"%Y-%m-%d");case"h":return e.format(r,"%b");case"H":return e.padNumber(r.getHours(),2);case"I":return e.padNumber(e.tweleveHour(r),2);case"j":return e.padNumber(e.dayOfYear(r),3);case"k":return e.padNumber(r.getHours(),2," ");case"l":return e.padNumber(e.tweleveHour(r),2," ");case"L":return e.padNumber(r.getMilliseconds(),3);case"m":return e.padNumber(r.getMonth()+1,2);case"M":return e.padNumber(r.getMinutes(),2);case"n":return"\n";case"p":return r.getHours()>11?"PM":"AM";case"P":return e.format(r,"%p").toLowerCase();case"r":return e.format(r,"%I:%M:%S %p");case"R":return e.format(r,"%H:%M");case"s":return r.getTime()/1e3;case"S":return e.padNumber(r.getSeconds(),2);case"t":return" ";case"T":return e.format(r,"%H:%M:%S");case"u":return 0===r.getDay()?7:r.getDay();case"U":return e.padNumber(e.weekOfYear(r),2);case"v":return e.format(r,"%e-%b-%Y");case"V":return e.padNumber(e.isoWeekOfYear(r),2);case"W":return e.padNumber(e.weekOfYear(r),2);case"w":return e.padNumber(r.getDay(),2);case"x":return r.toLocaleDateString();case"X":return r.toLocaleTimeString();case"y":return String(r.getFullYear()).substring(2);case"Y":return r.getFullYear();case"z":return e.timeZoneOffset(r);default:return match}})},e.formats=/%(a|A|b|B|c|C|d|D|e|F|h|H|I|j|k|l|L|m|M|n|p|P|r|R|s|S|t|T|u|U|v|V|W|w|x|X|y|Y|z)/g,e.abbreviatedWeekdays=["Sun","Mon","Tue","Wed","Thur","Fri","Sat"],e.fullWeekdays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],e.abbreviatedMonths=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],e.fullMonths=["January","February","March","April","May","June","July","August","September","October","November","December"],n.addHelper("formatDate",function(r,n){return r=new Date(r),e.format(r,n)},["string|number|date","string"]),n.addHelper("now",function(r){var n;return n=new Date,t.isUndefined(r)?n:e.format(n,r)}),n.addHelper("timeago",function(e){var r,n;return e=new Date(e),n=Math.floor((new Date-e)/1e3),r=Math.floor(n/31536e3),r>1?""+r+" years ago":(r=Math.floor(n/2592e3),r>1?""+r+" months ago":(r=Math.floor(n/86400),r>1?""+r+" days ago":(r=Math.floor(n/3600),r>1?""+r+" hours ago":(r=Math.floor(n/60),r>1?""+r+" minutes ago":0===Math.floor(n)?"Just now":Math.floor(n)+" seconds ago"))))},"string|number|date"),n.addHelper("inflect",function(e,r,n,a){var i;return e=parseFloat(e),i=e>1||0===e?n:r,t.isUndefined(a)||a===!1?i:""+e+" "+i},["number","string","string"]),n.addHelper("ordinalize",function(e){var r,n;if(e=parseFloat(e),r=Math.abs(Math.round(e)),n=r%100,a.call([11,12,13],n)>=0)return""+e+"th";switch(r%10){case 1:return""+e+"st";case 2:return""+e+"nd";case 3:return""+e+"rd";default:return""+e+"th"}},"number"),r={},r.parseAttributes=function(e){return Object.keys(e).map(function(r){return""+r+'="'+e[r]+'"'}).join(" ")},n.addHelper("ul",function(e,n){return"
    "+e.map(function(e){return"
  • "+n.fn(t.result(e))+"
  • "}).join("\n")+"
"}),n.addHelper("ol",function(e,n){return"
    "+e.map(function(e){return"
  1. "+n.fn(t.result(e))+"
  2. "}).join("\n")+"
"}),n.addHelper("br",function(e){var r,n;if(r="
",!t.isUndefined(e))for(n=0;n",n++;return t.safeString(r)}),n.addHelper("log",function(e){return console.log(e)},"string|number|boolean|array|object"),n.addHelper("debug",function(e){return console.log("Context: ",this),t.isUndefined(e)||console.log("Value: ",e),console.log("-----------------------------------------------")}),n.addHelper("default",function(e,r){return e||r},"safe:string|number","string|number"),("undefined"==typeof Ember||null===Ember)&&n.addHelper("partial",function(e,r,a){var i;return i=n.Config.partialsPath+e,null==n.Handlebars.partials[e]&&(t.isUndefined(a)?"undefined"!=typeof define&&null!==define&&t.isFunc(define)&&define.amd?(n.Config.precompiledTemplates||(i="!text"+i),require([i],function(r){return t.isString(r)&&(r=n.Handlebars.compile(r)),n.Handlebars.registerPartial(e,r)})):"undefined"!=typeof require&&null!==require?(a=require(i),t.isString(a)&&(a=n.Handlebars.compile(a)),n.Handlebars.registerPartial(e,a)):t.err("{{partial}} no amd or commonjs module support found."):(t.isString(a)&&(a=n.Handlebars.compile(a)),n.Handlebars.registerPartial(e,a))),t.safeString(n.Handlebars.partials[e](r))},"string")}.call(this); \ No newline at end of file diff --git a/src/sa_web/templates/base.html b/src/sa_web/templates/base.html index 538c66b86..41e42328c 100644 --- a/src/sa_web/templates/base.html +++ b/src/sa_web/templates/base.html @@ -160,9 +160,12 @@

+ + + {% endcompress %}