<commit_before>/**
 * ------------------------------------------------------------
 * Component 组件基类
 * @author   sensen(<EMAIL>)
 * ------------------------------------------------------------
 */

'use strict';

var Regular = require('regularjs');
var polyfill = require('./polyfill');
var _ = require('./_');
var filter = require('./filter');
var directive = require('./directive');
var language = require('../language');

/**
 * @class Component
 * @extend Regular
 * @param {boolean}       [options.data.readonly=false]     => 是否只读
 * @param {boolean}       [options.data.disabled=false]     => 是否禁用
 * @param {boolean}       [options.data.visible=true]       => 是否显示
 * @param {string}        [options.data.class]              => 补充class
 */
var Component = Regular.extend({
    /**
     * @protected
     */
    config: function() {
        _.extend(this.data, {
            readonly: false,
            disabled: false,
            visible: true,
            // zh-CN, en-US
            lang: 'zh-CN',
            'class': '',
            console: typeof console === 'undefined' ? undefined : console
        });
        this.$on('update_nek', function(conf) {
            if (!conf) return;
            var typeTrans = function(value, type, extraType) {
                // type: 'none', 'string', 'number', 'boolean', 'array', 'select', 'expression'
                // extraType: string/number/boolean
                if (type === 'none' || type === 'boolean') {
                    return value === 'true';
                }
                if (type === 'number'
                  || (type === 'select' && extraType === 'number')) {
                    return value / 1;
                }
                if (type === 'array') {
                    return JSON.parse(value || '[]').map(function(d) {
                        if (extraType === 'number') {
                            return d / 1;
                        }
                        if (extraType === 'boolean') {
                            return d === 'true';
                        }
                        return d;
                    });
                }
                // 表达式类型只是为了 NEK CLI 预填数据，不做设置
                if (type === 'expression') {
                    return 'NEK_EXP';
                }
                return value;
            };
            conf.forEach(function(d) {
                var val = typeTrans(d.value, d.type, d.extraType);
                if (this.data.hasOwnProperty(d.key) && val !== 'NEK_EXP') {
                    this.$update(d.key, val);
                }
            }.bind(this));
        });
        this.supr();
    },
    /**
     * @protected
     */
    defaults: function(data) {
      this.data = Object.assign(data, this.data);
    },
    /**
     * @protected
     */
    rules: function(attris) {
      this.data = Object.assign(attris, this.data);
    },
    /**
     * @protected
     */
    reset: function() {
        this.data = {};
        this.config();
    },
    $trans: function(key) {
        return language[this.data.lang || 'zh-CN'][key] || '';
    },
})
.filter(filter)
.directive(directive);

module.exports = Component;<commit_msg>feat: get lang from $NEKUI<commit_after>/**
 * ------------------------------------------------------------
 * Component 组件基类
 * @author   sensen(<EMAIL>)
 * ------------------------------------------------------------
 */

'use strict';

var Regular = require('regularjs');
var polyfill = require('./polyfill');
var _ = require('./_');
var filter = require('./filter');
var directive = require('./directive');
var language = require('../language');

/**
 * @class Component
 * @extend Regular
 * @param {boolean}       [options.data.readonly=false]     => 是否只读
 * @param {boolean}       [options.data.disabled=false]     => 是否禁用
 * @param {boolean}       [options.data.visible=true]       => 是否显示
 * @param {string}        [options.data.class]              => 补充class
 */
var Component = Regular.extend({
    /**
     * @protected
     */
    config: function() {
        _.extend(this.data, {
            readonly: false,
            disabled: false,
            visible: true,
            // zh-CN, en-US
            lang: 'zh-CN',
            'class': '',
            console: typeof console === 'undefined' ? undefined : console
        });
        this.$on('update_nek', function(conf) {
            if (!conf) return;
            var typeTrans = function(value, type, extraType) {
                // type: 'none', 'string', 'number', 'boolean', 'array', 'select', 'expression'
                // extraType: string/number/boolean
                if (type === 'none' || type === 'boolean') {
                    return value === 'true';
                }
                if (type === 'number'
                  || (type === 'select' && extraType === 'number')) {
                    return value / 1;
                }
                if (type === 'array') {
                    return JSON.parse(value || '[]').map(function(d) {
                        if (extraType === 'number') {
                            return d / 1;
                        }
                        if (extraType === 'boolean') {
                            return d === 'true';
                        }
                        return d;
                    });
                }
                // 表达式类型只是为了 NEK CLI 预填数据，不做设置
                if (type === 'expression') {
                    return 'NEK_EXP';
                }
                return value;
            };
            conf.forEach(function(d) {
                var val = typeTrans(d.value, d.type, d.extraType);
                if (this.data.hasOwnProperty(d.key) && val !== 'NEK_EXP') {
                    this.$update(d.key, val);
                }
            }.bind(this));
        });
        this.supr();
    },
    /**
     * @protected
     */
    defaults: function(data) {
      this.data = Object.assign(data, this.data);
    },
    /**
     * @protected
     */
    rules: function(attris) {
      this.data = Object.assign(attris, this.data);
    },
    /**
     * @protected
     */
    reset: function() {
        this.data = {};
        this.config();
    },
    $trans: function(key) {
        const $NEKUI = window.$NEKUI || {};
        return language[$NEKUI.lang || this.data.lang || 'zh-CN'][key] || '';
    },
})
.filter(filter)
.directive(directive);

module.exports = Component;
{'fraction_non_alphanumeric': 0.19194902082685733, 'fraction_numerical': 0.0006216972334473111, 'mean_word_length': 1.2429417915650052, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2732713', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1744, 'n_words': 361}