Statistics
| Branch: | Tag: | Revision:

root / ui / templates / networks.html @ db269618

History | View | Annotate | Download (39.8 kB)

1
<!--
2
Copyright 2011 GRNET S.A. All rights reserved.
3

4
Redistribution and use in source and binary forms, with or
5
without modification, are permitted provided that the following
6
conditions are met:
7

8
  1. Redistributions of source code must retain the above
9
     copyright notice, this list of conditions and the following
10
     disclaimer.
11

12
  2. Redistributions in binary form must reproduce the above
13
     copyright notice, this list of conditions and the following
14
     disclaimer in the documentation and/or other materials
15
     provided with the distribution.
16

17
THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
POSSIBILITY OF SUCH DAMAGE.
29

30
The views and conclusions contained in the software and
31
documentation are those of the authors and should not be
32
interpreted as representing official policies, either expressed
33
or implied, of GRNET S.A.
34
-->
35

    
36
{% load i18n %}
37

    
38
<!-- the create button -->
39
<div id="networks-createcontainer">
40
    <div id="beforecreate"></div>
41
    <a id="networkscreate" rel="#networks-wizard" href="#">{% trans "Create New +" %}</a>
42
</div>
43

    
44
<!-- add new network overlay -->
45
<div>
46
    <div class="modal" id="networks-wizard">
47
        <div class="header">
48
        </div>
49
        <h2>{% trans "Name your network" %}</h2>
50
        <hr class="topruler" />
51
        <div class="container">
52
            <div class="name-input">
53
                <label>{% trans "Name" %}:</label>
54
                <input type="text" class="text" name="machine_name"></input>
55
                <span class="help">{% trans "(* Required field)" %}</span>
56
            </div>
57
        </div>
58
        <hr class="bottomruler" />
59
        <button type="button" id="add-network-cancel" class="cancel">{% trans "Cancel" %}</button>
60
        <button type="button" id="add-network-create" class="create">{% trans "Create Network" %}</button>
61
        <hr class="separator-end"></hr>
62
    </div>
63
</div>
64

    
65
<!-- networks list -->
66
<div id="networks-container">
67
    <!-- spinner while loading list -->
68
    <div class = "large-spinner"></div>
69

    
70
    <!-- public network template -->
71
    <div class="network" id="public-template">
72
        <img class="network-logos" src="static/internet.png" />
73
        <div href="#" class="name-div">
74
            <h5 class="namecontainer">
75
                <span class="name">{% trans "Internet" %}</span>
76
            </h5>
77
        </div>
78
        <h5 class="network-machines">
79
            {% trans "Show:" %} <span class="show-machines">{% trans "machines" %}</span>
80
            (<span class="machines-count">0</span>)
81
        </h5>
82
        <div class="network-contents">
83
            <div class="network-placeholder">
84
                <div class="network-contents-start-separator"></div>
85
                <div class="machines-list">
86
                <!-- append machines here -->
87
                </div>
88
                <div class="empty-network-slot" id="machine-template">
89
                    <div class='network-add-machine'>
