summaryrefslogtreecommitdiff
path: root/modules/local_import/js/local_import.js
blob: 2e84c0d61e99db3007479a44360ca3b688afa9ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function open_close_branch(icon, event) {
  var parent = icon.parentNode;
  var children = $(parent).find(".gCheckboxTree");
  var closed = $(icon).hasClass("ui-icon-plus");

  if (closed) {
    if (children.length == 0) {
      load_children(icon, function(data, textStatus) {
        $(parent).append(data);
        $(icon).addClass("ui-icon-minus");
        $(icon).removeClass("ui-icon-plus");
        var checkbox = $(parent).find(":checkbox")[0];
        checkbox_click(checkbox, null);
      });
    } else {
      $(icon).addClass("ui-icon-minus");
      $(icon).removeClass("ui-icon-plus");
      $(parent).children("ul").slideDown("fast");
    }
  } else {
    $(icon).addClass("ui-icon-plus");
    $(icon).removeClass("ui-icon-minus");
    $(parent).children("ul").slideUp("fast");
  }
}

function checkbox_click(checkbox, event) {
  var parents = $(checkbox).parents("li");
  var parent = parents.get(0);
  $(parent).find(".gCheckboxTree :checkbox").attr("checked", checkbox.checked);
  var checked = $("#gLocalImport :checkbox[checked]");
  $("#gLocalImport form :submit").attr("disabled", checked.length == 0);
}

function load_children(icon, callback) {
  var csrf = $("#gLocalImport form :hidden[name='csrf']")[0].value;
  var base_url = $("#gLocalImport form :hidden[name='base_url']")[0].value;
  var parms = "&csrf=" + csrf;
  var parents = $(icon).parents("li");
  for (i=parents.length - 1; i >= 0; i--) {
    parms += "&path[]=" +  $(parents[i]).children("span").attr("ref");
  }
  $.ajax({async: false,
          success: callback,
          data: parms,
          dataType: "html",
          type: "POST",
          url: base_url + "local_import/children"
  });
}

var current = 0;
var process_length = 0;
function do_import(submit, event) {
  event.preventDefault();
  $("#gProgressBar").progressbar('value', 0);
  $("#gProgressBar").css("visibility", "visible");
  var check_list = $("#gLocalImport :checkbox[checked]");
  process_length = check_list.length;
  current = 0;
  $.each(check_list, function () {
    process_checkbox(this);
  });
  document.location.reload();
  return false;
}

function process_checkbox(checkbox) {
  var parents = $(checkbox).parents("li");
  var csrf = $("#gLocalImport form :hidden[name='csrf']")[0].value;
  var parms = "&csrf=" + csrf;
  for (i=parents.length - 1; i > 0; i--) {
    parms += "&path[]=" +  $(parents[i]).children("span").attr("ref");
  }
  parms += "&path[]=" + $(checkbox).val();

  var parent = parents[0];
  if ($(parent).hasClass("gFile")) {
    process_file(parents[0], parms);
  } else if ($(parent).hasClass("gDirectory") && $(parents[0]).find(".gCheckboxTree").length == 0) {
    // If it is a directory and retrieve the children and process them
    var icon = $(parent).children("span")[0];
    load_children(icon, function(data, textStatus) {
      $(parent).append(data);
      $(icon).addClass("ui-icon-plus");
      checkbox_click(checkbox, null);
      var boxes = $(parent).find(".gCheckboxTree :checkbox[checked]");
      process_length += boxes.length;
      $.each(boxes, function () {
        process_checkbox(this);
      });
    });
    current++;
    $("#gProgressBar").progressbar('value', current / process_length * 100);
  }
}

function process_file(li_element, parms) {
  var url = $("#gLocalImport form").attr("action");
  $.post(url, parms, function(data, status) {
  });
  current++;
  $("#gProgressBar").progressbar('value', current / process_length * 100);
}