helpers.js 5.62 KB
'use strict';

const url = require('url');
const _ = require('lodash');
const config = require('../config/common');
const assetUrl = config.assetUrl;

const unitfromat = function(value, params) {
    if (params.rem) {
        value = Number(value) / (params.width || 40) + 'rem';
    } else if (params.percent) {
        value = value + '%';
    } else {
        value = value + 'px';
    }

    return value;
};

module.exports = {
    getAnalysis: function(data, index) {
        if (!data) {
            return '';
        }

        let fp = {
            F_ID: data.id,
            F_INDEX: data.order,
            F_NM: data.param.name,
            I_INDEX: index
        };

        if (data.component[index] && data.component[index].name) {
            fp.I_NM = data.component[index].name;
        }
        return JSON.stringify(fp);
    },
    styleFormat: function(styleObj, opts) {
        let style = '';

        if (styleObj && _.isObject(styleObj)) {
            let params = opts.hash;
            let keys = ['width', 'height', 'top', 'left', 'right', 'bottom'];

            keys.forEach(function(k) {
                if (styleObj[k]) {
                    style += k + ':' + unitfromat(styleObj[k], params) + ';';
                }
            });
        }
        return style;
    },
    tabStyle: function(index, count) {
        let width = (100 / Number(count)).toFixed(2);
        let style = 'height:100%;width:' + width + '%;';

        style += 'left:' + (Number(index) * Number(width)) + '%;';
        return style;
    },
    stringify: function(obj) {
        if (!obj) {
          return '';  
        } 
        return JSON.stringify(obj);
    },
    repeat: function(n, options) {
        if (n) {
            let str = '';

            for (let i = 0; i < n; i++) {
                let opt = {
                    index: i,
                    first: i === 0
                };

                str += options.fn(options, {
                    data: _.merge(opt, options)
                });
            }
            return str;
        }
        return options.inverse(this);
    },
    imgSrc: function(imgSrc) {
        return url.resolve(assetUrl, imgSrc);
    },
    image2: function(imageUrl, opts) {
        if (imageUrl && _.isString(imageUrl)) {
            let params = opts.hash;
            let urls = imageUrl.split('?');
            let query = urls[1] || '';
            let uri = urls[0];

            if (uri.indexOf('http:') === 0) {
                uri = uri.replace('http:', '');
            }

            if (query) {
                query = query.replace(/{width}/g, params.w).replace(/{height}/g, params.h).replace(/{mode}/g, (params.mode || 2));

                if (query.indexOf('imageView2') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                    } else if (params.q) {
                        query += '/q/' + params.q;
                    }
                } else if (query.indexOf('imageMogr2') === 0) {
                    if (params.q && query.indexOf('/quality/') > 0) {
                        query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
                    } else if (params.q) {
                        query += '/quality/' + params.q;
                    }
                } else if (query.indexOf('imageView/') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                    } else if (params.q) {
                        query += '/q/' + params.q;
                    }

                    if (params.mode) {
                        query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
                    }
                }
            } else {
                query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
            }
            return uri + '?' + query;
        } else {
            return '';
        }
    },
    isEqualOr: function() {
        var args = Array.prototype.slice.call(arguments);
        var v1 = args[0];
        var opt = args[args.length - 1];
        var isTrue = false;

        for (var i = 1; i < args.length - 1; i++) {
            if (v1 === args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this); // eslint-disable-line
        } else {
            return opt.inverse(this); // eslint-disable-line    
        }
    },
    ifand: function() {
        var args = Array.prototype.slice.call(arguments);
        var opt = args[args.length - 1];
        var isTrue = true;

        for (var i = 0; i < args.length - 1; i++) {
            if (!args[i]) {
                isTrue = false;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },
    ifor: function() {
        var args = Array.prototype.slice.call(arguments);
        var opt = args[args.length - 1];
        var isTrue = false;

        for (var i = 0; i < args.length - 1; i++) {
            if (args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },

    /**
     * 小于某zhi
     *
     * @param variable
     * @param number
     */
    within: function(variable, number, opt) {
        if (variable < number) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    }
};