<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="base/resource"/>
    <title>脚本编辑 - Big Whale</title>
    <link href="../libs/bootstrap-select/bootstrap-select.min.css" rel="stylesheet"/>
    <link href="../libs/dropzone/dropzone.min.css" rel="stylesheet">
    <link href="../css/common.css" rel="stylesheet"/>
    <link href="../css/script_edit.css" rel="stylesheet">
</head>
<body ng-app="content-app" ng-controller="content-controller">
<div class="container-fluid animated fadeInDown">
    <div class="bw-nav">
        <div class="row">
            <ol class="breadcrumb">
                <li><a href="javascript:" ng-click="onOpenList()">脚本管理</a></li>
                <li class="active">{{editItem.id ? "编辑" : "新增"}}</li>
            </ol>
            <a class="bw-refresh" onclick="location.reload()" title="刷新">
                <i class="fa fa-refresh" style="font-size: 18px"></i>
            </a>
        </div>
    </div>
    <div class="bw-body">
        <div class="row">
            <div class="col-md-12">
                <form class="form-horizontal" ng-submit="onSaveItem()">
                    <!-- 基本配置 -->
                    <div class="container shadow">
                        <div class="form-group" th:if="${session.user.root}">
                            <label for="select_uid" class="col-sm-2 control-label">用户&nbsp;<B>*</B></label>
                            <div class="col-sm-4">
                                <select id="select_uid" class="form-control selectpicker show-tick" data-live-search="true"
                                        ng-model="editItem.uid" ng-options="item.id as item.username for item in userList" ng-change="onUidChange(editItem.uid)" required>
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_name" class="col-sm-2 control-label">
                                名称<B class="help" data-toggle="tooltip" data-placement="top" title="任务名称（注：当脚本与yarn任务相关的时候，此名称与yarn中的应用名称无关）">?</B>&nbsp;<B>*</B>
                            </label>
                            <div class="col-sm-4">
                                <input id="input_name" class="form-control" ng-model="editItem.name" autocomplete="off" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="textarea_description" class="col-sm-2 control-label">描述</label>
                            <div class="col-sm-4">
                                <textarea id="textarea_description" class="form-control" ng-model="editItem.description" rows="3" maxlength="255"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="select_type" class="col-sm-2 control-label">类型</label>
                            <div class="col-sm-4">
                                <select id="select_type" class="form-control selectpicker show-tick"
                                        ng-model="editItem.type" ng-options="item.value as item.name for item in scriptTypeList" ng-change="onTypeChange(editItem.type)" required>
                                </select>
                            </div>
                            <div ng-if="editItem.type == 1 || editItem.type == 2">
                                <label for="select_spark_version" class="col-sm-2 control-label">版本</label>
                                <div class="col-sm-4">
                                    <select id="select_spark_version" class="form-control selectpicker show-tick"
                                            ng-model="editItem.spark.$command" ng-options="item.command as item.version for item in sparkVersionList" ng-change="onSparkVersionChange(editItem.spark.$command)" required>
                                    </select>
                                </div>
                            </div>
                            <div ng-if="editItem.type == 3 || editItem.type == 4">
                                <label for="select_flink_version" class="col-sm-2 control-label">版本</label>
                                <div class="col-sm-4">
                                    <select id="select_flink_version" class="form-control selectpicker show-tick"
                                            ng-model="editItem.flink.$command" ng-options="item.command as item.version for item in flinkVersionList" ng-change="onSparkVersionChange(editItem.flink.$command)" required>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div ng-if="editItem.type == 0" class="form-group">
                            <label for="select_agentId" class="col-sm-2 control-label">运行代理&nbsp;<B>*</B></label>
                            <div class="col-sm-4">
                                <select id="select_agentId" class="form-control selectpicker show-tick"
                                        ng-model="editItem.agentId" ng-options="item.id as item.name for item in agentList" required>
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div ng-if="editItem.type != 0">
                            <div class="form-group">
                                <label for="select_clusterId" class="col-sm-2 control-label">运行集群&nbsp;<B>*</B></label>
                                <div class="col-sm-4">
                                    <select id="select_clusterId" class="form-control selectpicker show-tick"
                                            ng-model="editItem.clusterId" ng-options="item.id as item.name for item in clusterList" ng-change="onClusterIdChange(editItem.clusterId)" required>
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                                <label for="select_queue" class="col-sm-2 control-label">运行队列&nbsp;<B>*</B></label>
                                <div class="col-sm-4">
                                    <select id="select_queue" class="form-control selectpicker show-tick"
                                            ng-model="editItem.queue_" ng-options="item for item in queueList" ng-change="onQueueChange(editItem.queue_)" required>
                                        <option value="">无队列</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    程序包<B class="help" data-toggle="tooltip" data-placement="top" title="程序包地址（注：编辑或上传完成功后如果URL与脚本代码中的URL不符，请手动更新一下脚本代码中的URL），请勿包含空格">?</B>&nbsp;<B>*</B>
                                </label>
                                <div class="col-sm-5">
                                    <input class="form-control-plaintext" ng-model="editItem.jarPath_" placeholder="请输入已知程序包URL或点击上传" ng-blur="onJarPathVerify(editItem.jarPath_)" required>
                                    <button type="button" id="advancedDropzone" class="btn btn-primary" style="margin-bottom: 5px;">&emsp;上传&emsp;</button>
                                </div>
                                <div id="dropzone_filetable" class="col-sm-5" style="padding-top: 7px;"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_timeOut" class="col-sm-2 control-label">
                                脚本执行超时(分)<B class="help" data-toggle="tooltip" data-placement="top" title="只与当前脚本的执行时间相关，与Spark或Flink任务的运行时间无关，不能超过5分钟">?</B>&nbsp;<B>*</B>
                            </label>
                            <div class="col-sm-4">
                                <input id="input_timeOut" class="form-control" type="number" min="1" max="5" autocomplete="off" ng-model="editItem.timeout" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_input" class="col-sm-2 control-label">
                                输入<B class="help" data-toggle="tooltip" data-placement="top" title="程序输入，多个数据源用“,”分隔，如：kafka:topic1">?</B>&nbsp;<B>*</B>
                            </label>
                            <div class="col-sm-4">
                                <input id="input_input" class="form-control" ng-model="editItem.input" autocomplete="off" required>
                            </div>
                            <label for="input_output" class="col-sm-2 control-label">
                                输出<B class="help" data-toggle="tooltip" data-placement="top" title="程序输出，多个数据源用“,”分隔，如：hbase:table1">?</B>&nbsp;<B>*</B>
                            </label>
                            <div class="col-sm-4">
                                <input id="input_output" class="form-control" ng-model="editItem.output" autocomplete="off" required>
                            </div>
                        </div>
                    </div>

                    <!-- 资源配置 -->
                    <div ng-show="editItem.type != 0" class="container shadow">
                        <ul class="nav nav-tabs" style="margin-bottom: 20px;">
                            <li role="presentation" class="active"><a href="#resource" aria-controls="visual" role="tab" data-toggle="tab">资源选项<B class="help" data-toggle="tooltip" data-placement="top" title="该部分参数会体现在入口函数的最后，如果对业务有影响，请自行处理">?</B></a></li>
                        </ul>
                        <div class="tab-content">
                            <div id="resource" class="tab-pane fade in active"  role="tabpanel">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">屏蔽策略<B class="help" data-toggle="tooltip" data-placement="top" title="选中的屏蔽节点，在分配任务时将被忽略">?</B></label>
                                    <div class="col-sm-4">
                                        <select class="form-control selectpicker show-tick" ng-model="editItem.nodeBlackListType" ng-change="onNodeBlackListTypeChange()" title="请选择" required>
                                            <option value="" selected>无</option>
                                            <option value="stream">非流式节点</option>
                                            <option value="batch">非批处理节点</option>
                                        </select>
                                    </div>
                                    <label class="col-sm-2 control-label">分配策略<B class="help" data-toggle="tooltip" data-placement="top" title="容器分配策略：(1) 根据总资源（根据节点总cores做判断，即使cores已被耗光也与其他节点拥有同样的权重，影响权重的因素只有代理实例的规格）、(2) 根据剩余可用资源（根据当前剩余可用的cores作为权重判断，剩余可用的cores越多则权重越高）">?</B></label>
                                    <div class="col-sm-4">
                                        <select class="form-control selectpicker show-tick" ng-model="editItem.allocateBalancerType" ng-change="onAllocateBalancerTypeChange()" title="请选择" required>
                                            <option value="" selected>无</option>
                                            <option value="total">根据总资源</option>
                                            <option value="available">根据剩余可用资源</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 脚本 -->
                    <div class="container shadow">
                        <ul class="nav nav-tabs" style="margin-bottom: 20px;">
                            <li role="presentation" class="active"><a href="#visual" aria-controls="visual" role="tab" data-toggle="tab">可视化视图</a></li>
                            <li role="presentation"><a href="#text" aria-controls="text" role="tab" data-toggle="tab">代码视图</a></li>
                        </ul>
                        <div class="tab-content">
                            <div id="visual" class="tab-pane fade in active" role="tabpanel">
                                <!-- Spark -->
                                <div ng-if="editItem.type == 1 || editItem.type == 2">
                                    <div class="div_default_block">
                                        <div class="form-group" >
                                            <label class="col-sm-2 control-label">Driver<B class="help" data-toggle="tooltip" data-placement="top" title="Driver一般不会参与计算，默认设置为512MB，1Cores即可">?</B></label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_driver_memory" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_driver_memory" class="form-control input-control" ng-model="editItem.spark.driver_memory" autocomplete="off">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_driver_cores" class="control-label">cores</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_driver_cores" class="form-control input-control" ng-model="editItem.spark.driver_cores" autocomplete="off">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">Excutor</label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_executor_memory" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_executor_memory" class="form-control input-control" ng-model="editItem.spark.executor_memory" autocomplete="off">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_num_executors" class="control-label">数量</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_num_executors" class="form-control input-control" ng-model="editItem.spark.num_executors" autocomplete="off">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_executor_cores" class="control-label">cores</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_executor_cores" class="form-control input-control" ng-model="editItem.spark.executor_cores" autocomplete="off">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">资源占用<B class="help" data-toggle="tooltip" data-placement="top" title="运行时最大总资源分配情况">?</B></label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_total_memory" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_total_memory" class="form-control input-control" ng-value="editItem.spark.$total_memory()" autocomplete="off" readonly="readonly">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_spark_total_cores" class="control-label">cores</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_spark_total_cores" class="form-control input-control" ng-value="editItem.spark.$total_cores()" autocomplete="off" readonly="readonly">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_spark_name" class="col-sm-2 control-label">
                                            Name<B class="help" data-toggle="tooltip" data-placement="top" title="参数--name，此字段应该是个唯一值，切勿与其他程序同名">?</B>&nbsp;<B>*</B>
                                        </label>
                                        <div class="col-sm-4">
                                            <input id="input_spark_name" class="form-control" ng-model="editItem.spark.name" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_spark_class" class="col-sm-2 control-label">Class<B class="help" data-toggle="tooltip" data-placement="top" title="参数--class，程序入口类，pyspark无需此项">?</B></label>
                                        <div class="col-sm-4">
                                            <input id="input_spark_class" class="form-control" ng-model="editItem.spark.class" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_spark_args" class="col-sm-2 control-label">入口参数<B class="help" data-toggle="tooltip" data-placement="top" title="入口函数的参数">?</B></label>
                                        <div class="col-sm-10">
                                            <input id="input_spark_args" class="form-control" ng-model="editItem.spark.args" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="textarea_spark_other_args" class="col-sm-2 control-label">Spark参数<B class="help" data-toggle="tooltip" data-placement="top" title="spark参数，例如：--conf spark.eventLog.enabled=true">?</B></label>
                                        <div class="col-sm-10">
                                            <textarea id="textarea_spark_other_args" class="form-control" rows="3" ng-model="editItem.spark.spark_other_args"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <!-- Flink -->
                                <div ng-if="editItem.type == 3 || editItem.type == 4">
                                    <div class="div_default_block">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">JobManager<B class="help" data-toggle="tooltip" data-placement="top" title="相当于spark的Driver">?</B></label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_yjm" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_yjm" class="form-control input-control" ng-model="editItem.flink.yjm" autocomplete="off">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">TaskManager<B class="help" data-toggle="tooltip" data-placement="top" title="相当于spark的executor">?</B></label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_ytm" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_ytm" class="form-control input-control" ng-model="editItem.flink.ytm" autocomplete="off">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_yn" class="control-label">数量</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_yn" class="form-control input-control" ng-model="editItem.flink.yn" autocomplete="off">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_ys" class="control-label">slots</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_ys" class="form-control input-control" ng-model="editItem.flink.ys" autocomplete="off">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">资源占用<B class="help" data-toggle="tooltip" data-placement="top" title="运行时最大总资源分配情况">?</B></label>
                                            <div class="col-sm-10">
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_total_memory" class="control-label">内存</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_total_memory" class="form-control input-control" ng-value="editItem.flink.$total_memory()" autocomplete="off" readonly="readonly">
                                                    </div>
                                                </div>
                                                <div class="col-sm-4 col-control">
                                                    <div class="col-sm-2" style="padding-right: 0; padding-left: 0">
                                                        <label for="input_flink_total_cores" class="control-label">cores</label>
                                                    </div>
                                                    <div class="col-sm-10" style="padding-right: 0; padding-left: 0">
                                                        <input id="input_flink_total_cores" class="form-control input-control" ng-value="editItem.flink.$total_cores()" autocomplete="off" readonly="readonly">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_flink_ynm" class="col-sm-2 control-label">
                                            Name<B class="help" data-toggle="tooltip" data-placement="top" title="参数-ynm，此字段应该是个唯一值，切莫与其他程序同名">?</B>&nbsp;<B>*</B>
                                        </label>
                                        <div class="col-sm-4">
                                            <input id="input_flink_ynm" class="form-control" ng-model="editItem.flink.ynm" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_flink_c" class="col-sm-2 control-label">Class<B class="help" data-toggle="tooltip" data-placement="top" title="参数-c，程序入口类">?</B></label>
                                        <div class="col-sm-4">
                                            <input id="input_flink_c" class="form-control" ng-model="editItem.flink.c" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="input_flink_args" class="col-sm-2 control-label">入口参数<B class="help" data-toggle="tooltip" data-placement="top" title="入口函数的参数">?</B></label>
                                        <div class="col-sm-10">
                                            <input id="input_flink_args" class="form-control" ng-model="editItem.flink.args" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="textarea_flink_other_args" class="col-sm-2 control-label">Flink参数<B class="help" data-toggle="tooltip" data-placement="top" title="fink参数，例如：-d -yst">?</B></label>
                                        <div class="col-sm-10">
                                            <textarea id="textarea_flink_other_args" class="form-control" rows="3" ng-model="editItem.flink.flink_other_args"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="text" role="tabpanel" class="tab-pane fade in">
                                <div class="form-group row">
                                    <label for="textarea_script" class="col-sm-2 control-label">脚本代码&nbsp;<B>*</B></label>
                                    <div class="col-sm-10">
                                        <textarea id="textarea_script" class="form-control" rows="10" ng-model="editItem.script" ng-blur="onScriptVerify()"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="container text-center">
                        <button type="submit" class="btn btn-primary" id="btn_submit">&emsp;&emsp;保存&emsp;&emsp;</button>
                        <button type="reset" class="btn btn-secondary">&emsp;&emsp;重置&emsp;&emsp;</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="../libs/bootstrap-select/bootstrap-select.min.js" type="text/javascript"></script>
