Helpers.js 4.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define(['require',
    'handlebars',
], function(require, Handlebars, localization) {
    /*
     * General guidelines while writing helpers:
     *
     * - If returning HTML use return new Handlebars.SafeString();
     * - If the helper needs optional arguments use the "hash arguments"
     *   Eg. {{{link . "See more..." story.url class="story"}}}
     *   NOTE: the first argument after the helper name should be . which will be context in the helper function
     *   Handlebars.registerHelper('link', function (context, text, url, options) {
     *    var attrs = [];
     *
     *    for(var prop in options.hash) {
     *      attrs.push(prop + '="' + options.hash[prop] + '"');
     *    }
     *    return new Handlebars.SafeString("<a " + attrs.join(" ") + ">" + text + "</a>");
     *   });
     *
     *
     * NOTE: Due to some limitations in the require-handlebars-plugin, we cannot have helper that takes zero arguments,
     *       for such helpers we have to pass a "." as first argument. [https://github.com/SlexAxton/require-handlebars-plugin/issues/72]
     */

    var HHelpers = {};

    /**
     * Convert new line (\n\r) to <br>
     * from http://phpjs.org/functions/nl2br:480
     */
    HHelpers.nl2br = function(text) {
        text = Handlebars.Utils.escapeExpression(text);
        var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
        return new Handlebars.SafeString(nl2br);
    };
    Handlebars.registerHelper('nl2br', HHelpers.nl2br);

    Handlebars.registerHelper('toHumanDate', function(val) {
        if (!val) return "";
58
        return val; //localization.formatDate(val, 'f');
59 60 61 62 63 64 65 66 67 68 69
    });
    Handlebars.registerHelper('tt', function(str) {
        //return localization.tt(str);
        return str;
    });

    Handlebars.registerHelper('ifCond', function(v1, operator, v2, options) {
        switch (operator) {
            case '==':
                return (v1 == v2) ? options.fn(this) : options.inverse(this);
                break;
70

71 72 73
            case '===':
                return (v1 === v2) ? options.fn(this) : options.inverse(this);
                break;
74 75 76 77 78 79
            case '!=':
                return (v1 !== v2) ? options.fn(this) : options.inverse(this);
                break;
            case '!==':
                return (v1 !== v2) ? options.fn(this) : options.inverse(this);
                break;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            case '<':
                return (v1 < v2) ? options.fn(this) : options.inverse(this);
                break;
            case '<=':
                return (v1 <= v2) ? options.fn(this) : options.inverse(this);
                break;
            case '>':
                return (v1 > v2) ? options.fn(this) : options.inverse(this);
                break;
            case '>=':
                return (v1 >= v2) ? options.fn(this) : options.inverse(this);
                break;
            default:
                return options.inverse(this);
                break;
        }
        //return options.inverse(this);
    });

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    Handlebars.registerHelper('arithmetic', function(val1, operator, val2, options) {
        var v1 = parseInt(val1) || 0,
            v2 = parseInt(val2) || 0;
        switch (operator) {
            case '+':
                return (v1 + v2);
                break;
            case '-':
                return (v1 - v2);
                break;
            case '/':
                return (v1 / v2);
                break;
            case '*':
                return (v1 * v2);
                break;
            case '%':
                return (v1 % v2);
                break;
            default:
                return 0;
                break;
        }
    });

124 125 126 127
    Handlebars.registerHelper('lookup', function(obj, field, defaulValue) {
        return (obj[field] ? obj[field] : (defaulValue ? defaulValue : ""));
    });

128 129 130 131 132 133 134 135 136 137
    Handlebars.registerHelper('eachlookup', function(obj, field, options) {
        return Handlebars.helpers.each((obj[field] ? obj[field] : null), options);
    });

    Handlebars.registerHelper('callmyfunction', function(functionObj, param, options) {
        var argumentObj = _.extend([], arguments);
        argumentObj.shift();
        return functionObj.apply(this, argumentObj);
    });

138
    return HHelpers;
139
});