All files / Zapcard_API/docs2/apidoc/utils handlebars_helper.js

0% Statements 0/170
0% Branches 0/68
0% Functions 0/33
0% Lines 0/168
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
define([
    'locales',
    'handlebars',
    'diffMatchPatch'
], function(locale, Handlebars, DiffMatchPatch) {
 
    /**
     * Return a text as markdown.
     * Currently only a little helper to replace apidoc-inline Links (#Group:Name).
     * Should be replaced with a full markdown lib.
     * @param string text
     */
    Handlebars.registerHelper('markdown', function(text) {
        if ( ! text ) {
          return text;
        }
        text = text.replace(/((\[(.*?)\])?\(#)((.+?):(.+?))(\))/mg, function(match, p1, p2, p3, p4, p5, p6) {
          var link = p3 || p5 + '/' + p6;
          return '<a href="#api-' + p5 + '-' + p6 + '">' + link + '</a>';
        });
        return text;
    });
 
    /**
     * start/stop timer for simple performance check.
     */
    var timer;
    Handlebars.registerHelper('startTimer', function(text) {
        timer = new Date();
        return '';
    });
 
    Handlebars.registerHelper('stopTimer', function(text) {
        console.log(new Date() - timer);
        return '';
    });
 
    /**
     * Return localized Text.
     * @param string text
     */
    Handlebars.registerHelper('__', function(text) {
        return locale.__(text);
    });
 
    /**
     * Console log.
     * @param mixed obj
     */
    Handlebars.registerHelper('cl', function(obj) {
        console.log(obj);
        return '';
    });
 
    /**
     * Replace underscore with space.
     * @param string text
     */
    Handlebars.registerHelper('underscoreToSpace', function(text) {
        return text.replace(/(_+)/g, ' ');
    });
 
    /**
     *
     */
    Handlebars.registerHelper('assign', function(name) {
        if(arguments.length > 0) {
            var type = typeof(arguments[1]);
            var arg = null;
            if(type === 'string' || type === 'number' || type === 'boolean') arg = arguments[1];
            Handlebars.registerHelper(name, function() { return arg; });
        }
        return '';
    });
 
    /**
     *
     */
    Handlebars.registerHelper('nl2br', function(text) {
        return _handlebarsNewlineToBreak(text);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('if_eq', function(context, options) {
        var compare = context;
        // Get length if context is an object
        if (context instanceof Object && ! (options.hash.compare instanceof Object))
             compare = Object.keys(context).length;
 
        if (compare === options.hash.compare)
            return options.fn(this);
 
        return options.inverse(this);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('if_gt', function(context, options) {
        var compare = context;
        // Get length if context is an object
        if (context instanceof Object && ! (options.hash.compare instanceof Object))
             compare = Object.keys(context).length;
 
        if(compare > options.hash.compare)
            return options.fn(this);
 
        return options.inverse(this);
    });
 
    /**
     *
     */
    var templateCache = {};
    Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
        if ( ! templateCache[name])
            templateCache[name] = Handlebars.compile($('#template-' + name).html());
 
        var template = templateCache[name];
        var templateContext = $.extend({}, this, sourceContext.hash);
        return new Handlebars.SafeString( template(templateContext) );
    });
 
    /**
     *
     */
    Handlebars.registerHelper('toLowerCase', function(value) {
        return (value && typeof value === 'string') ? value.toLowerCase() : '';
    });
 
    /**
     *
     */
    Handlebars.registerHelper('splitFill', function(value, splitChar, fillChar) {
        var splits = value.split(splitChar);
        return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
    });
 
    /**
     * Convert Newline to HTML-Break (nl2br).
     *
     * @param {String} text
     * @returns {String}
     */
    function _handlebarsNewlineToBreak(text) {
        return ('' + text).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
    }
 
    /**
     *
     */
    Handlebars.registerHelper('each_compare_list_field', function(source, compare, options) {
        var fieldName = options.hash.field;
        var newSource = [];
        if (source) {
            source.forEach(function(entry) {
                var values = entry;
                values['key'] = entry[fieldName];
                newSource.push(values);
            });
        }
 
        var newCompare = [];
        if (compare) {
            compare.forEach(function(entry) {
                var values = entry;
                values['key'] = entry[fieldName];
                newCompare.push(values);
            });
        }
        return _handlebarsEachCompared('key', newSource, newCompare, options);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('each_compare_keys', function(source, compare, options) {
        var newSource = [];
        if (source) {
            var sourceFields = Object.keys(source);
            sourceFields.forEach(function(name) {
                var values = {};
                values['value'] = source[name];
                values['key'] = name;
                newSource.push(values);
            });
        }
 
        var newCompare = [];
        if (compare) {
            var compareFields = Object.keys(compare);
            compareFields.forEach(function(name) {
                var values = {};
                values['value'] = compare[name];
                values['key'] = name;
                newCompare.push(values);
            });
        }
        return _handlebarsEachCompared('key', newSource, newCompare, options);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
        return _handlebarsEachCompared('field', source, compare, options);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
        return _handlebarsEachCompared('title', source, compare, options);
    });
 
    /**
     *
     */
    Handlebars.registerHelper('reformat', function(source, type){
        if (type == 'json')
            try {
               return JSON.stringify(JSON.parse(source.trim()),null, "    ");
            } catch(e) {
 
            }
        return source
    });
 
    /**
     *
     */
    Handlebars.registerHelper('showDiff', function(source, compare, options) {
        var ds = '';
        if(source === compare) {
            ds = source;
        } else {
            if( ! source)
                return compare;
 
            if( ! compare)
                return source;
 
            var d = diffMatchPatch.diff_main(compare, source);
            diffMatchPatch.diff_cleanupSemantic(d);
            ds = diffMatchPatch.diff_prettyHtml(d);
            ds = ds.replace(/&para;/gm, '');
        }
        if(options === 'nl2br')
            ds = _handlebarsNewlineToBreak(ds);
 
        return ds;
    });
 
    /**
     *
     */
    function _handlebarsEachCompared(fieldname, source, compare, options)
    {
        var dataList = [];
        var index = 0;
        if(source) {
            source.forEach(function(sourceEntry) {
                var found = false;
                if (compare) {
                    compare.forEach(function(compareEntry) {
                        if(sourceEntry[fieldname] === compareEntry[fieldname]) {
                            var data = {
                                typeSame: true,
                                source: sourceEntry,
                                compare: compareEntry,
                                index: index
                            };
                            dataList.push(data);
                            found = true;
                            index++;
                        }
                    });
                }
                if ( ! found) {
                    var data = {
                        typeIns: true,
                        source: sourceEntry,
                        index: index
                    };
                    dataList.push(data);
                    index++;
                }
            });
        }
 
        if (compare) {
            compare.forEach(function(compareEntry) {
                var found = false;
                if (source) {
                    source.forEach(function(sourceEntry) {
                        if(sourceEntry[fieldname] === compareEntry[fieldname])
                            found = true;
                    });
                }
                if ( ! found) {
                    var data = {
                        typeDel: true,
                        compare: compareEntry,
                        index: index
                    };
                    dataList.push(data);
                    index++;
                }
            });
        }
 
        var ret = '';
        var length = dataList.length;
        for (var index in dataList) {
            if(index == (length - 1))
                dataList[index]['_last'] = true;
            ret = ret + options.fn(dataList[index]);
        }
        return ret;
    }
 
    var diffMatchPatch = new DiffMatchPatch();
 
    /**
     * Overwrite Colors
     */
    DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
      var html = [];
      var pattern_amp = /&/g;
      var pattern_lt = /</g;
      var pattern_gt = />/g;
      var pattern_para = /\n/g;
      for (var x = 0; x < diffs.length; x++) {
        var op = diffs[x][0];    // Operation (insert, delete, equal)
        var data = diffs[x][1];  // Text of change.
        var text = data.replace(pattern_amp, '&amp;').replace(pattern_lt, '&lt;')
            .replace(pattern_gt, '&gt;').replace(pattern_para, '&para;<br>');
        switch (op) {
          case DIFF_INSERT:
            html[x] = '<ins>' + text + '</ins>';
            break;
          case DIFF_DELETE:
            html[x] = '<del>' + text + '</del>';
            break;
          case DIFF_EQUAL:
            html[x] = '<span>' + text + '</span>';
            break;
        }
      }
      return html.join('');
    };
 
    // Exports
    return Handlebars;
});