Javascript force use of the identity operator

In order to reduce possible bugs in our scripts the use of identity
operator must be mandatory over the equality operator. This rule force
developers to use the '===' and '!==' instead of '==' and '!='.

Change-Id: I1069ed2baed7bce305fce80d688a5a2c10249148
Implements: blueprint jshint-codestyle
This commit is contained in:
Maxime Vidori 2014-01-02 15:58:43 +01:00
parent ce66bd8320
commit ae5730e255
13 changed files with 57 additions and 56 deletions

View File

@ -330,8 +330,8 @@ horizon.d3_bar_chart = {
start at 0.
*/
self.chart_start_x = 0;
if (self.data.settings.orientation=='vertical'){
if (self.used_label_placement == 'left'){
if (self.data.settings.orientation === 'vertical'){
if (self.used_label_placement === 'left'){
self.chart_start_x = 44;
}
self.chart_wrapper_w = self.w + self.chart_start_x;
@ -392,7 +392,7 @@ horizon.d3_bar_chart = {
// Return true if it chart is oriented horizontally
self.horizontal_orientation = function (){
return (self.data.settings.orientation == 'horizontal');
return (self.data.settings.orientation === 'horizontal');
};
},
/**
@ -458,7 +458,7 @@ horizon.d3_bar_chart = {
.duration(500)
.attr(self.trasition_attr, self.trasition_value);
if (self.wrapper.used_label_placement == 'left') {
if (self.wrapper.used_label_placement === 'left') {
// Now it works only for vertical bar chart placed left form the chart
var label_placement_y = self.wrapper.h - self.wrapper.used_value_in_pixels;

View File

@ -425,7 +425,7 @@ horizon.d3_line_chart = {
});
var renderer = self.settings.renderer;
if (renderer == 'StaticAxes'){
if (renderer === 'StaticAxes'){
renderer = Rickshaw.Graph.Renderer.StaticAxes;
}
@ -451,7 +451,7 @@ horizon.d3_line_chart = {
*/
graph.render();
if (self.hover_formatter == 'verbose'){
if (self.hover_formatter === 'verbose'){
var hoverDetail = new Rickshaw.Graph.HoverDetail({
graph: graph,
formatter: function(series, x, y) {

View File

@ -163,7 +163,7 @@ horizon.addInitFunction(function () {
base_type = $this.val();
$this.find("option").each(function () {
if (this.value != base_type) {
if (this.value !== base_type) {
$("#id_" + this.value).closest(".control-group").hide();
} else {
$("#id_" + this.value).closest(".control-group").show();
@ -202,7 +202,7 @@ horizon.addInitFunction(function () {
});
// Hide the tooltip after escape button pressed
$(document).on('keydown.esc_btn', function (evt) {
if (evt.keyCode == 27) {
if (evt.keyCode === 27) {
$('.tooltip').hide();
}
});

View File

@ -95,7 +95,7 @@ function removeNode (name) {
var i = 0;
var n = findNode(name);
while (i < links.length) {
if (links[i].source == n || links[i].target == n) {
if (links[i].source === n || links[i].target === n) {
links.splice(i, 1);
} else {
i++;
@ -110,7 +110,7 @@ function remove_nodes(old_nodes, new_nodes){
for (var i=0;i<old_nodes.length;i++) {
var remove_node = true;
for (var j=0;j<new_nodes.length;j++) {
if (old_nodes[i].name==new_nodes[j].name){
if (old_nodes[i].name === new_nodes[j].name){
remove_node = false;
break;
}
@ -142,7 +142,7 @@ function build_node_links(node){
}
//check for duplicates
for (var lidx=0;lidx<links.length;lidx++) {
if (links[lidx].source == source_idx && links[lidx].target == target_idx) {
if (links[lidx].source === source_idx && links[lidx].target === target_idx) {
push_link=false;
break;
}
@ -164,7 +164,7 @@ function build_reverse_links(node){
for (var j=0;j<nodes[i].required_by.length;j++){
var dependency = nodes[i].required_by[j];
//if new node is required by existing node, push new link
if(node.name==dependency){
if(node.name === dependency){
links.push({
'source':findNodeIndex(nodes[i].name),
'target':findNodeIndex(node.name),
@ -199,7 +199,7 @@ function ajax_poll(poll_time){
current_node.status = d.status;
//Status has changed, image should be updated
if (current_node.image != d.image){
if (current_node.image !== d.image){
current_node.image = d.image;
var this_image = d3.select("#image_"+current_node.name);
this_image

View File

@ -175,7 +175,7 @@ horizon.membership = {
var $roles_display = $dropdown.children('.dropdown-toggle').children('.roles_display');
var roles_to_display = [];
for (var i = 0; i < role_ids.length; i++) {
if (i == 2) {
if (i === 2) {
roles_to_display.push('...');
break;
}
@ -280,7 +280,7 @@ horizon.membership = {
var css_class = $(this).find('ul').attr('class');
// Example value: members step_slug_members
// Pick the class name that contains the step_slug
var filter = $.grep(css_class.split(' '), function(val){ return val.indexOf(step_slug) != -1; })[0];
var filter = $.grep(css_class.split(' '), function(val){ return val.indexOf(step_slug) !== -1; })[0];
if (!$('.' + filter).children('ul').length) {
$('#no_' + filter).show();
@ -392,7 +392,7 @@ horizon.membership = {
var css_class = $(this).children().children('ul').attr('class');
// Example value: members step_slug_members
// Pick the class name that contains the step_slug
var filter = $.grep(css_class.split(' '), function(val){ return val.indexOf(step_slug) != -1; })[0];
var filter = $.grep(css_class.split(' '), function(val){ return val.indexOf(step_slug) !== -1; })[0];
var input = $("input[id='" + filter +"']");
input.quicksearch('ul.' + filter + ' ul li span.display_name', {
@ -400,7 +400,7 @@ horizon.membership = {
'loader': 'span.loading',
'show': function () {
$(this).parent().parent().show();
if (filter == "available_" + step_slug) {
if (filter === "available_" + step_slug) {
$(this).parent('.dropdown-toggle').hide();
}
},
@ -415,7 +415,7 @@ horizon.membership = {
return new RegExp(val, "i");
},
'testQuery': function (query, txt, span) {
if ($(input).attr('id') == filter) {
if ($(input).attr('id') === filter) {
$(input).prev().removeAttr('disabled');
return query.test($(span).text());
}
@ -464,7 +464,7 @@ horizon.membership = {
// prevent filter inputs from submitting form on 'enter'
$form.find('.' + step_slug + '_membership').keydown(function(event){
if(event.keyCode == 13) {
if(event.keyCode === 13) {
event.preventDefault();
return false;
}

View File

@ -219,7 +219,7 @@ horizon.addInitFunction(function() {
$(document).on('hidden', '.modal', function () {
var $this = $(this),
modal_stack = $("#modal_wrapper .modal");
if ($this[0] == modal_stack.last()[0] || $this.hasClass("loading")) {
if ($this[0] === modal_stack.last()[0] || $this.hasClass("loading")) {
$this.remove();
if (!$this.hasClass("loading")) {
$("#modal_wrapper .modal").last().modal("show");

View File

@ -101,7 +101,7 @@ horizon.network_topology = {
$(window)
.on('message',function(e){
var message = $.parseJSON(e.originalEvent.data);
if (self.previous_message != message.message) {
if (self.previous_message !== message.message) {
horizon.alert(message.type, message.message);
horizon.autoDismissAlerts();
self.previous_message = message.message;
@ -133,7 +133,7 @@ horizon.network_topology = {
select_draw_mode:function() {
var self = this;
var draw_mode = $.cookie('ntp_draw_mode');
if (draw_mode && (draw_mode == 'normal'| draw_mode == 'small')) {
if (draw_mode && (draw_mode === 'normal'| draw_mode === 'small')) {
self.draw_mode = draw_mode;
} else {
if (self.model.networks.length *
@ -174,14 +174,14 @@ horizon.network_topology = {
self.select_main_port(device.ports).network_id : self.model.networks[0].id;
var height = element_properties.port_margin*(device.ports.length - 1);
device.height =
(self.draw_mode == 'normal' && height > element_properties.default_height) ? height :
(self.draw_mode === 'normal' && height > element_properties.default_height) ? height :
element_properties.default_height;
device.pos_y = self.network_height;
device.port_height =
(self.draw_mode == 'small' && height > device.height) ? 1 :
(self.draw_mode === 'small' && height > device.height) ? 1 :
element_properties.port_height;
device.port_margin =
(self.draw_mode == 'small' && height > device.height) ?
(self.draw_mode === 'small' && height > device.height) ?
device.height/device.ports.length :
element_properties.port_margin;
self.network_height += device.height + element_properties.margin;
@ -191,7 +191,7 @@ horizon.network_topology = {
network.devices = [];
$.each([model.routers, model.servers],function(index, devices) {
$.each(devices,function(index, device) {
if(network.id == device.parent_network) {
if(network.id === device.parent_network) {
network.devices.push(device);
}
});
@ -313,16 +313,16 @@ horizon.network_topology = {
.select('.name')
.text(function(d) { return self.string_truncate(d.name); });
device.each(function(d) {
if (d.status == 'BUILD') {
if (d.status === 'BUILD') {
d3.select(this).classed('loading',true);
} else if (d.task == 'deleting') {
} else if (d.task === 'deleting') {
d3.select(this).classed('loading',true);
if ('bl_' + d.id == self.balloon_id) {
if ('bl_' + d.id === self.balloon_id) {
self.delete_balloon();
}
} else {
d3.select(this).classed('loading',false);
if ('bl_' + d.id == self.balloon_id) {
if ('bl_' + d.id === self.balloon_id) {
var $this = $(this);
self.show_balloon(d,$this);
}
@ -330,7 +330,7 @@ horizon.network_topology = {
});
device.exit().each(function(d){
if ('bl_' + d.id == self.balloon_id) {
if ('bl_' + d.id === self.balloon_id) {
self.delete_balloon();
}
}).remove();
@ -376,7 +376,7 @@ horizon.network_topology = {
});
port.attr('transform',function(d,i){
var x = (this._direction == 'left') ? 0 : element_properties.device_width;
var x = (this._direction === 'left') ? 0 : element_properties.device_width;
var ports_length = this.parentNode._portdata[this._direction];
var distance = this.parentNode._portdata.port_margin;
var y = (this.parentNode._portdata.device_height -
@ -397,14 +397,14 @@ horizon.network_topology = {
var parent = this.parentNode;
var width = (Math.abs(parent._index_diff) - 1)*element_properties.network_width +
element_properties.port_width;
return (parent._direction == 'left') ? -1*width : width;
return (parent._direction === 'left') ? -1*width : width;
});
port
.select('.port_text')
.attr('x',function(d) {
var parent = this.parentNode;
if (parent._direction == 'left') {
if (parent._direction === 'left') {
d3.select(this).classed('left',true);
return element_properties.port_text_margin.x*-1;
} else {
@ -433,7 +433,7 @@ horizon.network_topology = {
},
select_port: function(device_id){
return $.map(this.model.ports,function(port, index){
if (port.device_id == device_id) {
if (port.device_id === device_id) {
return port;
}
});
@ -503,7 +503,7 @@ horizon.network_topology = {
object.router_id = port.device_id;
object.url = port.url;
object.port_status = port.status;
object.port_status_css = (port.status == "ACTIVE")? 'active' : 'down';
object.port_status_css = (port.status === "ACTIVE")? 'active' : 'down';
var ip_address = '';
try {
ip_address = port.fixed_ips[0].ip_address;
@ -518,7 +518,7 @@ horizon.network_topology = {
}
object.ip_address = ip_address;
object.device_owner = device_owner;
object.is_interface = (device_owner == 'router_interface') ? true : false;
object.is_interface = (device_owner === 'router_interface') ? true : false;
ports.push(object);
});
var html_data = {
@ -531,15 +531,15 @@ horizon.network_topology = {
return $0.toUpperCase();
}),
status:d.status,
status_class:(d.status == "ACTIVE")? 'active' : 'down'
status_class:(d.status === "ACTIVE")? 'active' : 'down'
};
if (d.type == 'router') {
if (d.type === 'router') {
html_data.port = ports;
html = balloon_tmpl.render(html_data,{
table1:device_tmpl,
table2:port_tmpl
});
} else if (d.type == 'instance') {
} else if (d.type === 'instance') {
html_data.console_id = d.id;
html_data.console = d.console;
html = balloon_tmpl.render(html_data,{

View File

@ -132,7 +132,7 @@ horizon.Quota = {
findImageById: function(id) {
_image = undefined;
$.each(horizon.Quota.images, function(i, image){
if(image.id == id) {
if(image.id === id) {
_image = image;
}
});
@ -167,7 +167,7 @@ horizon.Quota = {
// Now, disable anything from above:
$.each(to_disable, function(i, flavor_name) {
flavors.each(function(){
if ($(this).text() == flavor_name) {
if ($(this).text() === flavor_name) {
$(this).attr('disabled', 'disabled');
}
});
@ -186,7 +186,7 @@ horizon.Quota = {
}
});
}
horizon.Quota.noteDisabledFlavors(to_disable.length == flavors.length);
horizon.Quota.noteDisabledFlavors(to_disable.length === flavors.length);
}
},
@ -222,7 +222,7 @@ horizon.Quota = {
getSelectedFlavor: function() {
if(this.is_flavor_quota) {
this.selected_flavor = $.grep(this.flavors, function(flavor) {
return flavor.id == $("#id_flavor").children(":selected").val();
return flavor.id === $("#id_flavor").children(":selected").val();
})[0];
} else {
this.selected_flavor = null;
@ -421,7 +421,7 @@ horizon.Quota = {
that input, resulting in "8723".
*/
user_integer = integers_in_input.join('');
} else if(integers_in_input.length == 1) {
} else if(integers_in_input.length === 1) {
user_integer = integers_in_input[0];
}

View File

@ -75,7 +75,7 @@ horizon.datatables = {
}
// Only replace row if the html content has changed
if($new_row.html() != $row.html()) {
if($new_row.html() !== $row.html()) {
if($row.find('.table-row-multi-select:checkbox').is(':checked')) {
// Preserve the checkbox if it's already clicked
$new_row.find('.table-row-multi-select:checkbox').prop('checked', true);
@ -277,11 +277,11 @@ horizon.datatables.set_table_sorting = function (parent) {
$th = $(this);
if (!$th.hasClass('sortable')) {
header_options[i] = {sorter: false};
} else if ($th.data('type') == 'size'){
} else if ($th.data('type') === 'size'){
header_options[i] = {sorter: 'sizeSorter'};
} else if ($th.data('type') == 'ip'){
} else if ($th.data('type') === 'ip'){
header_options[i] = {sorter: 'ipAddress'};
} else if ($th.data('type') == 'timesince'){
} else if ($th.data('type') === 'timesince'){
header_options[i] = {sorter: 'timesinceSorter'};
}
});
@ -368,7 +368,7 @@ horizon.datatables.set_table_fixed_filter = function (parent) {
});
$(elm).find('div.table_filter button').each(function (i, button) {
// Select the first non-empty category
if ($(button).text().indexOf(' (0)') == -1) {
if ($(button).text().indexOf(' (0)') === -1) {
$(button).addClass('active');
$(button).trigger('click');
return false;

View File

@ -111,7 +111,7 @@ horizon.inline_edit = {
td_element.css('margin', 0).css('padding', 0);
table_cell_wrapper.css('margin', 0).css('padding', 0);
if (self.form_element.attr('type')=='checkbox'){
if (self.form_element.attr('type') === 'checkbox'){
var inline_edit_form = td_element.find(".inline-edit-form");
inline_edit_form.css('padding-top', '11px').css('padding-left', '4px');
inline_edit_form.width(width - 40);
@ -241,7 +241,7 @@ horizon.addInitFunction(function() {
});
$('table').on('keypress', '.inline-edit-form', function (evt) {
if (evt.which == 13 && !evt.shiftKey) {
if (evt.which === 13 && !evt.shiftKey) {
submit_form(evt, this);
}
});

View File

@ -20,10 +20,10 @@ horizon.user = {
var password = $("#id_password").val();
var confirm_password = $("#id_confirm_password").val();
if (password != confirm_password && $("#" + error_id).length === 0) {
if (password !== confirm_password && $("#" + error_id).length === 0) {
$(row).parent().addClass("error");
$(row).after(msg);
} else if (password == confirm_password) {
} else if (password === confirm_password) {
$(row).parent().removeClass("error");
$("#" + error_id).remove();
}

View File

@ -10,7 +10,8 @@ horizon.addInitFunction(function () {
// Proposed set of rules
//'camelcase' : true,
'indent': 2
'indent': 2,
'eqeqeq': true
//'undef': true,
//'quotmark': 'single',
//'maxlen': 80,

View File

@ -30,7 +30,7 @@ horizon.addInitFunction(function () {
safe_string = "A safe message <a>here</a>!";
message = horizon.alert("success", safe_string, "safe");
ok(message, "Create a message with extra tag.");
ok((message.html().indexOf(safe_string ) != -1), 'Verify the message with HTML tag was not escaped.');
ok((message.html().indexOf(safe_string) !== -1), 'Verify the message with HTML tag was not escaped.');
equal($('#main_content .messages .alert').length, 1, "Verify our message was added to the DOM.");
horizon.clearAllMessages();
equal($('#main_content .messages .alert').length, 0, "Verify our message was removed.");