Skip to content

Commit

Permalink
Remove extra jQuery object creation in horizon.instances.js
Browse files Browse the repository at this point in the history
Reduces the number of jQuery document objects from 8 to 1 using a variable.
Removes the creation of redundant jQuery objects based on a selector.

Change-Id: I9ed0815f4fd171cec49c66b73cc04addd46c820d
Partial-Bug: #1412971
  • Loading branch information
mattfarina committed Jan 22, 2015
1 parent 67dee8e commit b48789e
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions horizon/static/horizon/js/horizon.instances.js
Expand Up @@ -81,18 +81,21 @@ horizon.instances = {
**/
generate_networklist_html: function() {
var self = this;
var available_network = $("#available_network");
var selected_network = $("#selected_network");
var updateForm = function() {
var lists = $("#networkListId li").attr('data-index',100);
var networkListId = $("#networkListId");
var lists = networkListId.find("li").attr('data-index',100);
var active_networks = $("#selected_network > li").map(function(){
return $(this).attr("name");
});
$("#networkListId input:checkbox").removeAttr('checked');
networkListId.find("input:checkbox").removeAttr('checked');
active_networks.each(function(index, value){
$("#networkListId input:checkbox[value=" + value + "]")
networkListId.find("input:checkbox[value=" + value + "]")
.prop('checked', true)
.parents("li").attr('data-index',index);
});
$("#networkListId ul").html(
networkListId.find("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; }
Expand All @@ -104,14 +107,14 @@ horizon.instances = {
$("#networkListIdContainer").hide();
self.init_network_list();
// Make sure we don't duplicate the networks in the list
$("#available_network").empty();
available_network.empty();
$.each(self.networks_available, function(index, value){
$("#available_network").append(self.generate_network_element(value.name, value.id, value.value));
available_network.append(self.generate_network_element(value.name, value.id, value.value));
});
// Make sure we don't duplicate the networks in the list
$("#selected_network").empty();
selected_network.empty();
$.each(self.networks_selected, function(index, value){
$("#selected_network").append(self.generate_network_element(value.name, value.id, value.value));
selected_network.append(self.generate_network_element(value.name, value.id, value.value));
});
// $(".networklist > li").click(function(){
// $(this).toggleClass("ui-selected");
Expand All @@ -121,25 +124,25 @@ horizon.instances = {
e.preventDefault();
e.stopPropagation();
if($this.parents("ul#available_network").length > 0) {
$this.parent().appendTo($("#selected_network"));
$this.parent().appendTo(selected_network);
} else if ($this.parents("ul#selected_network").length > 0) {
$this.parent().appendTo($("#available_network"));
$this.parent().appendTo(available_network);
}
updateForm();
});
if ($("#networkListId > div.form-group.error").length > 0) {
var errortext = $("#networkListId > div.form-group.error").find("span.help-block").text();
var errortext = $("#networkListId > div.form-group.error span.help-block").text();
$("#selected_network_label").before($('<div class="dynamic-error">').html(errortext));
}
$(".networklist").sortable({
connectWith: "ul.networklist",
placeholder: "ui-state-highlight",
distance: 5,
start:function(e,info){
$("#selected_network").addClass("dragging");
selected_network.addClass("dragging");
},
stop:function(e,info){
$("#selected_network").removeClass("dragging");
selected_network.removeClass("dragging");
updateForm();
}
}).disableSelection();
Expand All @@ -152,7 +155,9 @@ horizon.instances = {
};

horizon.addInitFunction(horizon.instances.init = function () {
$(document).on('submit', '#tail_length', function (evt) {
var $document = $(document);

$document.on('submit', '#tail_length', function (evt) {
horizon.instances.user_decided_length = true;
horizon.instances.getConsoleLog(true);
evt.preventDefault();
Expand Down Expand Up @@ -192,7 +197,7 @@ horizon.addInitFunction(horizon.instances.init = function () {
}
}

$(document).on('change', '.workflow #id_source_type', function (evt) {
$document.on('change', '.workflow #id_source_type', function (evt) {
update_launch_source_displayed_fields(this);
});

Expand Down Expand Up @@ -231,19 +236,19 @@ horizon.addInitFunction(horizon.instances.init = function () {
size_field.val(volume_size);
}

$(document).on('change', '.workflow #id_flavor', function (evt) {
$document.on('change', '.workflow #id_flavor', function (evt) {
update_device_size();
});

$(document).on('change', '.workflow #id_image_id', function (evt) {
$document.on('change', '.workflow #id_image_id', function (evt) {
update_device_size();
});

$(document).on('input', '.workflow #id_volume_size', function (evt) {
$document.on('input', '.workflow #id_volume_size', function (evt) {
horizon.instances.user_volume_size = true;
// We only need to listen for the first user input to this field,
// so remove the listener after the first time it gets called.
$(document).off('input', '.workflow #id_volume_size');
$document.off('input', '.workflow #id_volume_size');
});

horizon.instances.decrypt_password = function(encrypted_password, private_key) {
Expand All @@ -252,7 +257,7 @@ horizon.addInitFunction(horizon.instances.init = function () {
return crypt.decrypt(encrypted_password);
};

$(document).on('change', '#id_private_key_file', function (evt) {
$document.on('change', '#id_private_key_file', function (evt) {
var file = evt.target.files[0];
var reader = new FileReader();
if (file) {
Expand All @@ -274,14 +279,16 @@ horizon.addInitFunction(horizon.instances.init = function () {
The font-family is changed because with the default policy the major I
and minor the l cannot be distinguished.
*/
$(document).on('show.bs.modal', '#password_instance_modal', function (evt) {
$("#id_decrypted_password").css("font-family","monospace");
$("#id_decrypted_password").css("cursor","text");
$document.on('show.bs.modal', '#password_instance_modal', function (evt) {
$("#id_decrypted_password").css({
"font-family": "monospace",
"cursor": "text"
});
$("#id_encrypted_password").css("cursor","text");
$("#id_keypair_name").css("cursor","text");
});

$(document).on('click', '#decryptpassword_button', function (evt) {
$document.on('click', '#decryptpassword_button', function (evt) {
encrypted_password = $("#id_encrypted_password").val();
private_key = $('#id_private_key').val();
if (!private_key) {
Expand Down

0 comments on commit b48789e

Please sign in to comment.