-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Swag and OpenPlans handlebars helpers
- nlToBr for user input values by default
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <br> 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, '<br>')); | ||
} 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 = $('<div/>').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)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters