Commit 045c5cd populate dropdown-menu when button is clicked, not upon page load

1 file Authored and Committed by karsten 6 days ago
populate dropdown-menu when button is clicked, not upon page load

fixes #3599
every page related to a git repository has a pull-request button that
opens a dropdown-menu. This menu is populated when the page loads, even
when creating a PR might not be the intention of visiting that page.

Signed-off-by: Karsten Hopp <karsten@redhat.com>

    
  1 @@ -95,7 +95,7 @@
  2               <div class="btn-group">
  3                 <a href="#" class="btn btn-outline-primary btn-sm dropdown-toggle"
  4                   data-toggle="dropdown"
  5 -                 aria-haspopup="true" aria-expanded="false">
  6 +                 aria-haspopup="true" aria-expanded="false" id="pr-button">
  7                   <i class="fa fa-arrow-circle-down fa-fw"></i> Open PR
  8                 </a>
  9                 <div class="dropdown-menu dropdown-menu-right" id="PR-dropdown">
 10 @@ -412,99 +412,103 @@
 11   {% if g.authenticated and not g.repo_obj.is_empty %}
 12   
 13   {% if g.repo_committer %}
 14 - $(function() {
 15 -   $.ajax({
 16 -     url: '{{ url_for("internal_ns.get_pull_request_ready_branch") }}' ,
 17 -     type: 'POST',
 18 -     data: {
 19 -       repo: "{{ repo.name }}",
 20 -       namespace: "{{ repo.namespace if repo.namespace }}",
 21 -       repouser: "",
 22 -       csrf_token: "{{ g.confirmationform.csrf_token.current_token }}",
 23 -     },
 24 -     dataType: 'json',
 25 -     error: function(res) {
 26 -        console.log(res);
 27 -     },
 28 -     success: function(res) {
 29 -       if (res.code == 'OK'){
 30 -         var first_item = true;
 31 -         for (branch in res.message.new_branch){
 32 -           if (first_item){
 33 -             $("#PR-dropdown").prepend("<div class='dropdown-divider'></div>")
 34 -             first_item = false;
 35 + $("#pr-button").click(
 36 +   function() {
 37 +     $.ajax({
 38 +       url: '{{ url_for("internal_ns.get_pull_request_ready_branch") }}' ,
 39 +       type: 'POST',
 40 +       data: {
 41 +         repo: "{{ repo.name }}",
 42 +         namespace: "{{ repo.namespace if repo.namespace }}",
 43 +         repouser: "",
 44 +         csrf_token: "{{ g.confirmationform.csrf_token.current_token }}",
 45 +       },
 46 +       dataType: 'json',
 47 +       error: function(res) {
 48 +          console.log(res);
 49 +       },
 50 +       success: function(res) {
 51 +         if (res.code == 'OK'){
 52 +           var first_item = true;
 53 +           for (branch in res.message.new_branch){
 54 +             if (first_item){
 55 +               $("#PR-dropdown").prepend("<div class='dropdown-divider'></div>")
 56 +               first_item = false;
 57 +             }
 58 +             var url = "{{ url_for(
 59 +               'ui_ns.new_request_pull',
 60 +               repo=repo.name,
 61 +               username=repo.user.user if repo.is_fork else None,
 62 +               namespace=repo.namespace,
 63 +               branch_to='-',
 64 +               branch_from='-') }}";
 65 +             url = url.slice(0, -4) + res.message.new_branch[branch]['target_branch'] + '..' + branch;
 66 +             html = '<a class="dropdown-item" href="' + url + '">'
 67 +               + '<span class="badge badge-light badge-pill border border-secondary font-size-1">'
 68 +                 + ' <i class="fa fa-calendar-o fa-rotate-270 fa-fw"></i> '
 69 +                 + '{{ repo.name }}'
 70 +               + '</span> '
 71 +               + '<span class="badge badge-secondary border border-secondary badge-pill font-size-1">'
 72 +                 + '<span class="fa fa-random fa-fw"> </span> '
 73 +                 + branch
 74 +               + '</span></a> ';
 75 +             $("#PR-dropdown").prepend(html);
 76             }
 77 -           var url = "{{ url_for(
 78 -             'ui_ns.new_request_pull',
 79 -             repo=repo.name,
 80 -             username=repo.user.user if repo.is_fork else None,
 81 -             namespace=repo.namespace,
 82 -             branch_to='-',
 83 -             branch_from='-') }}";
 84 -           url = url.slice(0, -4) + res.message.new_branch[branch]['target_branch'] + '..' + branch;
 85 -           html = '<a class="dropdown-item" href="' + url + '">'
 86 -             + '<span class="badge badge-light badge-pill border border-secondary font-size-1">'
 87 -               + ' <i class="fa fa-calendar-o fa-rotate-270 fa-fw"></i> '
 88 -               + '{{ repo.name }}'
 89 -             + '</span> '
 90 -             + '<span class="badge badge-secondary border border-secondary badge-pill font-size-1">'
 91 -               + '<span class="fa fa-random fa-fw"> </span> '
 92 -               + branch
 93 -             + '</span></a> ';
 94 -           $("#PR-dropdown").prepend(html);
 95           }
 96         }
 97 -     }
 98 -   });
 99 - });
100 +     });
101 +   }
102 + );
103   {% endif %}
104   
105 - $(function() {
106 -   $.ajax({
107 -     url: '{{ url_for("internal_ns.get_pull_request_ready_branch") }}' ,
108 -     type: 'POST',
109 -     data: {
110 -       namespace: "{{ repo.namespace if repo.namespace }}",
111 -       repo: "{{ repo.name }}",
112 -       repouser: "{{ g.fas_user.username }}",
113 -       csrf_token: "{{ g.confirmationform.csrf_token.current_token }}",
114 -     },
115 -     dataType: 'json',
116 -     error: function(res) {
117 -       $('#spinnergif').hide()
118 -       console.log(res);
119 -     },
120 -     success: function(res) {
121 -       $('#spinnergif').hide()
122 -       console.log("done");
123 -       if (res.code == 'OK'){
124 -         var first_item = true;
125 -         for (branch in res.message.new_branch){
126 -           if (first_item){
127 -             $("#PR-dropdown").prepend("<div class='dropdown-divider'></div>");
128 -             first_item = false;
129 + $("#pr-button").click(
130 +   function() {
131 +     $.ajax({
132 +       url: '{{ url_for("internal_ns.get_pull_request_ready_branch") }}' ,
133 +       type: 'POST',
134 +       data: {
135 +         namespace: "{{ repo.namespace if repo.namespace }}",
136 +         repo: "{{ repo.name }}",
137 +         repouser: "{{ g.fas_user.username }}",
138 +         csrf_token: "{{ g.confirmationform.csrf_token.current_token }}",
139 +       },
140 +       dataType: 'json',
141 +       error: function(res) {
142 +         $('#spinnergif').hide()
143 +         console.log(res);
144 +       },
145 +       success: function(res) {
146 +         $('#spinnergif').hide()
147 +         console.log("done");
148 +         if (res.code == 'OK'){
149 +           var first_item = true;
150 +           for (branch in res.message.new_branch){
151 +             if (first_item){
152 +               $("#PR-dropdown").prepend("<div class='dropdown-divider'></div>");
153 +               first_item = false;
154 +             }
155 +             var url = "{{ url_for(
156 +               'ui_ns.new_request_pull',
157 +               repo=repo.name,
158 +               username=g.fas_user.username,
159 +               namespace=repo.namespace,
160 +               branch_to='-',
161 +               branch_from='-') }}";
162 +             url = url.slice(0, -4) + res.message.new_branch[branch]['target_branch'] + '..' + branch;
163 +             html = '<a class="dropdown-item" \
164 +             href="' + url + '"><span class="badge badge-light badge-pill border border-secondary font-size-1">'
165 +             + '<i class="fa fa-code-fork fa-fw"></i>'
166 +             +'{{ g.fas_user.username }}/{{ repo.name }}</span> '
167 +             +'<span class="badge badge-secondary border border-secondary badge-pill font-size-1">'
168 +             +'<i class="fa fa-random"></i> '
169 +             + branch + '</span></a> ';
170 +             $("#PR-dropdown").prepend(html);
171             }
172 -           var url = "{{ url_for(
173 -             'ui_ns.new_request_pull',
174 -             repo=repo.name,
175 -             username=g.fas_user.username,
176 -             namespace=repo.namespace,
177 -             branch_to='-',
178 -             branch_from='-') }}";
179 -           url = url.slice(0, -4) + res.message.new_branch[branch]['target_branch'] + '..' + branch;
180 -           html = '<a class="dropdown-item" \
181 -           href="' + url + '"><span class="badge badge-light badge-pill border border-secondary font-size-1">'
182 -           + '<i class="fa fa-code-fork fa-fw"></i>'
183 -           +'{{ g.fas_user.username }}/{{ repo.name }}</span> '
184 -           +'<span class="badge badge-secondary border border-secondary badge-pill font-size-1">'
185 -           +'<i class="fa fa-random"></i> '
186 -           + branch + '</span></a> ';
187 -           $("#PR-dropdown").prepend(html);
188           }
189         }
190 -     }
191 -   });
192 - });
193 +     })
194 +   }
195 + );
196   
197   {% endif %}
198   </script>