summaryrefslogtreecommitdiff
path: root/horizon/static/horizon/js/horizon.lists.js
blob: 3782be3272978ea69b498355686cbc6ee808494d (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
horizon.lists = {

  /*
   * Generates the HTML structure for a "+type+" that will be displayed
   * as a list item.
   **/
  generate_element: function (name, value, type) {
    var $li = $('<li>');
    $li.attr('name', value).html(
      name + '<em class="' + type + '_id">(' + value + ')</em>' +
      '<a href="#" class="btn btn-primary"></a>'
    );
    return $li;
  },

  /*
   * Generates the HTML structure for the feature list.
   **/
  generate_html: function (type) {
    var self = this;
    self.feature_type = type;

    var update_form = function () {
      var lists = $("#"+type+"ListId li").attr('data-index',100);
      var active_features = $("#selected_"+type+" > li").map(function () {
        return $(this).attr("name");
      });
      $("#"+type+"ListId input:checkbox").prop('checked', false);
      active_features.each(function (index, value) {
        $("#"+type+"ListId input:checkbox[value=" + value + "]")
          .prop('checked', true)
          .parents("li").attr('data-index',index);
      });
      $("#"+type+"ListId ul").html(
        lists.sort(function (a,b) {
          if ($(a).data("index") < $(b).data("index")) { return -1; }
          if ($(a).data("index") > $(b).data("index")) { return 1; }
          return 0;
        })
      );
    };

    var append_new_elements = function (type, state) {
      $("#"+state+"_"+type).empty();
      $.each(self[type+"s_"+state], function (index, value) {
        $("#"+state+"_"+self.feature_type).append(
          self.generate_element(value.name, value.value, self.feature_type)
        );
      });
    };

    $("#"+type+"ListSortContainer").show();
    $("#"+type+"ListIdContainer").hide();
    self.init_list(type);
    append_new_elements(type, "available");
    append_new_elements(type, "selected");

    $("."+type+"list > li > a.btn").click(function (e) {
      var $this = $(this);
      e.preventDefault();
      e.stopPropagation();

      if ($this.parents("ul#available_"+type).length > 0) {
        $this.parent().appendTo($("#selected_"+type));
      } else if ($this.parents("ul#selected_"+type).length > 0) {
        $this.parent().appendTo($("#available_"+type));
      }
      update_form();
    });

    if ($("#"+type+"ListId > div.form-group.error").length > 0) {
      var errortext = $("#"+type+"ListId > div.form-group.error").find("span.help-block").text();
      $("#selected_"+type+"_label").before($('<div class="dynamic-error">').html(errortext));
    }

    $("."+type+"list").sortable({
      connectWith: "ul."+type+"list",
      placeholder: "ui-state-highlight",
      distance: 5,
      start:function () {
        $("#selected_"+type).addClass("dragging");
      },
      stop:function () {
        $("#selected_"+type).removeClass("dragging");
        update_form();
      }
    }).disableSelection();
  },

  get_console_log: function (via_user_submit, user_decided_length) {
    var form_element = $("#tail_length");
    var error_txt = gettext('There was a problem communicating with the server, please try again.');

    if (!via_user_submit) {
      via_user_submit = false;
    }

    $.ajax({
      url: $(form_element).attr('action'),
      data: (user_decided_length) ? $(form_element).serialize() : "length=35",
      method: 'get',
      success: function (response_body) {
        $('pre.logs').text(response_body);
      },
      error: function () {
        if (via_user_submit) {
          horizon.clearErrorMessages();
          horizon.toast.add('error', error_txt);
        }
      }
    });
  },

  /*
   * Gets the html select element associated with a given id.
   **/
  get_element: function (id, type) {
    return $('label[for^="id_'+type+'_' + id + '"]');
  },

  /*
   * Initializes an associative array of lists for the current feature.
   **/
  init_list: function (type) {
    var self = this;
    self[type+"s_selected"] = [];
    self[type+"s_available"] = [];

    $(horizon.lists.get_element("", type)).each(function () {
      var $parent = $(this).parent();
      var $input = $parent.children("input");
      var name = horizon.string.escapeHtml($parent.text().replace(/^\s+/, ""));
      var properties = {
        "name": name,
        "id": $input.attr("id"),
        "value": $input.attr("value")
      };

      var feature_state = ($input.is(":checked")) ? "selected" : "available";
      self[type+"s_"+feature_state].push(properties);
    });
  }

};