From 2fd73ea4463d6fafa82bffc6d79a9ff06b54ab62 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 26 Jun 2013 17:29:48 -0700 Subject: [PATCH] Added handlebars.helpers (if_eq, etc) --- .../backbone.marionette.templates.js | 1 + UI/JsLibraries/handlebars.helpers.js | 145 ++++++++++++++++++ UI/Settings/Indexers/ItemTemplate.html | 4 +- UI/Settings/Indexers/Model.js | 1 - UI/app.js | 8 + 5 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 UI/JsLibraries/handlebars.helpers.js diff --git a/UI/Handlebars/backbone.marionette.templates.js b/UI/Handlebars/backbone.marionette.templates.js index acf74916c..a566b1336 100644 --- a/UI/Handlebars/backbone.marionette.templates.js +++ b/UI/Handlebars/backbone.marionette.templates.js @@ -2,6 +2,7 @@ define( [ 'templates', + 'handlebars.helpers', 'Handlebars/Helpers/DateTime', 'Handlebars/Helpers/Html', 'Handlebars/Helpers/Numbers', diff --git a/UI/JsLibraries/handlebars.helpers.js b/UI/JsLibraries/handlebars.helpers.js new file mode 100644 index 000000000..56df9b642 --- /dev/null +++ b/UI/JsLibraries/handlebars.helpers.js @@ -0,0 +1,145 @@ +/* Handlebars Helpers - Dan Harper (http://github.com/danharper) */ + +/* This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. */ + +/** + * Following lines make Handlebars helper function to work with all + * three such as Direct web, RequireJS AMD and Node JS. + * This concepts derived from UMD. + * @courtesy - https://github.com/umdjs/umd/blob/master/returnExports.js + */ + +(function (root, factory) { + if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like enviroments that support module.exports, + // like Node. + module.exports = factory(require('handlebars')); + } else if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(['handlebars'], factory); + } else { + // Browser globals (root is window) + root.returnExports = factory(root.Handlebars); + } +}(this, function (Handlebars) { + + /** + * If Equals + * if_eq this compare=that + */ + Handlebars.registerHelper('if_eq', function(context, options) { + if (context == options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Equals + * unless_eq this compare=that + */ + Handlebars.registerHelper('unless_eq', function(context, options) { + if (context == options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Greater Than + * if_gt this compare=that + */ + Handlebars.registerHelper('if_gt', function(context, options) { + if (context > options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Greater Than + * unless_gt this compare=that + */ + Handlebars.registerHelper('unless_gt', function(context, options) { + if (context > options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Less Than + * if_lt this compare=that + */ + Handlebars.registerHelper('if_lt', function(context, options) { + if (context < options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Less Than + * unless_lt this compare=that + */ + Handlebars.registerHelper('unless_lt', function(context, options) { + if (context < options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Greater Than or Equal To + * if_gteq this compare=that + */ + Handlebars.registerHelper('if_gteq', function(context, options) { + if (context >= options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Greater Than or Equal To + * unless_gteq this compare=that + */ + Handlebars.registerHelper('unless_gteq', function(context, options) { + if (context >= options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Less Than or Equal To + * if_lteq this compare=that + */ + Handlebars.registerHelper('if_lteq', function(context, options) { + if (context <= options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Less Than or Equal To + * unless_lteq this compare=that + */ + Handlebars.registerHelper('unless_lteq', function(context, options) { + if (context <= options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + /** + * Convert new line (\n\r) to
+ * from http://phpjs.org/functions/nl2br:480 + */ + Handlebars.registerHelper('nl2br', function(text) { + text = Handlebars.Utils.escapeExpression(text); + var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '
' + '$2'); + return new Handlebars.SafeString(nl2br); + }); + +})); \ No newline at end of file diff --git a/UI/Settings/Indexers/ItemTemplate.html b/UI/Settings/Indexers/ItemTemplate.html index b88224700..bdbf677c0 100644 --- a/UI/Settings/Indexers/ItemTemplate.html +++ b/UI/Settings/Indexers/ItemTemplate.html @@ -1,11 +1,11 @@ 

{{name}}

- {{#if isNewznab}} + {{#if_eq implementation compare="Newznab"}} - {{/if}} + {{/if_eq}}
diff --git a/UI/Settings/Indexers/Model.js b/UI/Settings/Indexers/Model.js index eda24acee..14bd8ddb0 100644 --- a/UI/Settings/Indexers/Model.js +++ b/UI/Settings/Indexers/Model.js @@ -6,6 +6,5 @@ define([ successMessage: 'Indexer Saved', errorMessage : 'Couldn\'t save indexer' - }); }); diff --git a/UI/app.js b/UI/app.js index fec403ed3..f0b0a537c 100644 --- a/UI/app.js +++ b/UI/app.js @@ -7,6 +7,7 @@ require.config({ 'backbone' : 'JsLibraries/backbone', 'sugar' : 'JsLibraries/sugar', 'handlebars' : 'JsLibraries/handlebars.runtime', + 'handlebars.helpers' : 'JsLibraries/handlebars.helpers', 'bootstrap' : 'JsLibraries/bootstrap', 'backbone.mutators' : 'JsLibraries/backbone.mutators', 'backbone.deepmodel' : 'JsLibraries/backbone.deep.model', @@ -159,6 +160,13 @@ require.config({ [ 'backgrid' ] + }, + + 'handlebars.helpers': { + deps: + [ + 'handlebars' + ] } } });