Commit a4397d394e15d2c18fce19ecd9f972fd6d864368

Authored by Rafael Manzo
Committed by Heitor
1 parent 6791fa1b

Refatored Repository form JS fetch_branches into JS Branch class

app/assets/javascripts/repository/branch.js.coffee
... ... @@ -3,14 +3,14 @@ class Repository.Branch
3 3 @names = {}
4 4 @request = null
5 5  
6   - @toggle: ->
  6 + toggle: ->
7 7 scm_type_field = document.getElementById("repository_scm_type")
8 8 index = scm_type_field.selectedIndex
9 9 option = scm_type_field.options[index]
10 10  
11 11 if option.value != "SVN"
12 12 $("#branches").show()
13   - fetch_branches (document.getElementById("repository_address"))
  13 + @fetch_branches(document.getElementById("repository_address"))
14 14 else
15 15 $("#branches").hide()
16 16  
... ... @@ -27,3 +27,29 @@ class Repository.Branch
27 27 el.append($("<option></option>")
28 28 .attr("value", option)
29 29 .text(option))
  30 +
  31 + fetch_branches: (address_field) ->
  32 + @cancel_request()
  33 + address = address_field.value
  34 +
  35 + # Prevent a call with blank address
  36 + if address == ""
  37 + return
  38 +
  39 + el = $("#repository_branch")
  40 + el.empty() # remove old options
  41 +
  42 + if @names[address]?
  43 + @fill_options(@names[address], el)
  44 + return
  45 +
  46 + scm_type = $("#repository_scm_type option:selected").text()
  47 +
  48 + context = this
  49 + @request = $.get '/repository_branches',
  50 + {'url': address, 'scm_type': scm_type},
  51 + (data) ->
  52 + options = data["branches"]
  53 + if options != null
  54 + context.names[address] = options
  55 + context.fill_options(options, el)
... ...
app/views/repositories/_form.html.erb
... ... @@ -43,7 +43,7 @@
43 43 <div class="form-row">
44 44 <div class="field-container">
45 45 <%= f.label :scm_type, class: 'control-label' %>
46   - <%= f.select( :scm_type, @repository_types, {}, class: 'tooltip-control', onchange: "Repository.Branch.toggle();" ) %>
  46 + <%= f.select( :scm_type, @repository_types, {}, class: 'tooltip-control', onchange: "_repository_branch.toggle();" ) %>
47 47 </div>
48 48 <div class="help-container">
49 49 <p>
... ... @@ -55,7 +55,7 @@
55 55 <div class="form-row">
56 56 <div class="field-container">
57 57 <%= f.label :address, class: 'control-label' %>
58   - <%= f.text_field :address, :required => true, class: 'text-field form-control', onchange: "fetch_branches(this);" %>
  58 + <%= f.text_field :address, :required => true, class: 'text-field form-control', onchange: "_repository_branch.fetch_branches(this);" %>
59 59 </div>
60 60 <div class="help-container">
61 61 <p>
... ... @@ -109,41 +109,10 @@
109 109 <%= link_to t('back'), project_path(@project_id), class: 'btn btn-default' %>
110 110 </div>
111 111  
112   -<script>
  112 +<script type="text/javascript">
  113 + var _repository_branch = new Repository.Branch;
  114 +
113 115 $(document).on('page:load ready', function() {
114   - Repository.Branch.toggle();
  116 + _repository_branch.toggle();
115 117 });
116 118 </script>
117   -
118   -<script>
119   - var repository_branch = new Repository.Branch;
120   - function fetch_branches (address_field) {
121   - repository_branch.cancel_request();
122   - var address = address_field.value;
123   -
124   - var el = $("#repository_branch");
125   - el.empty(); // remove old options
126   -
127   - if(repository_branch.names[address] != null) {
128   - repository_branch.fill_options(repository_branch.names[address], el);
129   - return;
130   - }
131   -
132   - var scm_type = $("#repository_scm_type option:selected").text();
133   -
134   - repository_branch.request = ($.ajax({
135   - url: "<%= repository_branches_path() %>",
136   - data: {'url': address, 'scm_type': scm_type},
137   - type: 'GET',
138   - timeout: 30000,
139   - success: function (data){
140   - options = data["branches"];
141   - if (options != null) {
142   - repository_branch.names[address] = options;
143   - repository_branch.fill_options(options, el);
144   - }
145   - }
146   - }));
147   - }
148   -
149   -</script>
... ...