MAGICSCIENTIST
11/24/2017 - 8:15 AM

funcMark

javascript code snippet

 //code
 require([`../control/${conDefine.url}.js`], resolve)
 
 //config
 const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const HtmlWebpackPlugin = require('html-webpack-plugin');
const dynamicImport = require('@babel/plugin-syntax-dynamic-import')


module.exports = {
    target: "web",
    devtool: "cheap-module-eval-source-map",
    mode: "development",
    entry: {
        // historyImage: "./src/historyImage/index.js",
        // legislation: "./src/legislation/index.js",
        mapHelper: "./src/mapHelper/index.js",
        mapHelperTest: "./sample/mapHelperTest.js"
    },
    output: {
        path: path.resolve(__dirname, "dist"), // string
        filename: "[name]/[name].js",
        publicPath: '/'
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new MiniCssExtractPlugin({
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "[name]/[name].css",
            chunkFilename: "[id].css"
        }),
        // new HtmlWebpackPlugin({
        //     title: 'Output Management'
        // })
    ],
    module: {
        rules: [
            { test: /\.jpg$/, use: ["file-loader"] },
            { test: /\.(woff|woff2|eot|ttf|otf)$/, use: ['file-loader'] },
            { test: /\.png$/, use: ["url-loader?mimetype=image/png"] },
            {
                test: /\.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            // you can specify a publicPath here
                            // by default it use publicPath in webpackOptions.output
                            publicPath: '../'
                        }
                    },

                    "css-loader"
                ]
                // use: [
                //     'style-loader',
                //     'css-loader'
                // ]
            },
            {
                // test: /\.(html)$/,
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        attrs: [':data-src']
                    }
                }
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        // presets: ['@babel/preset-env'],
                        presets: [
                            ['@babel/preset-env', {

                            }]
                        ],
                        plugins: [
                            [
                                '@babel/transform-runtime', {
                                    "corejs": 2,
                                    "regenerator": true
                                },
                            ],
                            [
                                // dynamicImport
                                "@babel/plugin-syntax-dynamic-import", {

                                }
                            ]

                        ]

                    }
                }
            }

        ]
    },
    devServer: {
        contentBase: './dist'
    },
}
(function(factory){	
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as anonymous module.
		define('viewer', ['jquery'], factory);
	} else if (typeof exports === 'object') {
		// Node / CommonJS
		factory(require('jquery'));
	} else {
		// Browser globals.
		factory(jQuery);
	}
}(function ($) {

	//your code
}))
//get obj array max
Math.max.apply(Math,array.map(function(o){return o.y;}))

//string format
 String.prototype.format = function (args) {
            var result = this;
            if (arguments.length < 1) {
                return result;
            }

            var data = arguments; //如果模板参数是数组
            if (arguments.length == 1 && typeof (args) == "object") {
                //如果模板参数是对象
                data = args;
            }
            for (var key in data) {
                var value = data[key];
                if (undefined != value) {
                    //result = result.replace("{" + key + "}", value);
                    result = result.replaceAll("{" + key + "}", value);
                }
            }
            return result;
        };
        
//replace all
String.prototype.replaceAll = function (search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

//promise
function(){
  return new Promise(resolve=>{
      //do something
      reslove(something);
  })  
}

//array combine
 Array.prototype.push.apply(arr1,arr2); 

    var [head, ...tail] = [1, 2, 3, 4];
    console.log(tail);
    // [2, 3, 4]
    
    var robotA = { name: "Bender" };
    var robotB = { name: "Flexo" };
    var { name: nameA } = robotA;
    var { name: nameB } = robotB;
    console.log(nameA);
    // "Bender"
    console.log(nameB);
    // "Flexo"
    
     var { foo, bar } = { foo: "lorem", bar: "ipsum" };
    console.log(foo);
    // "lorem"
    console.log(bar);
    // "ipsum"
    
    //flatten 数组的数组=>数组
    var a = [[1,2],[4,5],3]
    [].concat.apply(b,a)
    //[].concat.apply([],a)
    
// in array? index:-1
$.inArray( "John", arr )

// array filter 
// jQuery.grep( array, function [, invert ] )
$.grep( [ 0, 1, 2 ], function( n, i ) {
  return n > 0;
});
 swal({
                title: "添加周期",
                text: "一整个植物物候监测周期",
                type: "input",
                showCancelButton: true,
                closeOnConfirm: false,
                //animation: "slide-from-top",
                inputPlaceholder: "周期名"
            },
                function (inputValue) {
                    if (inputValue === false) return false;

                    if (inputValue === "") {
                        swal.showInputError("周期名不能为空!");
                        return false
                    }
                    var addGroup = $.ajax({
                        type: 'post',
                        url: appConfig.sysInfo.sysServiceUrl + 'PhenologyService.asmx/AddWholePeriods',
                        data: JSON.stringify({
                            groupName: inputValue,
                            periods: $self.options.phenology.map(function (x) { return x.id }),
                        }),
                        cache: false,
                        async: true,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                    });
                    addGroup.then(function (res) {
                        res = JSON.parse(res.d);
                        if (res.Flag === -1) {
                            swal("失败", "添加失败" + res.Error, "error");
                            return;
                        }
                        res = res.Result;
                        //refresh combobox
                        $self.LoadPhenology();
                        swal("成功", "添加成功!", "success");
                    });
                });