importer_status.js 2.83 KB
Newer Older
Fatih Acet's avatar
Fatih Acet committed
1 2 3 4 5 6 7 8 9 10 11 12
(function() {
  this.ImporterStatus = (function() {
    function ImporterStatus(jobs_url, import_url) {
      this.jobs_url = jobs_url;
      this.import_url = import_url;
      this.initStatusPage();
      this.setAutoUpdate();
    }

    ImporterStatus.prototype.initStatusPage = function() {
      $('.js-add-to-import').off('click').on('click', (function(_this) {
        return function(e) {
13
          var $btn, $namespace_input, $target_field, $tr, id, target_namespace;
Fatih Acet's avatar
Fatih Acet committed
14 15 16
          $btn = $(e.currentTarget);
          $tr = $btn.closest('tr');
          $target_field = $tr.find('.import-target');
17
          $namespace_input = $target_field.find('.js-select-namespace option:selected');
Fatih Acet's avatar
Fatih Acet committed
18
          id = $tr.attr('id').replace('repo_', '');
19
          target_namespace = null;
20
          new_name = null;
Fatih Acet's avatar
Fatih Acet committed
21
          if ($namespace_input.length > 0) {
22 23 24
            target_namespace = $namespace_input[0].innerHTML;
            new_name = $target_field.find('#path').prop('value');
            $target_field.empty().append(target_namespace + "/" + new_name);
Fatih Acet's avatar
Fatih Acet committed
25 26 27 28
          }
          $btn.disable().addClass('is-loading');
          return $.post(_this.import_url, {
            repo_id: id,
29 30
            target_namespace: target_namespace,
            new_name: new_name
Fatih Acet's avatar
Fatih Acet committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
          }, {
            dataType: 'script'
          });
        };
      })(this));
      return $('.js-import-all').off('click').on('click', function(e) {
        var $btn;
        $btn = $(this);
        $btn.disable().addClass('is-loading');
        return $('.js-add-to-import').each(function() {
          return $(this).trigger('click');
        });
      });
    };

    ImporterStatus.prototype.setAutoUpdate = function() {
      return setInterval(((function(_this) {
        return function() {
          return $.get(_this.jobs_url, function(data) {
            return $.each(data, function(i, job) {
              var job_item, status_field;
              job_item = $("#project_" + job.id);
              status_field = job_item.find(".job-status");
              if (job.import_status === 'finished') {
                job_item.removeClass("active").addClass("success");
                return status_field.html('<span><i class="fa fa-check"></i> done</span>');
              } else if (job.import_status === 'started') {
                return status_field.html("<i class='fa fa-spinner fa-spin'></i> started");
              } else {
                return status_field.html(job.import_status);
              }
            });
          });
        };
      })(this)), 4000);
    };

    return ImporterStatus;

  })();

72 73 74 75
  $(function() {
    if ($('.js-importer-status').length) {
      var jobsImportPath = $('.js-importer-status').data('jobs-import-path');
      var importPath = $('.js-importer-status').data('import-path');
76

77 78 79
      new ImporterStatus(jobsImportPath, importPath);
    }
  });
Fatih Acet's avatar
Fatih Acet committed
80
}).call(this);