<script src="../libs/bootstrap-select/i18n/defaults-zh_CN.js" type="text/javascript"></script>
<script src="../libs/angular-1.3.9/angular.min.js" type="text/javascript"></script>
<script src="../libs/angular-1.3.9/myangular.js" type="text/javascript"></script>
<script src="../js/common.js" type="text/javascript"></script>
<script src="../libs/dropzone/dropzone.min.js" type="application/javascript"></script>
<script th:inline="javascript">
    var app = angular.module("content-app", []);
    registerHttpInterceptor(app);

    var user = [[${session.user}]];
    var $a_visual = $('a[href="#visual"]');
    var $a_text = $('a[href="#text"]');

    var sparkVisualKey = {
        'proxy-user': 'proxy_user',
        'queue': 'queue',
        'driver-memory': 'driver_memory',
        'driver-cores': 'driver_cores',
        'executor-memory': 'executor_memory',
        'num-executors': 'num_executors',
        'executor-cores': 'executor_cores',
        'name': 'name',
        'class': 'class',
        'master': 'master',
        'deploy-mode': 'deploy_mode'
    };
    var flinkVisualKey = {
        'yjm': 'yjm',
        'ytm': 'ytm',
        'yn': 'yn',
        'ys': 'ys',
        'ynm': 'ynm',
        'c': 'c',
        'p': 'p',
        'm': 'm',
        'yqu': 'yqu'
    };

    app.controller("content-controller", function ($scope, $http, $timeout) {
        if (user.root) {
            getAuthUser($scope, $http);
        }
        appendScriptType($scope);
        getAgent($scope, $http);
        getClusterUser($scope, $http);
        getScript($scope, $http, false, true);

        var Spark = {
            'createDefault': function () {
                return {
                    'master': 'yarn',
                    'deploy_mode': 'cluster',
                    'driver_memory': '512M',
                    'driver_cores': '1',
                    'executor_memory': '1024M',
                    'num_executors': '1',
                    'executor_cores': '1',
                    '$command': $scope.sparkVersionList[0]['command'],
                    'args': '--allocate.balancer.type=total',
                    'spark_other_args': '--conf spark.yarn.submit.waitAppCompletion=false',
                    '$total_memory': this['$cal_total_memory'],
                    '$total_cores': this['$cal_total_cores']
                };
            },
            'createEmpty': function () {
                return {
                    '$total_memory': this['$cal_total_memory'],
                    '$total_cores': this['$cal_total_cores']
                }
            },
            '$cal_total_memory': function () {
                var driver_memory_str = this['driver_memory'] ? this['driver_memory'] : '512M';
                var driver_memory = parseMemory(driver_memory_str);
                var executor_memory_str = this['executor_memory'] ? this['executor_memory'] : '1024M';
                var executor_memory = parseMemory(executor_memory_str);
                var num_executors = this['num_executors'] ? this['num_executors'] : 1;
                var memoryOverhead;
                if (this['spark_other_args'] && this['spark_other_args'].indexOf('spark.yarn.executor.memoryOverhead=') !== -1) {
                    var pos = this['spark_other_args'].indexOf('spark.yarn.executor.memoryOverhead=');
                    var memoryOverhead_str = this['spark_other_args'].substring(pos + 'spark.yarn.executor.memoryOverhead='.length).split(' ')[0];
                    if (memoryOverhead_str) {
                        memoryOverhead = parseMemory(memoryOverhead_str);
                    }
                }
                if (memoryOverhead) {
                    return (num_executors * (executor_memory + memoryOverhead) + (driver_memory + memoryOverhead)) + 'M';
                } else {
                    return (num_executors * (executor_memory + Math.max(executor_memory * 0.1, 384)) + (driver_memory + Math.max(driver_memory * 0.1, 384))) + 'M';
                }
            },
            '$cal_total_cores': function () {
                var driver_cores = this['driver_cores'] ? parseInt(this['driver_cores']) : 1;
                var num_executors = this['num_executors'] ? parseInt(this['num_executors']) : 1;
                var executor_cores = this['executor_cores'] ? parseInt(this['executor_cores']) : 1;
                return num_executors * executor_cores + driver_cores;
            }
        };

        var Flink = {
            'createDefault': function () {
                return {
                    'm': 'yarn-cluster',
                    'yjm': '512',
                    'ytm': '1024',
                    'yn': '1',
                    'ys': '1',
                    '$command': $scope.flinkVersionList[0]['command'],
                    'flink_other_args': '-d',
                    '$total_memory': this['$cal_total_memory'],
                    '$total_cores': this['$cal_total_cores']
                };
            },
            'createEmpty': function () {
                return {
                    '$total_memory': this['$cal_total_memory'],
                    '$total_cores': this['$cal_total_cores']
                }
            },
            '$cal_total_memory': function () {
                var yjm_str = this['yjm'] ? this['yjm'] : '512M';
                var yjm = parseMemory(yjm_str);
                var ytm_str = this['ytm'] ? this['ytm'] : '1024M';
                var ytm = parseMemory(ytm_str);
                var yn = this['yn'] ? this['yn'] : 1;
                return (yn * ytm + yjm) + 'M';
            },
            '$cal_total_cores': function () {
                var yn = this['yn'] ? parseInt(this['yn']) : 1;
                var ys = this['ys'] ? parseInt(this['ys']) : 1;
                return yn * ys + 1;
            }
        };

        var scriptId = [[${scriptId}]];

        $scope.onOpenList = function () {
            if (parent.xadmin) {
                parent.xadmin.add_tab('脚本管理', 'script/list.html');
            } else {
                window.open('./list.html', '脚本管理');
            }
        };

        //初始化
        $scope.init = function () {
            getClusterSync($scope);
            getComputeFrameworkVersionSync($scope);
            var initDefault = function () {
                $scope.editItem = {
                    type: 1,
                    timeout: 2,
                    spark: Spark.createDefault(),
                    flink: Flink.createDefault(),
                    nodeBlackListType: 'stream',
                    allocateBalancerType: 'total'
                };
            };
            var initItem = function (item) {
                //初始化
                $scope.editItem = item;
                if ($scope.editItem.type !== 0) {
                    if ($scope.editItem.type === 1 || $scope.editItem.type === 2) {
                        //flink赋予默认值
                        $scope.editItem.flink = Flink.createDefault();
                        $timeout(function () {
                            $('#input_spark_name').attr('required', true);
                        }, 50);
                    } else {
                        //spark赋予默认值
                        $scope.editItem.spark = Spark.createDefault();
                        $timeout(function () {
                            $('#input_flink_ynm').attr('required', true);
                        }, 50);
                    }
                    $timeout(function () {
                        $('.selectpicker').selectpicker('refresh');
                    }, 50);
                    $timeout(function () {
                        $scope.parseText($scope.editItem.type);
                    }, 50);
                } else {
                    $scope.editItem.spark = Spark.createDefault();
                    $scope.editItem.flink = Flink.createDefault();
                    $a_visual.parent().addClass('disabled');
                    $a_text.tab('show');
                    $timeout(function () {
                        $('#textarea_script').attr('required', true);
                    }, 50);
                }
                $timeout(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                }, 50);
            };
            if (scriptId) {
                var query = {
                    'id': scriptId,
                    'pageNo': 1,
                    'pageSize': 1,
                    'limit': 1
                };
                $http.post('./getpage.api', query)
                    .success(function (data) {
                        if (data.content.length) {
                            var item = data.content[0];
                            if (item.type !== 0) {
                                query = {
                                    'uid': item.uid,
                                    'clusterId': item.clusterId,
                                    'pageNo': 1,
                                    'pageSize': 1,
                                    'limit': 1
                                };
                                $http.post('../cluster/cluster_user/getpage.api', query)
                                    .success(function (data) {
                                        if (data.content.length) {
                                            var clusterUser = data.content[0];
                                            $scope.queueList = clusterUser.queue.split(',');
                                            if (item.script.indexOf('--queue ') !== -1) {
                                                item.queue_ = item.script.match(/--queue [\w-.,]+/g)[0].split(' ')[1];
                                            }
                                            if (item.script.indexOf('-yqu ') !== -1) {
                                                item.queue_ = item.script.match(/-yqu [\w-.,]+/g)[0].split(' ')[1];
                                            }
                                        }
                                        initItem(item);
                                    });
                            } else {
                                initItem(item);
                            }
                        } else {
                            initDefault();
                        }
                    });
            } else {
                initDefault();
            }
        };

        $scope.onUidChange = function (uid) {
            dealQueueAndProxyUser(uid || '', $scope.editItem.clusterId || '');
            $timeout(function () {
                $('.selectpicker').selectpicker('refresh');
            }, 50);
            $scope.parseVisual($scope.editItem.type);
        };

        //类型变更事件
        $scope.onTypeChange = function (type) {
            $scope.editItem.script = '';
            if (type !== 0) {
                if (type === 1 || type === 3) {
                    $scope.editItem.nodeBlackListType = 'stream';
                    $scope.editItem.allocateBalancerType = 'total';
                } else if (type === 2 || type === 4) {
                    $scope.editItem.nodeBlackListType = 'batch';
                    $scope.editItem.allocateBalancerType = 'available';
                }
                $a_visual.parent().removeClass('disabled');
                $scope.onNodeBlackListTypeChange();
                $scope.onAllocateBalancerTypeChange();
                $timeout(function () {
                    if ($a_visual.parent().hasClass('active')) {
                        if (type === 1 || type === 2) {
                            $('#input_spark_name').attr('required', true);
                            $('#input_flink_ynm').removeAttr('required');
                        } else {
                            $('#input_flink_ynm').attr('required', true);
                            $('#input_spark_name').removeAttr('required');
                        }
                        $('#textarea_script').removeAttr('required');
                    } else {
                        $('#input_spark_name').removeAttr('required');
                        $('#input_flink_ynm').removeAttr('required');
                        $('#textarea_script').attr('required', true);
                    }
                }, 50);
                $timeout(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                }, 50);
                if (!$('#advancedDropzone').length) {
                    $timeout(function () {
                        initUpload();
                    }, 50);
                }
            } else {
                $a_visual.parent().addClass('disabled');
                $a_text.tab('show');
                $timeout(function () {
                    $('#textarea_script').attr('required', true);
                }, 50);
            }
            $scope.parseVisual(type);
        };

        $scope.onSparkVersionChange = function (command) {
            var script = $scope.editItem.script;
            if (script) {
                if (script.indexOf(' ') !== -1) {
                    script = command + script.substring(script.indexOf(' '));
                } else {
                    script = command + ' ';
                }
            } else {
                script = command + ' ';
            }
            $scope.editItem.script = script;
        };

        $scope.onFlinkVersionChange = function (command) {
            var script = $scope.editItem.script;
            if (script) {
                if (script.indexOf(' run ') !== -1) {
                    script = command + script.substring(script.indexOf(' run '));
                } else {
                    script = command + ' run ';
                }
            } else {
                script = command + ' run ';
            }
            $scope.editItem.script = script;
        };

        $scope.onClusterIdChange = function (clusterId) {
            dealQueueAndProxyUser($scope.editItem.uid || user.id, clusterId || '');
            $timeout(function () {
                $('.selectpicker').selectpicker('refresh');
            }, 50);
            $scope.onNodeBlackListTypeChange();
            $scope.parseVisual($scope.editItem.type);
        };

        //处理队列和代理用户
        function dealQueueAndProxyUser(uid, clusterId) {
            var clusterUser = $scope.clusterUserMap[uid + '_' + clusterId];
            var tmp;
            if (clusterUser) {
                var queueArr = clusterUser.queue.split(',');
                $scope.editItem.queue_ = queueArr[0];
                $scope.queueList = queueArr;
                $scope.editItem.spark.queue = queueArr[0];
                $scope.editItem.flink.yqu = queueArr[0];
                if (clusterUser.user) {
                    $scope.editItem.spark.proxy_user = clusterUser.user;
                    tmp = $scope.editItem.flink.flink_other_args;
                    if (tmp) {
                        if (tmp.indexOf('-yD ypu=') !== -1) {
                            tmp = tmp.replace(/-yD ypu=[\w-.,]+/g, '-yD ypu=' + clusterUser.user);
                        } else {
                            tmp += ' -yD ypu=' + clusterUser.user;
                        }
                    } else {
                        tmp = '-yD ypu=' + clusterUser.user;
                    }
                    $scope.editItem.flink.flink_other_args = tmp;
                }
            } else {
                $scope.editItem.queue_ = '';
                $scope.queueList = [];
                $scope.editItem.spark.queue = '';
                $scope.editItem.flink.yqu = '';
                $scope.editItem.spark.proxy_user = '';
                tmp = $scope.editItem.flink.flink_other_args;
                if (tmp && tmp.indexOf('-yD ypu=') !== -1) {
                    tmp = tmp.replace(/\s*-yD ypu=[\w-.,]+/g, '').resetBlank();
                    if (tmp.length >= 1 && tmp.substring(0, 1) === ' ') {
                        tmp = tmp.substring(1);
                    }
                    $scope.editItem.flink.flink_other_args = tmp;
                }
            }
        }

        //队列变更
        $scope.onQueueChange = function (queue) {
            $scope.editItem.spark.queue = queue || '';
            $scope.editItem.flink.yqu = queue || '';
            $scope.parseVisual($scope.editItem.type);
        };

        //程序包变更事件
        $scope.onJarPathVerify = function (jarPath_) {
            if (jarPath_) {
                if (!jarPath_.endsWith('.jar') && !jarPath_.endsWith('.py')) {
                    swal('程序包检验错误', '文件格式错误', 'error');
                    return;
                }
                $scope.parseVisual($scope.editItem.type);
            }
        };

        //选择屏蔽节点
        $scope.onNodeBlackListTypeChange = function() {
            var type = $scope.editItem['nodeBlackListType'];
            $scope.editItem.spark.args = dealBlackListType(type, $scope.editItem.spark.args);
            $scope.editItem.flink.args = dealBlackListType(type, $scope.editItem.flink.args);
            $scope.editItem.script = dealBlackListType(type, $scope.editItem.script);
        };
        function dealBlackListType(type, target) {
            if (!target) {
                target = '';
            }
            var cluster = $scope.clusterMap[$scope.editItem.clusterId];
            var nodeList;
            if (type && cluster) {
                if (type === 'stream'){
                    if (cluster['streamingBlackNodeList']) {
                        nodeList = cluster['streamingBlackNodeList'];
                    } else {
                        swal({
                            position: 'center',
                            type: 'warning',
                            title: '当前集群暂无屏蔽策略对应黑名单',
                            showConfirmButton: false,
                            timer: 2000,
                            toast: true
                        });
                    }
                } else {
                    if (cluster['batchBlackNodeList']) {
                        nodeList = cluster['batchBlackNodeList'];
                    } else {
                        swal({
                            position: 'center',
                            type: 'warning',
                            title: '当前集群暂无屏蔽策略对应黑名单',
                            showConfirmButton: false,
                            timer: 2000,
                            toast: true
                        });
                    }
                }
            }
            if (nodeList) {
                if (target.indexOf('--node.black.list=') !== -1) {
                    target = target.replace(/--node.black.list=[\w-.,]+/g, '--node.black.list=' + nodeList).resetBlank();
                } else {
                    if (target) {
                        target += ' --node.black.list=' + nodeList;
                    }  else {
                        target += '--node.black.list=' + nodeList;
                    }
                }
            } else {
                if (target.indexOf('--node.black.list=') !== -1) {
                    target = target.replace(/\s*--node.black.list=[\w-.,]+/g, '').resetBlank();
                    if (target.length >= 1 && target.substring(0, 1) === ' ') {
                        target = target.substring(1);
                    }
                }
            }
            return target;
        }

        //选择容器分配策略
        $scope.onAllocateBalancerTypeChange = function() {
            var type = $scope.editItem['allocateBalancerType'];
            if ($scope.editItem.type === 1 || $scope.editItem.type === 2) {
                $scope.editItem.spark.args = dealAllocateBalancerType(type, $scope.editItem.spark.args);
            } else {
                $scope.editItem.flink.args = dealAllocateBalancerType(type, $scope.editItem.flink.args);
            }
            $scope.editItem.script = dealAllocateBalancerType(type, $scope.editItem.script);
        };
        function dealAllocateBalancerType(type, target) {
            if (!target) {
                target = '';
            }
            if (type) {
                if (target.indexOf('--allocate.balancer.type=') !== -1) {
                    target = target.replace(/--allocate.balancer.type=[\w-.,]+/g, '--allocate.balancer.type=' + type).resetBlank();
                } else {
                    if (target) {
                        target += ' --allocate.balancer.type=' + type;
                    }  else {
                        target += '--allocate.balancer.type=' + type;
                    }
                }
            } else {
                if (target.indexOf('--allocate.balancer.type=') !== -1) {
                    target = target.replace(/\s*--allocate.balancer.type=[\w-.,]+/g, '').resetBlank();
                    if (target.length >= 1 && target.substring(0, 1) === ' ') {
                        target = target.substring(1);
                    }
                }
            }
            return target;
        }

        $scope.onScriptVerify = function () {
            if ($scope.editItem.type !== 0) {
                $scope.parseText($scope.editItem.type);
            }
        };

        //保存
        $scope.onSaveItem = function() {
            if ($a_visual.parent().hasClass('active')) {
                $scope.parseVisual($scope.editItem.type);
            }
            if ($scope.editItem.type !== 0) {
                if ($scope.editItem.script.indexOf($scope.editItem.jarPath_) === -1) {
                    swal('脚本校验失败', '请指定程序包路径', 'error');
                    return;
                }
                if ($scope.editItem.type === 1 || $scope.editItem.type === 2) {
                    if ($scope.editItem.script.indexOf('spark.yarn.submit.waitAppCompletion=true') !== -1) {
                        swal('脚本校验失败', '禁用Spark参数: spark.yarn.submit.waitAppCompletion=true，因为该参数会消耗平台大量线程并使代理实例长时间处于高负载状态', 'error');
                        return;
                    }
                } else {
                    if ($scope.editItem.script.indexOf(' -d ') === -1) {
                        swal('脚本校验失败', '请添加Flink参数: -d，以后台模式执行，否则会消耗平台大量线程并使代理实例长时间处于高负载状态', 'error');
                        return;
                    }
                }
            }
            $('#btn_submit').attr('disabled','true');
            $http.post("./save.api", $scope.editItem)
                .success(function(data) {
                    $('#btn_submit').removeAttr('disabled');
                    swal({
                        title: '保存成功',
                        type: 'success',
                        showConfirmButton: false,
                        timer: 1500
                    }).then(function () {
                        if (parent.xadmin) {
                            closeTab();
                        } else {
                            location.href = "./list.html";
                        }
                    });
                })
                .error(function (data) {
                    $('#btn_submit').removeAttr('disabled');
                });
        };

        //可视化切换
        $a_visual.on('show.bs.tab', function(e) {
            //shell类型脚本禁用可视化
            if ($a_visual.parent().hasClass('disabled')) {
                e.preventDefault();
                return;
            }
            if ($scope.editItem.type === 1 || $scope.editItem.type === 2) {
                $('#input_spark_name').attr('required', true);
                $('#input_flink_ynm').removeAttr('required');
            } else {
                $('#input_flink_ynm').attr('required', true);
                $('#input_spark_name').removeAttr('required');
            }
            $('#textarea_script').removeAttr('required');
            $scope.parseText($scope.editItem.type);
        });
        $a_text.on('show.bs.tab', function() {
            $('#input_spark_name').removeAttr('required');
            $('#input_flink_ynm').removeAttr('required');
            $('#textarea_script').attr('required', true);
            $scope.parseVisual($scope.editItem.type);
        });
        $scope.parseText = function (type) {
            if (type === 1 || type === 2) {
                $scope.parseSparkText();
            } else if (type === 3 || type === 4) {
                $scope.parseFlinkText();
            }
            $timeout(function () {
                $('.selectpicker').selectpicker('render');
            }, 50);
        };
        $scope.parseVisual = function (type) {
            if (type === 1 || type === 2) {
                $scope.parseSparkVisual();
            } else if (type === 3 || type === 4) {
                $scope.parseFlinkVisual();
            }
            $timeout(function () {
                $('.selectpicker').selectpicker('render');
            }, 50);
        };
        $scope.parseSparkText = function () {
            var tokens = $scope.editItem.script ? $scope.editItem.script.resetBlank().split(' ') : [];
            var spark;
            var jarIndex;
            var key;
            //获取jar包索引位
            tokens.forEach(function(value, index) {
                if (value.indexOf('.jar') !== -1 || value.indexOf('.py') !== -1) {
                    if (tokens[index - 1] !== '--jars' && tokens[index - 1] !== '-j' && tokens[index - 1] !== '--jar') {
                        if (jarIndex === undefined) {
                            jarIndex = index;
                        }
                    }
                }
            });
            if (jarIndex === undefined) {
                $scope.editItem.jarPath_ = '';
            }
            if (tokens.length) {
                spark = Spark.createEmpty();
                for (var i = 0; i < tokens.length; i ++) {
                    var token = tokens[i].trim();
                    if (!token) {
                        continue;
                    }
                    if ($scope.sparkVersionMap[token]) {
                        spark['$command'] = token;
                        continue;
                    }
                    if (jarIndex !== undefined && i === jarIndex) {
                        if (!$scope.editItem.jarPath_) {
                            $scope.editItem.jarPath_ = token;
                        } else {
                            $scope.editItem.script = $scope.editItem.script.replace(token, $scope.editItem.jarPath_.trim());
                        }
                        continue;
                    }
                    if (token.startsWith('--conf')) {
                        if (i >= tokens.length - 1) {
                            return;
                        }
                        token = tokens[++i];
                        if (!token) {
                            return;
                        }
                        var tokenArr = token.split('=');
                        key = sparkVisualKey[tokenArr[0]];
                        if (key) {
                            spark[key] = tokenArr[1];
                        } else {
                            if (jarIndex !== undefined && i > jarIndex) {
                                if (spark.args) {
                                    spark.args += ' --conf ' + token;
                                } else {
                                    spark.args = '--conf ' + token;
                                }
                            } else {
                                if (spark.spark_other_args) {
                                    spark.spark_other_args += ' --conf ' + token;
                                } else {
                                    spark.spark_other_args = '--conf ' + token;
                                }
                            }
                        }
                    } else {
                        if ($scope.caseResource(token, spark)) {
                            continue;
                        }
                        key = sparkVisualKey[token.substring('--'.length)];
                        if (key) {
                            spark[key] = tokens[++i];
                        } else {
                            if (jarIndex !== undefined && i > jarIndex) {
                                if (spark.args) {
                                    spark.args += ' ' + token;
                                } else {
                                    spark.args = token;
                                }
                            } else {
                                if (spark.spark_other_args) {
                                    spark.spark_other_args += ' ' + token;
                                } else {
                                    spark.spark_other_args = token;
                                }
                            }
                        }
                    }
                }
            } else {
                spark = Spark.createDefault();
            }
            $scope.editItem.spark = spark;
        };
        $scope.parseSparkVisual = function () {
            var spark = $scope.editItem.spark;
            var script = $scope.editItem.script || '';
            script = script.resetBlank();
            if (script) {
                if (!script.startsWith(spark.$command)) {
                    script = spark.$command + ' ' + script;
                }
            } else {
                script = spark.$command + ' ';
            }
            script = script.substring(0, script.indexOf(' '));
            for (var key in spark){
                var keyTmp = key;
                if (keyTmp !== 'args' && keyTmp !== 'spark_other_args' && keyTmp.indexOf('$') === -1) {
                    var value = spark[keyTmp];
                    keyTmp = keyTmp.replace(/_/g, '-');
                    if (keyTmp.startsWith('spark')) {
                        keyTmp = keyTmp.replace(/-/g, ".");
                    }
                    if (value) {
                        value = value.toString().trim();
                        if (keyTmp.startsWith('spark')) {
                            script += ' --conf ' + keyTmp + '=' + value;
                        } else {
                            script += ' --' + keyTmp + ' ' + value;
                        }
                    }
                }
            }
            if (spark['spark_other_args']) {
                script += ' ' + spark['spark_other_args'].trim();
            }
            if ($scope.editItem.jarPath_) {
                script += ' ' + $scope.editItem.jarPath_.trim();
            }
            if (spark['args']) {
                script += ' ' + spark['args'].trim();
            }
            $scope.editItem.script = script;
        };
        $scope.parseFlinkText = function () {
            var tokens = $scope.editItem.script ? $scope.editItem.script.resetBlank().split(' ') : [];
            var flink;
            var jarIndex;
            var key;
            //获取jar包索引位
            tokens.forEach(function (value, index) {
                if (value.indexOf('.jar') !== -1 || value.indexOf('.py') !== -1) {
                    if (tokens[index - 1] !== '--jars' && tokens[index - 1] !== '-j' && tokens[index - 1] !== '--jar') {
                        if (jarIndex === undefined) {
                            jarIndex = index;
                        }
                    }
                }
            });
            if (jarIndex === undefined) {
                $scope.editItem.jarPath_ = '';
            }
            if (tokens.length) {
                flink = Flink.createEmpty();
                for (var i = 0; i < tokens.length; i ++) {
                    var token = tokens[i].trim();
                    if (!token) {
                        continue;
                    }
                    if ($scope.flinkVersionMap[token]) {
                        flink['$command'] = token;
                        continue;
                    }
                    if (token === 'run') {
                        continue;
                    }
                    if (jarIndex !== undefined && i === jarIndex) {
                        if (!$scope.editItem.jarPath_) {
                            $scope.editItem.jarPath_ = token;
                        } else {
                            $scope.editItem.script = $scope.editItem.script.replace(token, $scope.editItem.jarPath_.trim());
                        }
                        continue;
                    }
                    if ($scope.caseResource(token, flink)) {
                        continue;
                    }
                    key = flinkVisualKey[token.substring('-'.length)];
                    if (key) {
                        flink[key] = tokens[++i];
                    } else {
                        if (jarIndex !== undefined && i > jarIndex) {
                            if (flink.args) {
                                flink.args += ' ' + token;
                            } else {
                                flink.args = token;
                            }
                        } else {
                            if (flink.flink_other_args) {
                                flink.flink_other_args += ' ' + token;
                            } else {
                                flink.flink_other_args = token;
                            }
                        }
                    }
                }
            } else {
                flink = Flink.createDefault();
            }
            $scope.editItem.flink = flink;
        };
        $scope.parseFlinkVisual = function () {
            var flink = $scope.editItem.flink;
            var script = $scope.editItem.script ? $scope.editItem.script : '';
            script = script.resetBlank();
            if (script !== '') {
                if (!script.startsWith(flink.$command)) {
                    script = flink.$command + ' run ' + script;
                }
            } else {
                script = flink.$command + ' run ';
            }
            script = script.substring(0, script.indexOf('run') + 'run'.length);
            for (var key in flink){
                var keyTmp = key;
                if (keyTmp !== 'args' && keyTmp !== 'flink_other_args' && keyTmp.indexOf('$') === -1) {
                    var value = flink[keyTmp];
                    if (keyTmp.startsWith('flink')) {
                        keyTmp = keyTmp.replace(/_/g, ".");
                    }
                    keyTmp = '-' + keyTmp;
                    if (value) {
                        value = value.toString().trim();
                        script += ' ' + keyTmp + ' ' + value;
                    }
                }
            }
            if (flink['flink_other_args']) {
                script += ' ' + flink['flink_other_args'].trim();
            }
            if ($scope.editItem.jarPath_) {
                script += ' ' + $scope.editItem.jarPath_.trim();
            }
            if (flink['args']) {
                script += ' ' + flink['args'].trim();
            }
            $scope.editItem.script = script;
        };
        $scope.caseResource = function (token, target) {
            //处理屏蔽节点
            if (token.indexOf('--node.black.list=') !== -1) {
                if ($scope.editItem.clusterId) {
                    var cluster = $scope.clusterMap[$scope.editItem.clusterId];
                    if (token.split('=')[1] === cluster['streamingBlackNodeList']) {
                        $scope.editItem['nodeBlackListType'] = 'stream';
                    } else if (token.split('=')[1] === cluster['batchBlackNodeList']) {
                        $scope.editItem['nodeBlackListType'] = 'batch';
                    } else {
                        $scope.editItem['nodeBlackListType'] = '';
                    }
                    if (target.args) {
                        target.args += ' ' + token;
                    } else {
                        target.args = token;
                    }
                }
                return true;
            }
            //处理容器分配策略
            if (token.indexOf('--allocate.balancer.type=') !== -1) {
                if (token.split('=')[1] === 'total' || token.split('=')[1] === 'available') {
                    $scope.editItem['allocateBalancerType'] = token.split('=')[1];
                } else {
                    $scope.editItem['allocateBalancerType'] = '';
                }
                if (target.args) {
                    target.args += ' ' + token;
                } else {
                    target.args = token;
                }
                return true;
            }
            return false;
        };

        //上传控件
        function initUpload() {
            $('#advancedDropzone').dropzone({
                url: '../hdfs/upload',
                autoProcessQueue: false,
                // Events
                addedfile: function(file) {
                    if (!file.name.endsWith('.jar') && !file.name.endsWith('.py')) {
                        swal('程序包检验错误', '文件格式错误', 'error');
                        this.removeFile(file);
                        return;
                    }
                    $('#advancedDropzone').attr('disabled', 'true');
                    var $dropzoneFiletable = $('#dropzone_filetable');
                    //先清除子元素
                    $dropzoneFiletable.empty();
                    var $el = $(
                        '<div class="col-sm-6">' +
                        '<div class="progress progress-striped">' +
                        '<div class="progress-bar progress-bar-warning"></div>' +
                        '</div>' +
                        '</div>' +
                        '<div class="col-sm-6">' +
                        '<span>上传中...</span>' +
                        '</div>');
                    $dropzoneFiletable.append($el);
                    file.fileEntryTd = $el;
                    file.progressBar = $el.find('.progress-bar');
                    var dropzone = this;
                    $timeout(function () {
                        dropzone.processQueue();
                    }, 50);
                },
                sending:function(file, xhr, formData){
                    if ($scope.editItem.clusterId) {
                        formData.append('clusterId', $scope.editItem.clusterId);
                    }
                },
                uploadprogress: function(file, progress) {
                    file.progressBar.width(progress + '%');
                },
                success: function(file, data) {
                    $("#advancedDropzone").removeAttr('disabled');
                    if (data.code === 0) {
                        file.fileEntryTd.find('span:last').html('<span class="text-success">成功</span>');
                        file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-success');
                        $scope.editItem.jarPath_ = data.content;
                        $scope.parseVisual($scope.editItem.type);
                    } else {
                        file.fileEntryTd.find('span:last').html('<span class="text-danger">失败</span>');
                        file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-danger');
                        $scope.editItem.jarPath_ = '';
                        if (data.code === -999) {
                            swal({
                                title: '后台接口异常，请联系管理员',
                                type: 'error',
                                showConfirmButton: true
                            });
                        } else {
                            swal({
                                title: data.msg || "操作失败",
                                type: 'warning',
                                showConfirmButton: true
                            });
                        }
                    }
                },
                error: function(file, data) {
                    $('#advancedDropzone').removeAttr('disabled');
                    file.fileEntryTd.find('span:last').html('<span class="text-danger">失败</span>');
                    file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-red');
                    $scope.editItem.jarPath_ = '';
                    swal({
                        title: '后台接口异常，请联系管理员',
                        type: 'error',
                        showConfirmButton: true
                    });
                }
            });
        }

        function closeTab() {
            var el = parent.document.querySelector('li.layui-this').getElementsByClassName('layui-icon layui-unselect layui-tab-close')[0];
            $timeout(function () {
                parent.xadmin.reloadScript();
                el.click();
            }, 50);
            parent.xadmin.add_tab('脚本管理', 'script/list.html');
        }

        //多个空格转成一个空格
        String.prototype.resetBlank = function(){
            var regEx = /\s+/g;
            return this.replace(regEx, ' ');
        };

        //提示控件
        $timeout(function () {
            $('[data-toggle="tooltip"]').tooltip();
        },50);

        $timeout(function () {
            $('.selectpicker').selectpicker();
        }, 50);

        $timeout(function () {
            initUpload();
        }, 50);

        $scope.init();

    });

    function parseMemory(present) {
        present = present.toUpperCase();
        var val;
        if (present.indexOf('M') !== -1) {
            val = parseInt(present.substring(0, present.indexOf('M')));
        } else if (present.indexOf('G') !== -1) {
            val = parseInt(present.substring(0, present.indexOf('G'))) * 1024;
        } else {
            val = parseInt(present);
        }
        return val;
    }

</script>
</body>
</html>