{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends base_template %}
{% from 'appbuilder/dag_docs.html' import dag_docs %}

{% block page_title %}{{ dag.dag_id }} - Airflow{% endblock %}

{% block head_css %}
  {{ super() }}
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('switch.css') }}">
{% endblock %}

{% block content %}
  {% set base_date_arg = request.args.get('base_date') %}
  {% set num_runs_arg = request.args.get('num_runs') %}
  {% if execution_date is defined %}
    {% set execution_date_arg = execution_date %}
  {% else %}
    {% set execution_date_arg = request.args.get('execution_date') %}
  {% endif %}
  {% if dag.parent_dag is defined and dag.parent_dag %}
    <a href="{{ url_for('Airflow.' + dag.default_view, dag_id=dag.parent_dag.dag_id, base_date=base_date_arg, execution_date=execution_date_arg) }}" title="Go to parent DAG">
      <span class="material-icons" aria-hidden="true">keyboard_arrow_up</span>
      DAG: {{ dag.parent_dag.dag_id }}</a>
  {% endif %}

  <div>
    <h3 class="pull-left">
      {% if dag.parent_dag is defined and dag.parent_dag %}
        <span class="text-muted">SUBDAG:</span> {{ dag.dag_id }}
      {% else %}
        {% set can_edit = appbuilder.sm.can_edit_dag(dag.dag_id) %}
        {% if appbuilder.sm.can_edit_dag(dag.dag_id) %}
          {% set switch_tooltip = 'Pause/Unpause DAG' %}
        {% else %}
          {% set switch_tooltip = 'DAG is Paused' if dag.is_paused else 'DAG is Active' %}
        {% endif %}
        <label class="switch-label{{' disabled' if not can_edit else ''  }} js-tooltip" title="{{ switch_tooltip }}">
          <input class="switch-input" id="pause_resume" data-dag-id="{{ dag.dag_id }}"
                 type="checkbox"{{ " checked" if not dag.is_paused else "" }}
                 {{ " disabled" if not can_edit else "" }}>
          <span class="switch" aria-hidden="true"></span>
        </label>
        <span class="text-muted">DAG:</span> {{ dag.dag_id }}
        <small class="text-muted">{{ dag.description[0:150] + '…' if dag.description and dag.description|length > 150 else dag.description|default('', true) }}</small>
      {% endif %}
      {% if root %}
        <span class="text-muted">ROOT:</span> {{ root }}
      {% endif %}
    </h3>
    <h4 class="pull-right" style="user-select: none;-moz-user-select: auto;">
      {% if state_token is defined and state_token %}
        {{ state_token }}
      {% endif %}
      <a class="label label-default" href="{{ url_for('DagRunModelView.list') }}?_flt_3_dag_id={{ dag.dag_id }}">
        schedule: {{ dag.schedule_interval }}
      </a>
    </h4>
  </div>
  <div class="clearfix"></div>
  <br>
  <div>
    <div class="row">
      <div class="col-md-10">
        <ul class="nav nav-pills">
          <li><a href="{{ url_for('Airflow.tree', dag_id=dag.dag_id, num_runs=num_runs_arg, root=root, base_date=base_date_arg) }}">
            <span class="material-icons" aria-hidden="true">nature</span>
            Tree View
          </a></li>
          <li><a href="{{ url_for('Airflow.graph', dag_id=dag.dag_id, root=root, num_runs=num_runs_arg, base_date=base_date_arg, execution_date=execution_date_arg) }}">
            <span class="material-icons" aria-hidden="true">account_tree</span>
            Graph View</a></li>
          <li><a href="{{ url_for('Airflow.duration', dag_id=dag.dag_id, days=30, root=root, num_runs=num_runs_arg, base_date=base_date_arg) }}">
            <span class="material-icons" aria-hidden="true">hourglass_bottom</span>
            Task Duration</a></li>
          <li><a href="{{ url_for('Airflow.tries', dag_id=dag.dag_id, days=30, root=root, num_runs=num_runs_arg, base_date=base_date_arg) }}">
            <span class="material-icons" aria-hidden="true">repeat</span>
            Task Tries</a></li>
          <li><a href="{{ url_for('Airflow.landing_times', dag_id=dag.dag_id, days=30, root=root, num_runs=num_runs_arg, base_date=base_date_arg) }}">
            <span class="material-icons" aria-hidden="true">flight_land</span>
            Landing Times</a></li>
          <li><a href="{{ url_for('Airflow.gantt', dag_id=dag.dag_id, root=root, num_runs=num_runs_arg, base_date=base_date_arg, execution_date=execution_date_arg) }}">
            <span class="material-icons" aria-hidden="true">vertical_distribute</span>
            Gantt</a></li>
          <li><a href="{{ url_for('Airflow.dag_details', dag_id=dag.dag_id) }}">
            <span class="material-icons" aria-hidden="true">details</span>
            Details</a></li>
          <li><a href="{{ url_for('Airflow.code', dag_id=dag.dag_id, root=root) }}">
            <span class="material-icons" aria-hidden="true">code</span>
            Code</a></li>
        </ul>
      </div>
      <div class="col-md-2">
        <div class="btn-group pull-right">
          <a href="{{ url_for('Airflow.trigger', dag_id=dag.dag_id, origin=url_for('Airflow.' + dag.default_view, dag_id=dag.dag_id)) }}" title="Trigger DAG" aria-label="Trigger DAG" class="btn btn-default btn-icon-only">
            <span class="material-icons" aria-hidden="true">play_arrow</span>
          </a>
          <a href="{{ url_for('Airflow.refresh', dag_id=dag.dag_id) }}" title="Refresh DAG" aria-label="Refresh DAG" onclick="postAsForm(this.href); return false" class="btn btn-default btn-icon-only">
            <span class="material-icons" aria-hidden="true">refresh</span>
          </a>
          <a href="{{ url_for('Airflow.delete', dag_id=dag.dag_id) }}" title="Delete&nbsp;DAG" class="btn btn-default btn-icon-only"
            onclick="return confirmDeleteDag(this, '{{ dag.safe_dag_id }}')" aria-label="Delete DAG">
            <span class="material-icons text-danger" aria-hidden="true">delete_outline</span>
          </a>
        </div>
      </div>
    </div>
  </div>
  {{ dag_docs(doc_md) }}
  <!-- Modal for task instance -->
  <div class="modal fade" id="taskInstanceModal" tabindex="-1" role="dialog" aria-labelledby="taskInstanceModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="taskInstanceModalLabel">
            <span class="text-muted">Task Instance:</span> <span id="task_id"></span>
            <br><span class="text-muted">at:</span> <span id="execution_date"></span>
          </h4>
        </div>
        <div class="modal-body">
          <div id="div_btn_subdag">
            <a id="btn_subdag" class="btn btn-primary" data-base-url="{{ url_for('Airflow.' + dag.default_view) }}">
              Zoom into Sub DAG
            </a>
            <hr>
          </div>
          <a id="btn_task" class="btn btn-sm" data-base-url="{{ url_for('Airflow.task') }}">
            Instance Details
          </a>
          <a id="btn_rendered" class="btn btn-sm" data-base-url="{{ url_for('Airflow.rendered_templates') }}">
            Rendered
          </a>
          {% if k8s_or_k8scelery_executor %}
            <a id="btn_rendered_k8s" class="btn btn-sm" data-base-url="{{ url_for('Airflow.rendered_k8s') }}">
              K8s Pod Spec
            </a>
          {% endif %}
          <a id="btn_log" class="btn btn-sm" data-base-url="{{ url_for('Airflow.log') }}">
            Log
          </a>
          <a id="btn_ti" class="btn btn-sm" data-base-url="{{ url_for('TaskInstanceModelView.list') }}">
            All Instances
          </a>
          <a id="btn_te" class="btn btn-sm" data-base-url="{{ url_for('TaskExecutionModelView.list') }}">
            All Executions
          </a>
          <button id="btn_filter" type="button" class="btn btn-sm" title="Filter on this task and upstream ">
            Filter Upstream
          </button>
          <hr>
          <div id="dag_dl_logs">
            <label style="display:inline">Download Log (by attempts):</label>
            <ul class="nav nav-pills" role="tablist" id="try_index" style="display:inline">
            </ul>
            <hr>
          </div>
          {% if external_log_name is defined %}
            <div id="dag_redir_logs">
              <label style="display:inline"> View Logs in {{ external_log_name }} (by attempts):</label>
              <ul class="nav nav-pills" role="tablist" id="redir_log_try_index" style="display:inline">
              </ul>
              <hr>
              <hr>
            </div>
          {% endif %}
          <h4>Task Actions</h4>
          <form method="POST" data-action="{{ url_for('Airflow.run') }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="dag_id" value="{{ dag.dag_id }}">
            <input type="hidden" name="task_id">
            <input type="hidden" name="execution_date">
            <input type="hidden" name="origin" value="{{ request.base_url }}">
            <div class="row">
              <span class="btn-group col-xs-12 col-sm-9 task-instance-modal-column" data-toggle="buttons">
                <label
                  class="btn btn-default"
                  title="Ignores all non-critical dependencies, including task state and task_deps">
                  <input type="checkbox" value="true" name="ignore_all_deps" autocomplete="off">
                  Ignore All Deps</label>
                <label class="btn btn-default"
                  title="Ignore previous success/failure">
                  <input type="checkbox" value="true" name="ignore_ti_state" autocomplete="off">
                  Ignore Task State
                </label>
                <label class="btn btn-default"
                  title="Disregard the task-specific dependencies, e.g. status of upstream task instances and depends_on_past">
                  <input type="checkbox" value="true" name="ignore_task_deps" autocomplete="off">
                  Ignore Task Deps
                </label>
              </span>
              <span class="col-xs-12 col-sm-3 task-instance-modal-column">
                <button type="submit" id="btn_run" class="btn btn-primary btn-block" title="Runs a single task instance">
                  Run
                </button>
              </span>
            </div>
          </form>
          <hr style="margin-bottom: 8px;">
          <form method="POST" data-action="{{ url_for('Airflow.clear') }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="dag_id" value="{{ dag.dag_id }}">
            <input type="hidden" name="task_id">
            <input type="hidden" name="execution_date">
            <input type="hidden" name="origin" value="{{ request.base_url }}">
            <div class="row">
              <span class="btn-group col-xs-12 col-sm-9 task-instance-modal-column" data-toggle="buttons">
                <label class="btn btn-default btn-sm" title="Also include past task instances when clearing this one">
                  <input type="checkbox" value="true" name="past" autocomplete="off">
                  Past
                </label>
                <label class="btn btn-default btn-sm" title="Also include future task instances when clearing this one">
                  <input type="checkbox" value="true" name="future" autocomplete="off">
                  Future
                </label>
                <label class="btn btn-default btn-sm" title="Also include upstream dependencies">
                  <input type="checkbox" value="true" name="upstream" autocomplete="off">
                  Upstream
                </label>
                <label class="btn btn-default btn-sm active" title="Also include downstream dependencies">
                  <input type="checkbox" value="true" name="downstream" checked autocomplete="off">
                  Downstream
                </label>
                <label class="btn btn-default btn-sm active">
                  <input type="checkbox" value="true" name="recursive" checked autocomplete="off">
                  Recursive
                </label>
                <label class="btn btn-default btn-sm" title="Only consider failed task instances when clearing this one">
                  <input type="checkbox" value="true" name="only_failed" autocomplete="off">
                  Failed
                </label>
              </span>
              <span class="col-xs-12 col-sm-3 task-instance-modal-column">
                <button type="submit" id="btn_clear" class="btn btn-primary btn-block"
                    title="Clearing deletes the previous state of the task instance, allowing it to get re-triggered by the scheduler or a backfill command">
                  Clear
                </button>
              </span>
            </div>
          </form>
          <hr style="margin-bottom: 8px;">
          <form method="POST" data-action="{{ url_for('Airflow.failed') }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="dag_id" value="{{ dag.dag_id }}">
            <input type="hidden" name="task_id">
            <input type="hidden" name="execution_date">
            <input type="hidden" name="origin" value="{{ request.base_url }}">
            <div class="row">
              <span class="btn-group col-xs-12 col-sm-9 task-instance-modal-column" data-toggle="buttons">
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="failed_past" autocomplete="off">
                  Past
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="failed_future" autocomplete="off">
                  Future
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="failed_upstream" autocomplete="off">
                  Upstream
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="failed_downstream" autocomplete="off">
                  Downstream
                </label>
              </span>
              <span class="col-xs-12 col-sm-3 task-instance-modal-column">
                <button type="submit" id="btn_failed" class="btn btn-primary btn-block">
                  Mark Failed
                </button>
              </span>
            </div>
          </form>
          <hr style="margin-bottom: 8px;">
          <form method="POST" data-action="{{ url_for('Airflow.success') }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="dag_id" value="{{ dag.dag_id }}">
            <input type="hidden" name="task_id">
            <input type="hidden" name="execution_date">
            <input type="hidden" name="origin" value="{{ request.base_url }}">
            <div class="row">
              <span class="btn-group col-xs-12 col-sm-9 task-instance-modal-column" data-toggle="buttons">
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="success_past" autocomplete="off">
                  Past
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="success_future" autocomplete="off">
                  Future
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="success_upstream" autocomplete="off">
                  Upstream
                </label>
                <label class="btn btn-default">
                  <input type="checkbox" value="true" name="success_downstream" autocomplete="off">
                  Downstream
                </label>
              </span>
              <span class="col-xs-12 col-sm-3 task-instance-modal-column">
                <button type="submit" id="btn_success" class="btn btn-primary btn-block">
                  Mark Success
                </button>
              </span>
            </div>
          </form>
          <hr>
          <span class="btn-group" id="extra_links"></span>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal for dag -->
  <div class="modal fade" id="dagModal" tabindex="-1" role="dialog" aria-labelledby="dagModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="dagModalLabel">
            <span class="text-muted">{{ 'SUBDAG' if dag.parent_dag is defined and dag.parent_dag else 'DAG' }}:</span> {{dag.dag_id}}
          </h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <span class="btn-group col-md-8">
              <form method="POST">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <input type="hidden" name="dag_id" value="{{ dag.dag_id }}">
                <input type="hidden" name="execution_date">
                <input type="hidden" name="origin" value="{{ request.base_url }}">
                <button type="button" id="btn_dagrun_clear" class="btn btn-primary" data-action="{{ url_for('Airflow.dagrun_clear') }}">
                  Clear
                </button>
                <button type="button" id="btn_dagrun_failed" class="btn btn-primary" data-action="{{ url_for('Airflow.dagrun_failed') }}">
                  Mark Failed
                </button>
                <button type="button" id="btn_dagrun_success" class="btn btn-primary" data-action="{{ url_for('Airflow.dagrun_success') }}">
                  Mark Success
                </button>
              </form>
            </span>
            <span class="col-md-4 text-right">
              <a id="btn_dag_graph_view" class="btn" data-base-url="{{ url_for('Airflow.graph') }}" role="button">
                <span class="material-icons" aria-hidden="true">account_tree</span>
                Graph View
              </a>
            </span>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal for event -->
  <div class="modal fade" id="eventModal" tabindex="-1" role="dialog" aria-labelledby="eventModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="eventModalLabel">
            <span class="text-muted">Event:</span>  <span id="event_id"></span>
          </h4>
        </div>
        <div class="modal-body">
          <h4>Send Events</h4>
          <div class="table-responsive">
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th>Sender</th>
                  <th>Count</th>
                </tr>
              </thead>
              <tbody id="eventTbody">
              </tbody>
            </table>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
{% endblock %}
{% block tail %}
  {{ super() }}
  <script>
    function updateQueryStringParameter(uri, key, value) {
      var re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
      var separator = uri.indexOf('?') !== -1 ? '&' : '?';
      if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
      }
      else {
        return uri + separator + key + '=' + value;
      }
    }

    // Pills highlighting
    $(document).ready(function () {
      $('a[href*="' + this.location.pathname + '"]').parent().addClass('active');
      $('.never_active').removeClass('active');
    });

    var id = '';
    var dag_id = '{{ dag.dag_id }}';
    var dagTZ = '{{ dag.timezone.name }}'; // Being used in datetime-utils.js
    var task_id = '';
    var execution_date = '';
    var subdag_id = '';
    var show_external_log_redirect = false;
    {% if show_external_log_redirect is defined %}
      show_external_log_redirect = '{{ show_external_log_redirect }}' == 'True';
    {% endif %}

    var buttons = Array.from(document.querySelectorAll('a[id^="btn_"][data-base-url]')).reduce(function(obj, elm) {
      obj[elm.id.replace('btn_', '')] = elm;
      return obj;
    }, {});

    // Update modal urls on toggle
    document.addEventListener('click', function(event) {
      if (event.target.matches('button[data-toggle="button"]')) {
        updateModalUrls();
      }
    });

    function updateButtonUrl(elm, params) {
      elm.setAttribute('href', elm.dataset.baseUrl + '?' + $.param(params));
    }

    function updateModalUrls() {
      updateButtonUrl(buttons.subdag, {
        dag_id: subdag_id,
        execution_date: execution_date,
      });

      updateButtonUrl(buttons.task, {
        dag_id: dag_id,
        task_id: task_id,
        execution_date: execution_date,
      });

      updateButtonUrl(buttons.rendered, {
        dag_id: dag_id,
        task_id: task_id,
        execution_date: execution_date,
      });

      if (buttons.rendered_k8s) {
        updateButtonUrl(buttons.rendered_k8s, {
          dag_id: dag_id,
          task_id: task_id,
          execution_date: execution_date,
        });
      }

      updateButtonUrl(buttons.ti, {
        flt1_dag_id_equals: dag_id,
        _flt_3_task_id: task_id,
        _oc_TaskInstanceModelView: execution_date,
      });

      updateButtonUrl(buttons.te, {
        flt1_dag_id_equals: dag_id,
        _flt_3_task_id: task_id,
        _oc_TaskExecutionModelView: execution_date,
      });

      updateButtonUrl(buttons.log, {
        dag_id: dag_id,
        task_id: task_id,
        execution_date: execution_date,
      });
    }

    function call_modal(t, d, extra_links, try_numbers, sd) {
      task_id = t;
      loc = String(window.location);
      $('#btn_filter').on('click', function(){
        window.location = updateQueryStringParameter(loc, 'root', task_id);
      });
      subdag_id = sd;
      execution_date = d;
      $('#task_id').text(t);
      $('#execution_date').text(d);
      $('#taskInstanceModal').modal({});
      $('#taskInstanceModal').css('margin-top','0');
      $('#extra_links').prev('hr').hide();
      $('#extra_links').empty().hide();
      if (subdag_id === undefined)
          $('#div_btn_subdag').hide();
      else {
          $('#div_btn_subdag').show();
          subdag_id = '{{ dag.dag_id }}.' + t;
      }

      $('#dag_dl_logs').hide();
      $('#dag_redir_logs').hide();
      if (try_numbers > 0) {
          $('#dag_dl_logs').show();
          if (show_external_log_redirect) {
              $('#dag_redir_logs').show();
          }
      }

      updateModalUrls();

      $('#try_index > li').remove();
      $('#redir_log_try_index > li').remove();
      var startIndex = (try_numbers > 2 ? 0 : 1)
      for (var index = startIndex; index < try_numbers; index++) {
        var url = '{{ url_for('Airflow.get_logs_with_metadata') }}' +
          '?dag_id=' + encodeURIComponent(dag_id) +
          '&task_id=' + encodeURIComponent(task_id) +
          '&execution_date=' + encodeURIComponent(execution_date) +
          '&metadata=null' +
          '&format=file';

        var showLabel = index;
        if (index != 0) {
          url += '&try_number=' + index;
        } else {
          showLabel = 'All';
        }

        $('#try_index').append(`<li role="presentation" style="display:inline">
          <a href="${url}"> ${showLabel} </a>
          </li>`
        );

        if (index == 0 || !show_external_log_redirect) continue;
        var redir_log_url = '{{ url_for('Airflow.redirect_to_external_log') }}' +
          '?dag_id=' + encodeURIComponent(dag_id) +
          '&task_id=' + encodeURIComponent(task_id) +
          '&execution_date=' + encodeURIComponent(execution_date) +
          '&try_number=' + index;
        $('#redir_log_try_index').append(`<li role="presentation" style="display:inline">
          <a href="${redir_log_url}"> ${showLabel} </a>
          </li>`
        );
      }

      if (extra_links && extra_links.length > 0){
        var markupArr = [];
        extra_links.sort();
        $.each(extra_links, function(i, link){
          var url = '{{ url_for('Airflow.extra_links') }}' +
                  '?task_id=' + encodeURIComponent(task_id) +
                  '&dag_id=' + encodeURIComponent(dag_id) +
                  '&execution_date=' + encodeURIComponent(execution_date) +
                  '&link_name=' + encodeURIComponent(link);
          var external_link = $('<a href="#" class="btn btn-primary disabled" target="_blank"></a>')
          var link_tooltip = $('<span class="tool-tip" data-toggle="tooltip" style="padding-right: 2px; padding-left: 3px" data-placement="top" ' +
            'title="link not yet available"></span>');
          link_tooltip.append(external_link)
          external_link.text(link);

          $.ajax(
            {url: url,
             cache: false,
             success: function (data, textStatus, jqXHR) {
               external_link.attr('href', data['url']);
               external_link.removeClass('disabled');
               link_tooltip.tooltip('disable');
             },
             error:  function (data) {
               link_tooltip.tooltip('hide').attr('title', data['responseJSON']['error']).tooltip('fixTitle');
             }
           });

          markupArr.push(link_tooltip)
        });

        var extra_links_span = $('#extra_links');
        extra_links_span.prev('hr').show();
        extra_links_span.append(markupArr).show();
        extra_links_span.find('[data-toggle="tooltip"]').tooltip();
      }
    }

    function call_modal_dag(dag) {
      execution_date = dag && dag.execution_date;
      dag_id = dag && dag.dag_id
      $('#dagModal').modal({});
      $('#dagModal').css('margin-top','0');
      updateButtonUrl(buttons.dag_graph_view, {
        dag_id: dag_id,
        execution_date: execution_date,
        });
    }

    function call_modal_event(event_id, event) {
      $('#eventTbody').empty();
      var url = '{{ url_for('Airflow.event_senders') }}' +
                  '?event_namespace=' + event[0] +
                  '&event_key=' + event[1] +
                  '&event_type=' + event[2];
      $.ajax(
            {url: url,
             cache: false,
             success: function (data, textStatus, jqXHR) {
              var event_senders=JSON.parse(data)['event_senders']
              console.info(event_senders);
              for(var event_sender in event_senders){
                var sender_url = '{{ url_for('EventModelView.list') }}' +
                    '?_flt_3_namespace=' + event[0] +
                    '&_flt_3_key=' + event[1] +
                    '&_flt_3_event_type=' + event[2] +
                    '&_flt_3_sender=' + event_sender;
                $('#eventTbody').append('<tr><td><a href=' + sender_url + '>' + event_sender + '</a></td><td>' + event_senders[event_sender] + '</td></tr>');
              }
             }
      });
      $('#event_id').text(event_id)
      $('#eventModal').modal({});
      $('#eventModal').css('margin-top','0');
    }

    function confirmDeleteDag(link, dag_id){
      if (confirm("Are you sure you want to delete '"+dag_id+"' now?\n\
        This option will delete ALL metadata, DAG runs, etc.\n\
        EXCEPT Log.\n\
        This cannot be undone.")) {
        postAsForm(link.href, {});
      }
      return false;
    }

    // Task Instance Modal actions
    $('form[data-action]').submit(function (e) {
      e.preventDefault();
      var form = $(this).get(0);
      form.execution_date.value = execution_date;
      form.origin.value = window.location;
      if (form.task_id) {
        form.task_id.value = task_id;
      }
      form.action = $(this).data('action');
      form.submit();
    })

    // DAG Modal actions
    $('form button[data-action]').click(function (e) {
      var form = $(this).closest('form').get(0);
      form.execution_date.value = execution_date;
      form.origin.value = window.location;
      if (form.task_id) {
        form.task_id.value = task_id;
      }
      form.action = $(this).data('action');
      form.submit();
    });

    $('#pause_resume').change(function() {
      var $input = $(this);
      var dagId = $input.data('dag-id');
      var isPaused = $input.is(':checked');
      var url = `{{ url_for('Airflow.paused') }}?is_paused=${isPaused}&dag_id=${encodeURIComponent(dagId)}`;
      $input.removeClass('switch-input--error');
      $.post(url).fail(function() {
        setTimeout(() => {
          $input.prop('checked', !isPaused);
          $input.addClass('switch-input--error');
        }, 500 );
      });
    });
  </script>
{% endblock %}