90
                        {# Replace the following with the comment below for full public network functionality #}
91
                        {# <span class="add-icon">+</span> #}
92
                        <span>&nbsp;</span>
93
                    </div>
94
                </div>
95
            </div>
96
            <div class="network-contents-end-separator"></div>
97
        </div>
98
        <div class="state">
99
            <div class="status">{% trans "Public Network" %}</div>
100
            <div class="indicators network-indicator">
101
                <div class="indicator1"></div>
102
                <div class="indicator2"></div>
103
                <div class="indicator3"></div>
104
                <div class="indicator4"></div>
105
            </div>
106
        </div>
107
        <div class="actions">
108
            {# Commented out the following until there is full public network support #}
109
            {# <a href="#" class="action-add">{% trans "Add Machine" %}</a> #}
110
        </div>
111
        <div class="confirm_single">
112
            <button class="yes">{% trans "Confirm" %}</button>
113
            <button class="no">{% trans "Cancel" %}</button>
114
        </div>
115
        <div class="action_error" align="center">
116
            {% trans "<span>Error</span> on" %} <span class="action">{% trans "error action" %}</span>
117
            <span class="code"></span>
118
            <span class="message"></span>
119
            <button class="details">{% trans "Details" %}</button>
120
        </div>
121
    </div>
122

    
123
    <!-- template for machines in public network -->
124
    <div class="network-machine" id="public-machine-template">
125
        <div class="machine-actions">
126
            <a href="#" class="action-details">{% trans "Details" %}</a>
127
        </div>
128
        <div class="confirm_single">
129
            <button class="yes">{% trans "Confirm" %}</button>
130
            <button class="no">{% trans "Cancel" %}</button>
131
        </div>
132
        <div class="action_error" align="center">
133
            {% trans "<span>Error</span> on" %} <span class="action">{% trans "error action" %}</span>
134
            <span class="code"></span>
135
            <span class="message"></span>
136
            <button class="details">{% trans "Details" %}</button>
137
        </div>
138
        <div class="ips">
139
            <div class="ip4-container status">
140
                <span class="discreet">{% trans "IPv4" %}: </span>
141
                <span class="ip4">192.94.73.15</span>
142
            </div>
143
            <div class="ip6-container status">
144
                <span class="discreet">{% trans "IPv6" %}: </span>
145
                <span class="ip6">vv</span>
146
            </div>
147
            <img class="spinner" style="display:none" src="static/icons/indicators/medium/progress.gif" />
148
            <img class="wave" style="display:none" src="static/icons/indicators/medium/wave.gif" />
149
        </div>
150
        <img class="logo" src="static/icons/machines/medium/debian-on.png" />
151
        <div class='network-remove-machine'>
152
            {# Replace the following with the comment below for full public network functionality #}
153
            {# <span class="remove-icon">X</span> #}
154
            <span>&nbsp;</span>
155
        </div>
156
        <div href="#" class="machine-name-div">
157
            <h5 class="namecontainer">
158
                <span class="name">my desktop1</span>
159
            </h5>
160
        </div>
161
        <h5 class="machine-settings">
162
            <span class="ip">{% trans "IPs" %}</span> |
163
            <span class="show-firewall">{% trans "Firewall" %}</span>
164
            (<span class="firewall-off">{% trans "Off" %}</span>)
165
        </h5>
166
        <div class="firewall-content">
167
            <div class="firewall-contents-start-separator"></div>
168
            <input type="radio" class="checkboxes" name="image-id1" />
169
            <span class="checkbox-legends">{% trans "Unprotected mode (Firewall off)" %}</span>
170
            <br />
171
            <input type="radio" class="checkboxes" name="image-id1" />
172
            <span class="checkbox-legends">{% trans "Fully protected mode (Firewall on)" %}</span>
173
            <br />
174
            <input type="radio" class="checkboxes" name="image-id1" />
175
            <span class="checkbox-legends">{% trans "Basically protected mode (Firewall on)" %}</span>
176
            <br />
177
            <input type="radio" class="checkboxes" name="image-id1" />
178
            <span class="checkbox-legends">{% trans "Custom protection" %} <a href="#" class="firewall-settings">{% trans "settings" %}</a> {% trans "(Advanced users only)" %}</span>
179
            <button type="submit" class="firewall-apply">{% trans "Apply" %}</button>
180
            <div class="firewall-contents-end-separator"></div>
181
        </div>
182
        <div class="network-separator machines"></div>
183
    </div>
184

    
185
    <!-- private network template -->
186
    <div class="network" id="private-template">
187
        <div class="actions">
188
            <a href="#" class="action-add">{% trans "Add Machine" %}</a>
189
            <a href="#" class="action-network-destroy">{% trans "Destroy" %}</a>
190
        </div>
191
        <div class="action_error" align="center">
192
            {% trans "<span>Error</span> on" %} <span class="action">{% trans "error action" %}</span>
193
            <span class="code"></span>
194
            <span class="message"></span>
195
            <button class="details">{% trans "Details" %}</button>
196
        </div>
197
        <div class="confirm_single">
198
            <button class="yes">{% trans "Confirm" %}</button>
199
            <button class="no">X</button>
200
        </div>
201
        <div class="action_error" align="center">
202
            {% trans "<span>Error</span> on" %} <span class="action">{% trans "error action" %}</span>
203
            <span class="code"></span>
204
            <span class="message"></span>
205
            <button class="details">{% trans "Details" %}</button>
206
        </div>
207
        <div class="state">
208
            <div class="status">{% trans "Private Network" %}</div>
209
            <div class="indicators network-indicator">
210
                <div class="indicator1"></div>
211
                <div class="indicator2"></div>
212
                <div class="indicator3"></div>
213
                <div class="indicator4"></div>
214
            </div>
215
            <img class="spinner" style="display:none" src="static/icons/indicators/medium/progress.gif" />
216
            <img class="wave" style="display:none" src="static/icons/indicators/medium/wave.gif" />
217
        </div>
218
        <img class="network-logos" src="static/network.png" />
219
        <div href="#" class="name-div">
220
            <h5 class="namecontainer editable">
221
                <span class="name">{% trans "My Network 1" %}</span>
222
                <span class="rename-network"></span>
223
                <div class="editbuttons" style="display:none">
224
                    <span class="save"></span>
225
                    <span class="cancel"></span>
226
                </div>
227
            </h5>
228
        </div>
229
        <h5 class="network-machines">
230
            {% trans "Show:" %} <span class="show-machines">{% trans "machines" %}</span>
231
            (<span class="machines-count">0</span>)
232
        </h5>
233
        <div class="network-contents">
234
            <div class="network-placeholder">
235
                <div class="network-contents-start-separator"></div>
236
                <div class="machines-list">
237
                <!-- append machines here -->
238
                </div>
239
                <div class="empty-network-slot" id="machine-template">
240
                    <div class='network-add-machine'><span class="add-icon">+</span></div>
241
                </div>
242
            </div>
243
            <div class="network-contents-end-separator"></div>
244
        </div>
245
        <div class="separator"></div>
246
    </div>
247

    
248
    <!-- template for machines in private network -->
249
    <div class="network-machine" id="private-machine-template">
250
        <div class="machine-actions">
251
            <a href="#" class="action-disconnect">{% trans "Disconnect" %}</a>
252
            <a href="#" class="action-details">{% trans "Details" %}</a>
253
        </div>
254
        <div class="confirm_single">
255
            <button class="yes">{% trans "Confirm" %}</button>
256
            <button class="no">X</button>
257
        </div>
258
        <div class="action_error" align="center">
259
            {% trans "<span>Error</span> on" %} <span class="action">{% trans "error action" %}</span>
260
            <span class="code"></span>
261
            <span class="message"></span>
262
            <button class="details">{% trans "Details" %}</button>
263
        </div>
264
        <div class="state">
265
            <img class="spinner" style="display:none" src="static/icons/indicators/medium/progress.gif" />
266
            <img class="wave" style="display:none" src="static/icons/indicators/medium/wave.gif" />
267
        </div>
268
        <img class="logo" src="static/icons/machines/medium/debian-on.png" />
269
        <div class='network-remove-machine'><span class="remove-icon">X</span></div>
270
        <div href="#" class="machine-name-div">
271
            <h5 class="namecontainer editable">
272
                <span class="name">my desktop1</span>
273
            </h5>
274
        </div>
275
        <h5 class='machine-connect'>
276
            <span>{% trans "Connect" %}</span> {% trans "to manage private IPs" %}
277
        </h5>
278
        <div class="network-separator machines"></div>
279
    </div>
280

    
281
    <!-- the actual structure to be populated -->
282
    <div class="public-networks"></div>
283
    <div class="private-networks" style="display:none;"></div>
284
</div>
285

    
286
<!-- add servers to network overlay -->
287
<div>
288
    <div id="add-machines-wizard" class="modal">
289
        <h3 class="popup-header">{% trans "Add machine" %}</h3>
290
        <p style='display:none;'>hidden network id</p>
291
        <div class="popup-body">
292
            <div class="popup-body-inner">
293
                <div class="popup-title">{% trans "Select machines to add to:" %}</div>
294
                <div class="network-name"></div>
295
                <div class="popup-separator"></div>
296
                <div class="machines-container">
297
                    <div class="large-spinner"></div>
298
                    <div id='machine-entry-template' style="display:none;">
299
                        <input type="checkbox" />
300
                        <img class=list-logo src="static/icons/machines/small/debian-on.png" title="debian"></img>
301
                        <span class="machine-name">kati</span>
302
                    </div>
303
                </div>
304
            </div>
305
        </div>
306
        <div class="buttons">
307
            <button type="button" class="cancel" id="add-server-cancel">{% trans "Cancel" %}</button>
308
            <button type="button" class="add" id="add-server-add">{% trans "Add" %}</button>
309
        </div>
310
    </div>
311
</div>
312

    
313
<a id="add-machines-overlay" href="#" rel="#add-machines-wizard"></a>
314

    
315
<!-- multiple confirmation dialog -->
316
<div class="confirm_multiple">
317
    <p>{% trans "Your actions will affect" %} <span class="actionLen">XX</span> {% trans "networks" %}</p>
318
    <button class="yes">{% trans "Confirm All" %}</button>
319
    <button class="no">{% trans "Cancel All" %}</button>
320
</div>
321

    
322
<script>
323
// add network wizard initialization
324
$(function() {
325
    // create wizard overlay
326
    $("a#networkscreate").overlay({
327
        mask: '#000',
328
        effect: 'default',
329
        top: '10%',
330
        closeOnClick: false,
331
        oneInstance: false,
332
        closeOnEsc: true,
333
        load: false
334
    });
335
});
336

337
// intercept click to create new private network
338
$("#networkscreate").click(function() {
339
    // reset and load wizard
340
    $('#networks-wizard span.help').removeClass('red');
341
    $('#networks-wizard input').val('');
342
    $('#networks-wizard #add-network-create').text('{% trans 'Create Network' %}');
343
    $("#networks-wizard").show();
344
    $('#networks-wizard input').focus();
345
    $("a#networkscreate").data('overlay').load();
346
    return false;
347
});
348

349
// exit the add machine to network wizard
350
$("#add-network-cancel").live('click', function() {
351
    $("a#networkscreate").overlay().close();
352
});
353

354
// add selected machines to network
355
$("#add-network-create").live('click', function() {
356
    if ($('#networks-wizard input').val() == '') {
357
        $('#networks-wizard input').focus();
358
        $('#networks-wizard span.help').addClass('red');
359
    } else {
360
        $(this).text('');
361
        $(this).html('<img src="static/icons/indicators/medium/horizontal-progress.gif"></img>');
362
        networkName = $('#networks-wizard input').val();
363
        create_network(networkName);
364
    }
365
    return false;
366
});
367

368
// toggle component with class network-contents
369
$("#networks-pane .show-machines").live('click', function() {
370
    if ($('#networks-pane .network-contents .network-machine').length > 0) {
371
        $(this).parent().next("#networks-pane .network-contents").slideToggle(600);
372
    }
373
    return false;
374
});
375

376
// toggle component with class network-contents
377
$("#networks-pane .network-logos").live('click', function() {
378
    $(this).siblings("#networks-pane .network-contents").slideToggle(600);
379
    return false;
380
});
381

382
//initiate network renaming
383
$("#networks-pane .name-div .rename-network, #networks-pane .name-div h5.editable span.name").live('click', function() {
384
    $(this).parent().find('.name').html("<input id=\"txtEdit\" type=\"text\" class=\"nametextbox\" value=\"" +
385
                                        $(this).parent().find('.name').text() +
386
                                        "\" / ><span class=\"oldValue\">" +
387
                                        $(this).parent().find('.name').text() + "</span>");
388
    $(this).parent().find('.rename-network').hide();
389
    $(this).parent().find(".editbuttons").fadeIn();
390
    $(this).parent().find(".nametextbox").focus().select();
391
    $(this).parent().removeClass('editable');
392

393
    //submit wizard by pressing enter on the name textbox
394
    $("#txtEdit").keydown(function (e) {
395
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
396
            $(this).parent().parent().find('div.editbuttons span.save').click();
397
            return false;
398
        } else if ((e.which && e.which == 27) || (e.keyCode && e.keyCode == 27)) {
399
            $(this).parent().parent().find('div.editbuttons span.cancel').click();
400
            return true;
401
        }
402
    });
403
    return false;
404
});
405

406
//rename machine
407
$("#networks-pane .name-div .editbuttons .save").live('click', function() {
408
    networkID = $(this).closest('.network').attr("id").split('-').pop();
409
    networkName = $(this).parent().parent().find('.nametextbox').val();
410
    if (networkName.trim() == ''){
411
        return false;
412
    }
413
    $(this).parent().parent().find('.name').html($(this).parent().parent().find('.nametextbox').val());
414
    $(this).parent().parent().find(".editbuttons").fadeOut("fast");
415
    $(this).parent().parent().find(".rename-network").fadeIn("slow");
416
    rename_network(networkID, networkName);
417
    return false;
418
});
419

420
//cancel renaming
421
$("#networks-pane .name-div .editbuttons .cancel").live('click', function() {
422
    $(this).parent().parent().find('.name').html($(this).parent().parent().find('.oldValue').text());
423
    $(this).parent().parent().find(".editbuttons").hide();
424
    $(this).parent().parent().find(".rename-network").fadeIn();
425
    $(this).parent().parent().addClass('editable');
426
    return false;
427
});
428

429
// intercept click to delete network
430
$("#networks-pane .action-network-destroy").live('click', function() {
431
    var networkID = $(this).parent().parent().attr('id').split('-').pop();
432
    var networkName = $(this).parent().parent().find(".name-div span.name").text();
433
    var found = false;
434
    $(this).parent().children('a').removeClass('selected');
435
    $(this).addClass('selected');
436
    $(this).parent().addClass('display')
437
    $(this).parent().parent().find('.action_error').hide();
438
    for (i=0; i < pending_actions.length; i++){ // if there is already a pending action for this network replace it
439
        if (pending_actions[i][0] == delete_network && pending_actions[i][1] == networkID){
440
            pending_actions[i][0] = delete_network;
441
            found = true
442
        }
443
    }
444
    if (!found) {
445
        // no pending action for this network was found, so let's just add it to the list
446
        pending_actions.push([delete_network, networkID, networkName]);
447
    }
448
    update_network_confirmations();
449
    return false;
450
});
451

452
// intercept click to remove machine from network
453
$("#networks-pane .remove-icon").live('click', function() {
454
    $(this).closest('.network-machine').find('.action-disconnect').click();
455
    return false;
456
});
457

458
// intercept click to remove machine from network
459
$("#networks-pane .action-disconnect").live('click', function() {
460
    var serverID = $(this).parent().parent().attr('id').split('-').pop();
461
    var networkID = $(this).closest('.network').attr('id').split('-').pop();
462
    var found = false;
463
    $(this).parent().children('a').removeClass('selected');
464
    $(this).addClass('selected');
465
    $(this).parent().addClass('display')
466
    $(this).parent().parent().find('.action_error').hide();
467
    for (i=0; i < pending_actions.length; i++){
468
        // if there is already a pending action for this network replace it
469
        if (pending_actions[i][0] == remove_server_from_network &&
470
            pending_actions[i][1] == networkID &&
471
            pending_actions[i][2] == serverID) {
472
                pending_actions[i][0] = remove_server_from_network;
473
                found = true
474
        }
475
    }
476
    if (!found) {
477
        // no pending action for this network was found, so let's just add it to the list
478
        pending_actions.push([remove_server_from_network, networkID, serverID]);
479
    }
480
    update_network_confirmations();
481
    return false;
482
});
483

484
$("#networks-pane .action-details").live('click', function() {
485
    var serverID = $(this).parent().parent().attr('id').split('-').pop();
486
    $.cookie('view', '2');
487
    $.cookie('server', serverID);
488
    $('a#machines').click();
489
    return false
490
});
491

492
// add machines wizard initialization
493
$(function() {
494
    // create wizard overlay
495
    $("a#add-machines-overlay").overlay({
496
        mask: '#000',
497
        effect: 'default',
498
        top: '10%',
499
        closeOnClick: false,
500
        oneInstance: false,
501
        closeOnEsc: true,
502
        load: false
503
    });
504
});
505

506
// intercept click to add machine to  network
507
$("#networks-pane .action-add").live('click', function() {
508
    // reset pre-existing values
509
    $('#add-server-add').text('{% trans 'Add' %}');
510
    $("#add-machines-wizard .network-name").text($(this).closest('.network').find('.name-div span.name').text());
511
    $("#add-machines-wizard p").text($(this).closest('.network').attr('id').split('-').pop());
512
    $("#add-machines-wizard .large-spinner").show();
513
    $('[id^="list-entry-server-"]').remove();
514
    // list new values
515
    list_machines();
516
    // load wizard
517
    $("#add-machines-wizard").show();
518
    $("a#add-machines-overlay").data('overlay').load();
519
    return false;
520
});
521

522
//submit wizard by pressing enter on the name textbox
523
$("#networks-wizard div.name-input input.text").keypress(function (e) {
524
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
525
        $('#add-network-create').click();
526
        return false;
527
    } else {
528
        return true;
529
    }
530
});
531

532
// intercept click to bring up the add machine to network wizard
533
$("#networks-pane .add-icon").live('click', function() {
534
    $(this).closest('.network').find('.action-add').click();
535
});
536

537
// get machines in public network and list them in add server to network wizard
538
function list_machines() {
539
    var networkID = $("#add-machines-wizard p").text();
540
    // get the list of servers from the UI
541
    var all_machines = $("#networks-pane .public-networks .network-machine");
542

543
    $.each(all_machines, function(i, machine) {
544
        var currentID = machine.id.split('-').pop();
545
        // if machine already exists in private network
546
        if ( $('#net-' + networkID + '-server-' + currentID).length > 0 ) {
547
            // do nothing
548
        } else {
549
            // put it in the list
550
            var entry = $("#add-machines-wizard #machine-entry-template").clone().attr("id", "list-entry-server-" + currentID);
551
            entry.find('input').attr("name", currentID);
552
            entry.find('img').attr('src', 'static/icons/machines/small/' + $(machine).find('img.logo').attr('src').split('/').pop());
553
            entry.find('.machine-name').text($(machine).find('span.name').text());
554
            entry.appendTo("#add-machines-wizard .machines-container");
555
        }
556
    });
557
    $("#add-machines-wizard .large-spinner").hide();
558
    $('[id^="list-entry-server-"]').fadeIn("slow");
559
    return false;
560
}
561

562
// exit the add machine to network wizard
563
$("#add-server-cancel").live('click', function() {
564
    $("a#add-machines-overlay").overlay().close();
565
});
566

567
// add selected machines to network
568
$("#add-server-add").live('click', function() {
569
    var selections = $("#add-machines-wizard :checkbox:checked");
570
    var networkID = $("#add-machines-wizard p").text();
571
    var serverIDs = [];
572
    $.each(selections, function(i, selection) {
573
        serverIDs.push(selection.name);
574
    });
575
    $(this).text('');
576
    add_server_to_network(networkID, serverIDs);
577
    $(this).html('<img src="static/icons/indicators/medium/horizontal-progress.gif"></img>');
578
    return false;
579
});
580

581
// toggle component with class firewall-contents
582
$("#networks-pane .show-firewall").live('click', function() {
583
    $(this).parent().next(".firewall-content").slideToggle(600);
584
    return false;
585
});
586

587
//hide firewall content on apply click
588
$("#networks-pane .firewall-apply").live('click', function() {
589
    $(this).parent().slideToggle(600);
590
    return false;
591
});
592

593
// confirm single action
594
$("#networks-pane div.confirm_single .yes").live('click', function() {
595
    // this works both for server and network actions
596
    var serverID = [];
597
    if ($(this).closest('.network-machine').attr('id')) {
598
        serverID = $(this).closest('.network-machine').attr('id').split('-').pop();
599
    }
600
    var networkID = $(this).closest('.network').attr("id").split('-').pop();
601
    // execute actions
602
    for (i=0;i<pending_actions.length;i++){
603
        // if there is a pending action for this server execute it
604
        if (pending_actions[i][0] == delete_network &&
605
            pending_actions[i][1] == networkID){
606
                action = pending_actions.splice(i,1)[0];
607
                $(this).parent().parent().find('.status').text(TRANSITIONS['Destroying']);
608
                $(this).parent().parent().find('.spinner').show();
609
                action[0]([action[1]]); // execute action
610
        } else if ( pending_actions[i][0] == remove_server_from_network &&
611
                    pending_actions[i][1] == networkID &&
612
                    pending_actions[i][2] == serverID) {
613
                        action = pending_actions.splice(i,1)[0];
614
                        action[0]([action[1]], [action[2]]); // execute action
615
        }
616
    }
617
    $(this).parent().hide();
618
    $(this).parent().prev().prev().removeClass('selected');
619
    $(this).closest('div.actions').find('a').removeClass('selected');
620
    $(this).closest("div.network").children('div.actions').removeClass('display');
621
    $(this).closest("div.network").children('div.actions').removeClass('selected');
622
    update_network_confirmations();
623
//
624
//    $(this).closest('div.actions').find('a').removeClass('selected');
625
//    $(this).closest("div.machine").children('.state').children('.spinner').show()
626
//    $(this).closest("div.machine").children('div.actions').removeClass('display');
627
    return false;
628
});
629

630
// cancel single action
631
$("#networks-pane div.confirm_single .no").live('click', function(){
632
    // this works both for server and network actions
633
    var serverID = [];
634
    if ($(this).closest('.network-machine').attr('id')) {
635
        serverID = $(this).closest('.network-machine').attr('id').split('-').pop();
636
    }
637
    var networkID = $(this).closest('.network').attr("id").split('-').pop();
638
    // remove the action from the pending list
639
    $(this).parent().prev().children('a').removeClass('selected');
640
    $(this).parent().prev().removeClass('display');
641
    for (i=0; i < pending_actions.length; i++){ // if there is a pending action for this network remove it
642
        if (pending_actions[i][0] == delete_network && pending_actions[i][1] == networkID){
643
                pending_actions.splice(i,1);
644
        } else if ( pending_actions[i][0] == remove_server_from_network &&
645
                    pending_actions[i][1] == networkID &&
646
                    pending_actions[i][2] == serverID) {
647
                        pending_actions.splice(i,1);
648
        }
649
    }
650
    $(this).parent().hide();
651
    update_network_confirmations();
652
    return false;
653
});
654

655
// show, single action, error details
656
$("#networks-pane div.action_error .details").live('click', function(){
657
    // remove the action from the pending list
658
    $(this).parent().hide();
659
    ajax_error($(this).parent().children('.code').text(),
660
                 undefined,
661
                 $(this).parent().children('.action').text(),
662
                 $(this).parent().children('.message').text());
663
    return false;
664
});
665

666
// confirm all actions
667
$("#networks-pane div.confirm_multiple .yes").live('click', function(){
668
    while(pending_actions.length > 0){ // if there is a pending action for this network execute it
669
        action = pending_actions.pop(); // extract action
670
        var networkID = action[1];
671
        $('#networks-pane .selected').removeClass('selected');
672
        $('#networks-pane .display').removeClass('display');
673
        $('#networks-pane .confirm_single').hide();
674
        if (action[0] == delete_network) {
675
            $('#networks-pane #net-' + networkID + ' .status').text(TRANSITIONS['Destroying']);
676
            $('#networks-pane #net-' + networkID + ' .spinner').show();
677
            action[0]([networkID]); // execute action
678
        } else if (action[0] == remove_server_from_network) {
679
            action[0]([action[1]], [action[2]]); // execute action
680
        }
681
    }
682
    update_network_confirmations();
683
    return false;
684
});
685

686
// cancel all actions
687
$("#networks-pane div.confirm_multiple .no").live('click', function(){
688
    pending_actions = [];
689
    $('#networks-pane .selected').removeClass('selected');
690
    $('#networks-pane .display').removeClass('display');
691
    $('#networks-pane .confirm_single').hide();
692
    update_network_confirmations();
693
    return false;
694
});
695

696
// update the networks list
697
function update_networks_view(servers_data, networks_data){
698
    /*
699
    Go through the input data. Update existing entries, add
700
    new ones to the list
701
    */
702

703
    // check for changes in networks
704
    if (networks_data){
705
        /*
706
        Here we are interested in private networks only
707
        and not for any server they might contain
708
        */
709

710
        // update private networks
711
        $.each(networks_data.networks.values, function(i,network) {
712
            // search DOM for this network
713
            existing = $('#networks-pane #net-' + network.id);
714

715
            // if multiple machines exist in the DOM, delete all but one
716
            // defensive coding - that shouldn't happen normally
717
            while (existing.length > 1){
718
                existing.remove();
719
            }
720
            // If network already exists in DOM, update it
721
            if (existing.length){
722
                // network was deleted
723
                if (network.status == 'DELETED') {
724
                    existing.remove();
725
                }
726

727
                // network was renamed
728
                if (existing.find('.name-div span.name').text() != network.name) {
729
                    // set the new name
730
                    existing.find('.name-div span.name').text(network.name);
731
                }
732
            }
733
            // If network does not exist in DOM, create it, do not take into account public network
734
            else if (network.id != 'public'){
735
                var privNet = $("#networks-pane #private-template").clone().attr("id", "net-" + network.id);
736
                privNet.find(".name-div span.name").text(network.name).fadeIn("slow");
737
                privNet.appendTo("#networks-pane .private-networks");
738
            }
739
        });
740
    }
741

742
    // check for changes in servers
743
    if (servers_data) {
744
        /*
745
        Here we are interested in any server contained
746
        either in a public network or a private one
747
        */
748
        $.each(servers_data.servers.values, function(i,server) {
749
            /*
750
            First update the public network
751
            */
752

753
            // search public network's DOM for this server
754
            existing_public = $('#networks-pane #net-pub-server-' + server.id);
755
            // get the server's OS icon
756
            var server_image = os_icon(server.metadata);
757

758
            // if multiple servers exist in public network, delete all but one
759
            // defensive coding - that shouldn't happen normally
760
            while (existing_public.length > 1){
761
                existing_public.remove();
762
            }
763

764
            // server was deleted
765
            if (server.status == 'DELETED') {
766
                existing_public.remove();
767
            }
768

769
            // If server already exists in public network, update it
770
            if (existing_public.length){
771
                // server was renamed
772
                if (server.name != existing_public.find('span.name').text()) {
773
                    existing_public.find('span.name').text(server.name);
774
                }
775
                // server has changed state
776
                if (server.status=='BUILD' || server.status=='ACTIVE' ||server.status=='REBOOT') {
777
                    existing_public.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-on.png');
778
                } else {
779
                    existing_public.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-off.png');
780
                }
781
                // server has new ips
782
                var old_ip4 = existing_public.find('span.ip4').text();
783
                var old_ip6 = existing_public.find('span.ip6').text();
784
                // the following are valid because the public ips always come in this order from the api
785
                var new_ips = get_public_ips(server);
786
                var new_ip4 = new_ips['ip4'];
787
                var new_ip6 = new_ips['ip6'];
788
                if (old_ip4 != new_ip4) {
789
                    if (new_ips['ip4'] == undefined) {
790
                        existing_public.find(".ip4-container").hide();
791
                    } else {
792
                        existing_public.find('span.ip4').text(new_ip4);
793
                    }
794
                }
795
                if (old_ip6 != new_ip6) {
796
                    if (new_ip6 == undefined) {
797
                        existing_public.find(".ip6-container").hide();
798
                    } else {
799
                        existing_public.find('span.ip6').text(new_ip6);
800
                    }
801
                }
802
                //TODO: server has changed OS
803
            }
804
            // If server does not exist in public network, create it
805
            else {
806
                var machine = $("#networks-pane #public-machine-template").clone().attr("id", "net-pub-server-" + server.id).fadeIn("slow");
807
                machine.find('span.name').text(server.name);
808
                // find and display ips
809
                var ips = get_public_ips(server);
810
                if (ips['ip4'] == undefined) {
811
                    machine.find(".ip4-container").hide();
812
                } else {
813
                    machine.find("span.ip4").text(ips['ip4']);
814
                }
815
                if (ips['ip6'] == undefined) {
816
                    machine.find(".ip6-container").hide();
817
                } else {
818
                    machine.find("span.ip6").text(ips['ip6']);
819
                }
820
                // add the proper icon
821
                if (server.status=='BUILD' || server.status=='ACTIVE' ||server.status=='REBOOT') {
822
                    machine.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-on.png');
823
                } else {
824
                    machine.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-off.png');
825
                }
826
                machine.appendTo("#networks-pane .public-networks .machines-list");
827
            }
828
            /*
829
            Now update all private networks
830
            Since one server may belong to more than one private networks,
831
            we follow a different approach.
832
            We check each server and add, update or leave it as it is accordingly
833
            */
834
            try {
835
                $.each(server.addresses.values, function(i,server_net) {
836
                    // find in which private networks it belongs
837
                    if (server_net.id != "public") {
838
                        var existing_private = $("#networks-pane .private-networks #net-" + server_net.id + "-server-" + server.id);
839
                        // add new server
840
                        if (!existing_private.length) {
841
                            var machine = $("#networks-pane #private-machine-template").clone().attr("id", "net-" + server_net.id + "-server-" + server.id).fadeIn("slow");
842
                            machine.find('span.name').text(server.name);
843
                            if (server.status=='BUILD' || server.status=='ACTIVE' ||server.status=='REBOOT') {
844
                                machine.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-on.png');
845
                            } else {
846
                                machine.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-off.png');
847
                            }
848
                            machine.appendTo("#networks-pane .private-networks #net-" + server_net.id + " .machines-list");
849
                        }
850
                        // server was deleted
851
                        if (server.status == 'DELETED') {
852
                            existing_private.remove();
853
                        }
854
                        // server was renamed
855
                        if (server.name != existing_private.find('span.name').text()) {
856
                            existing_private.find('span.name').text(server.name);
857
                        }
858
                        // server has changed state
859
                        if (server.status=='BUILD' || server.status=='ACTIVE' ||server.status=='REBOOT') {
860
                            existing_private.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-on.png');
861
                        } else {
862
                            existing_private.find("img.logo").attr("src","static/icons/machines/medium/" + server_image + '-off.png');
863
                        }
864
                        //TODO: server has changed OS
865
                    }
866
                });
867
            } catch (err) {
868
                try{console.info('Server ' + server.id + ' has no network addresses')}catch(err){};
869
            }
870
        });
871
    }
872

873
    $("#networks-pane #networks-container > .large-spinner").hide();
874

875
    // show all separators and hide the last one
876
    $("div.private-networks div.network div.separator").show();
877
    $("div.private-networks div.network div.separator:last").hide();
878
    // update the counters of servers in each network
879
    var nets = $('#networks-pane .network');
880
    $.each(nets, function(i,net) {
881
        $(net).find('span.machines-count').text($(net).find('.network-machine').length);
882
    });
883

884
    // the private div shows only when private networks are available
885
    if ($("div.private-networks div.network").length > 0) {
886
        $("div.private-networks").fadeIn("slow");
887
    } else {
888
        $("div.private-networks").fadeOut("slow");
889
    }
890

891
    // tag the last seperator for better styling
892
    $("#pub .network-machine").removeClass('last');
893
    $("#pub .network-machine").last().addClass('last');
894
}
895

896
// indicate that the requested action was not completed
897
function display_failure(status, networkID, action, responseText) {
898
    $('#networks-container #net-'+networkID+ ' .spinner').hide();
899
    $('#networks-container #net-'+networkID+ ' .action_error .action').text(action);
900
    $('#networks-container #net-'+networkID+ ' .action_error .code').text(status);
901
    $('#networks-container #net-'+networkID+ ' .action_error .message').text(responseText);
902
    $('#networks-container #net-'+networkID+ ' .action_error').show();
903
}
904

905

906
// reposition multiple confirmation box on window resize
907
$(window).resize(function(){
908
    if (this.innerHeight - 220 < $('#networks-pane #networks-container').height())
909
        $('#networks-pane .confirm_multiple').addClass('fixed');
910
    else
911
        $('#networks-pane .confirm_multiple').removeClass('fixed');
912
});
913

914
// basic functions executed on page load
915
// hide the all of the networks contents
916
$("#networks-pane .network-contents").hide();
917
// hide the all of the firewall contents
918
$("#networks-pane .firewall-content").hide();
919
changes_since = 0; // to reload full list of servers
920
networks_changes_since= 0; // to reload full list of networks
921
// there is alway one public network to render
922
var pubNet = $("#networks-pane #public-template").clone().attr("id", "pub").fadeIn("slow");
923
pubNet.appendTo("#networks-pane .public-networks");
924
update_networks(UPDATE_INTERVAL);
925

    
926
</script>