~ubuntu-branches/ubuntu/saucy/horizon/saucy-updates

« back to all changes in this revision

Viewing changes to horizon/static/horizon/js/horizon.firewalls.js

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Chuck Short
  • Date: 2013-10-03 13:48:12 UTC
  • mfrom: (1.1.31)
  • Revision ID: package-import@ubuntu.com-20131003134812-2vkwosem4flwuk5y
Tags: 1:2013.2~rc1-0ubuntu1
[ James Page ]
* New upstream release candidate:
  - d/static: Refreshed static assets for 2013.2~rc1.
  - d/patches: Refreshed patches.

[ Chuck Short ]
* debian/control: Add python-lesscpy as a suggests to optionally
  support online compression of static assets (LP: #1226674).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
horizon.firewalls = {
 
2
  user_decided_length: false,
 
3
  rules_selected: [],
 
4
  rules_available: [],
 
5
 
 
6
  getConsoleLog: function(via_user_submit) {
 
7
    var form_element = $("#tail_length"),
 
8
        data;
 
9
 
 
10
    if (!via_user_submit) {
 
11
      via_user_submit = false;
 
12
    }
 
13
 
 
14
    if(this.user_decided_length) {
 
15
      data = $(form_element).serialize();
 
16
    } else {
 
17
      data = "length=35";
 
18
    }
 
19
 
 
20
    $.ajax({
 
21
      url: $(form_element).attr('action'),
 
22
      data: data,
 
23
      method: 'get',
 
24
      success: function(response_body) {
 
25
        $('pre.logs').text(response_body);
 
26
      },
 
27
      error: function(response) {
 
28
        if(via_user_submit) {
 
29
          horizon.clearErrorMessages();
 
30
          horizon.alert('error', gettext('There was a problem communicating with the server, please try again.'));
 
31
        }
 
32
      }
 
33
    });
 
34
  },
 
35
 
 
36
  /*
 
37
   * Gets the html select element associated with a given
 
38
   * rule id for rule_id.
 
39
   **/
 
40
  get_rule_element: function(rule_id) {
 
41
      return $('li > label[for^="id_rule_' + rule_id + '"]');
 
42
  },
 
43
 
 
44
  /*
 
45
   * Initializes an associative array of lists of the current
 
46
   * rules.
 
47
   **/
 
48
  init_rule_list: function() {
 
49
    horizon.firewalls.rules_selected = [];
 
50
    horizon.firewalls.rules_available = [];
 
51
    $(this.get_rule_element("")).each(function(){
 
52
      var $this = $(this);
 
53
      var $input = $this.children("input");
 
54
      var rule_property = {
 
55
        name:$this.text().replace(/^\s+/,""),
 
56
        id:$input.attr("id"),
 
57
        value:$input.attr("value")
 
58
      };
 
59
      if($input.is(':checked')) {
 
60
        horizon.firewalls.rules_selected.push(rule_property);
 
61
      } else {
 
62
        horizon.firewalls.rules_available.push(rule_property);
 
63
      }
 
64
    });
 
65
  },
 
66
 
 
67
  /*
 
68
   * Generates the HTML structure for a rule that will be displayed
 
69
   * as a list item in the rule list.
 
70
   **/
 
71
  generate_rule_element: function(name, id, value) {
 
72
    var $li = $('<li>');
 
73
    $li.attr('name', value).html(name + '<em class="rule_id">(' + value + ')</em><a href="#" class="btn btn-primary"></a>');
 
74
    return $li;
 
75
  },
 
76
 
 
77
  /*
 
78
   * Generates the HTML structure for the Rule List.
 
79
   **/
 
80
  generate_rulelist_html: function() {
 
81
    var self = this;
 
82
    var updateForm = function() {
 
83
      var lists = $("#ruleListId div.input li").attr('data-index',100);
 
84
      var active_rules = $("#selected_rule > li").map(function(){
 
85
        return $(this).attr("name");
 
86
      });
 
87
      $("#ruleListId div.input input:checkbox").removeAttr('checked');
 
88
      active_rules.each(function(index, value){
 
89
        $("#ruleListId div.input input:checkbox[value=" + value + "]")
 
90
        .attr('checked','checked')
 
91
        .parents("li").attr('data-index',index);
 
92
      });
 
93
      $("#ruleListId div.input ul").html(
 
94
        lists.sort(function(a,b){
 
95
          if( $(a).data("index") < $(b).data("index")) return -1;
 
96
          if( $(a).data("index") > $(b).data("index")) return 1;
 
97
          return 0;
 
98
        })
 
99
      );
 
100
    };
 
101
    $("#ruleListSortContainer").show();
 
102
    $("#ruleListIdContainer").hide();
 
103
    self.init_rule_list();
 
104
    // Make sure we don't duplicate the rules in the list
 
105
    $("#available_rule").empty();
 
106
    $.each(self.rules_available, function(index, value){
 
107
      $("#available_rule").append(self.generate_rule_element(value.name, value.id, value.value));
 
108
    });
 
109
    // Make sure we don't duplicate the rules in the list
 
110
    $("#selected_rule").empty();
 
111
    $.each(self.rules_selected, function(index, value){
 
112
      $("#selected_rule").append(self.generate_rule_element(value.name, value.id, value.value));
 
113
    });
 
114
    $(".rulelist > li > a.btn").click(function(e){
 
115
      var $this = $(this);
 
116
      e.preventDefault();
 
117
      e.stopPropagation();
 
118
      if($this.parents("ul#available_rule").length > 0) {
 
119
        $this.parent().appendTo($("#selected_rule"));
 
120
      } else if ($this.parents("ul#selected_rule").length > 0) {
 
121
        $this.parent().appendTo($("#available_rule"));
 
122
      }
 
123
      updateForm();
 
124
    });
 
125
    if ($("#ruleListId > div.control-group.error").length > 0) {
 
126
      var errortext = $("#ruleListId > div.control-group.error").find("span.help-inline").text();
 
127
      $("#selected_rule_h4").before($('<div class="dynamic-error">').html(errortext));
 
128
    }
 
129
    $(".rulelist").sortable({
 
130
        connectWith: "ul.rulelist",
 
131
        placeholder: "ui-state-highlight",
 
132
        distance: 5,
 
133
        start:function(e,info){
 
134
          $("#selected_rule").addClass("dragging");
 
135
        },
 
136
        stop:function(e,info){
 
137
          $("#selected_rule").removeClass("dragging");
 
138
          updateForm();
 
139
        }
 
140
    }).disableSelection();
 
141
  },
 
142
 
 
143
  workflow_init: function(modal) {
 
144
    // Initialise the drag and drop rule list
 
145
    horizon.firewalls.generate_rulelist_html();
 
146
  }
 
147
};
 
148
 
 
149
horizon.addInitFunction(function () {
 
150
  $(document).on('submit', '#tail_length', function (evt) {
 
151
    horizon.firewalls.user_decided_length = true;
 
152
    horizon.firewalls.getConsoleLog(true);
 
153
    evt.preventDefault();
 
154
  });
 
155
});