Linting JSX files (#941)

`.jsx` linting is now in-scope for the `npm run lint` command, and
I linted the base files and some of the viz, there's still quite a bit
of work there, but that's a first pass on it.
This commit is contained in:
Maxime Beauchemin
2016-08-22 13:21:30 -07:00
committed by GitHub
parent 7f2805a3c5
commit a8fd23dfa4
26 changed files with 1443 additions and 1425 deletions

View File

@@ -4,9 +4,9 @@
//
// js
const $ = window.$ = require('jquery');
const jQuery = window.jQuery = $;
const px = require('./../modules/caravel.js');
const showModal = require('./../modules/utils.js').showModal;
const jQuery = window.jQuery = require('jquery'); // eslint-disable-line
import React from 'react';
import ReactDOM from 'react-dom';
@@ -76,23 +76,55 @@ function query(forceUpdate, pushState) {
slice.render(force);
}
function initExploreView() {
function get_collapsed_fieldsets() {
var collapsed_fieldsets = $('#collapsed_fieldsets').val();
if (collapsed_fieldsets !== undefined && collapsed_fieldsets !== '') {
collapsed_fieldsets = collapsed_fieldsets.split('||');
} else {
collapsed_fieldsets = [];
function saveSlice() {
const action = $('input[name=rdo_save]:checked').val();
if (action === 'saveas') {
const sliceName = $('input[name=new_slice_name]').val();
if (sliceName === '') {
showModal({
title: 'Error',
body: 'You must pick a name for the new slice',
});
return;
}
return collapsed_fieldsets;
document.getElementById('slice_name').value = sliceName;
}
const addToDash = $('input[name=addToDash]:checked').val();
if (addToDash === 'existing' && $('#save_to_dashboard_id').val() === '') {
showModal({
title: 'Error',
body: 'You must pick an existing dashboard',
});
return;
} else if (addToDash === 'new' && $('input[name=new_dashboard_name]').val() === '') {
showModal({
title: 'Error',
body: 'Please enter a name for the new dashboard',
});
return;
}
$('#action').val(action);
prepForm();
$('#query').submit();
}
function initExploreView() {
function getCollapsedFieldsets() {
let collapsedFieldsets = $('#collapsedFieldsets').val();
if (collapsedFieldsets !== undefined && collapsedFieldsets !== '') {
collapsedFieldsets = collapsedFieldsets.split('||');
} else {
collapsedFieldsets = [];
}
return collapsedFieldsets;
}
function toggle_fieldset(legend, animation) {
var parent = legend.parent();
var fieldset = parent.find('.legend_label').text();
var collapsed_fieldsets = get_collapsed_fieldsets();
var index;
function toggleFieldset(legend, animation) {
const parent = legend.parent();
const fieldset = parent.find('.legend_label').text();
const collapsedFieldsets = getCollapsedFieldsets();
let index;
if (parent.hasClass('collapsed')) {
if (animation) {
@@ -104,9 +136,9 @@ function initExploreView() {
parent.find('span.collapser').text('[-]');
// removing from array, js is overcomplicated
index = collapsed_fieldsets.indexOf(fieldset);
index = collapsedFieldsets.indexOf(fieldset);
if (index !== -1) {
collapsed_fieldsets.splice(index, 1);
collapsedFieldsets.splice(index, 1);
}
} else { // not collapsed
if (animation) {
@@ -117,33 +149,33 @@ function initExploreView() {
parent.addClass('collapsed');
parent.find('span.collapser').text('[+]');
index = collapsed_fieldsets.indexOf(fieldset);
index = collapsedFieldsets.indexOf(fieldset);
if (index === -1 && fieldset !== '' && fieldset !== undefined) {
collapsed_fieldsets.push(fieldset);
collapsedFieldsets.push(fieldset);
}
}
$('#collapsed_fieldsets').val(collapsed_fieldsets.join('||'));
$('#collapsedFieldsets').val(collapsedFieldsets.join('||'));
}
px.initFavStars();
function copyURLToClipboard(url) {
var textArea = document.createElement('textarea');
const textArea = document.createElement('textarea');
textArea.style.position = 'fixed';
textArea.style.left = '-1000px';
textArea.value = url;
document.body.appendChild(textArea);
textArea.select();
let successful;
try {
var successful = document.execCommand('copy');
successful = document.execCommand('copy');
if (!successful) {
throw new Error('Not successful');
}
} catch (err) {
window.alert('Sorry, your browser does not support copying. Use Ctrl / Cmd + C!');
window.alert('Sorry, your browser does not support copying. Use Ctrl / Cmd + C!'); // eslint-disable-line
}
document.body.removeChild(textArea);
return successful;
@@ -157,12 +189,20 @@ function initExploreView() {
data: '/' + window.location.pathname + slice.querystring(),
},
success(data) {
var close = '<a style="cursor: pointer;"><i class="fa fa-close" id="close_shortner"></i></a>';
var copy = '<a style="cursor: pointer;"><i class="fa fa-clipboard" title="Copy to clipboard" id="copy_url"></i></a>';
var spaces = '&nbsp;&nbsp;&nbsp;';
var popover = data + spaces + copy + spaces + close;
const close = (
'<a style="cursor: pointer;">' +
'<i class="fa fa-close" id="close_shortner"></i>' +
'</a>'
);
const copy = (
'<a style="cursor: pointer;">' +
'<i class="fa fa-clipboard" title="Copy to clipboard" id="copy_url"></i>' +
'</a>'
);
const spaces = '&nbsp;&nbsp;&nbsp;';
const popover = data + spaces + copy + spaces + close;
var $shortner = $('#shortner')
const $shortner = $('#shortner')
.popover({
content: popover,
placement: 'left',
@@ -170,42 +210,57 @@ function initExploreView() {
trigger: 'manual',
})
.popover('show');
function destroyPopover() {
$shortner.popover('destroy');
}
$('#copy_url').tooltip().click(function () {
var success = copyURLToClipboard(data);
$('#copy_url')
.tooltip()
.click(function () {
const success = copyURLToClipboard(data);
if (success) {
$(this).attr('data-original-title', 'Copied!').tooltip('fixTitle').tooltip('show');
$(this).attr('data-original-title', 'Copied!')
.tooltip('fixTitle')
.tooltip('show');
window.setTimeout(destroyPopover, 1200);
}
});
$('#close_shortner').click(destroyPopover);
function destroyPopover() {
$shortner.popover('destroy');
}
},
error(error) {
showModal({
title: 'Error',
body: 'Sorry, an error occurred during this operation:<br/>' + error,
});
console.warn('Short URL error', error);
},
});
});
$('#standalone').click(function () {
var src_link = window.location.origin + slice.data.standalone_endpoint;
var dataToCopy = '';
var close = '<a style="cursor: pointer;"><i class="fa fa-close" id="close_standalone"></i></a>';
var copy = '<a style="cursor: pointer;"><i class="fa fa-clipboard" title="Copy to clipboard" id="copy_embed"></i></a>';
var spaces = '&nbsp;&nbsp;&nbsp;';
var widthInput = '<input type="number" id="standalone_width" placeholder="width">';
var heightInput = '<input type="number" id="standalone_height" placeholder="height">';
var popover = "<input id='standalone_text' value='' disabled></input>";
const srcLink = window.location.origin + slice.data.standalone_endpoint;
const close = (
'<a style="cursor: pointer;">' +
'<i class="fa fa-close" id="close_standalone"></i>' +
'</a>'
);
const copy = (
'<a style="cursor: pointer;">' +
'<i class="fa fa-clipboard" title="Copy to clipboard" id="copy_embed"></i>' +
'</a>'
);
const spaces = '&nbsp;&nbsp;&nbsp;';
const widthInput = '<input type="number" id="standalone_width" placeholder="width">';
const heightInput = '<input type="number" id="standalone_height" placeholder="height">';
let popover = "<input id='standalone_text' value='' disabled></input>";
popover = popover + spaces + copy + spaces + close + spaces + widthInput + spaces + heightInput;
let dataToCopy = '';
const $standalone = $(this);
function destroyPopover() {
$standalone.popover('destroy');
}
var $standalone = $(this);
$standalone.popover({
content: popover,
title: 'embed html',
@@ -215,37 +270,36 @@ function initExploreView() {
})
.popover('show');
$('#copy_embed').tooltip().click(function () {
var success = copyURLToClipboard(dataToCopy);
const success = copyURLToClipboard(dataToCopy);
if (success) {
$(this).attr('data-original-title', 'Copied!').tooltip('fixTitle').tooltip('show');
$(this).attr('data-original-title', 'Copied!')
.tooltip('fixTitle')
.tooltip('show');
window.setTimeout(destroyPopover, 1200);
}
});
$('#close_standalone').click(destroyPopover);
function destroyPopover() {
$standalone.popover('destroy');
const $standaloneWidth = $('#standalone_width');
const $standaloneHeight = $('#standalone_height');
const $standaloneText = $('#standalone_text');
function generateEmbedHTML() {
const width = $standaloneWidth.val();
const height = $standaloneHeight.val();
dataToCopy = `<iframe src="${srcLink}" width="${width}" height="${height}"`;
dataToCopy = dataToCopy + ' seamless frameBorder="0" scrolling="no"></iframe>';
$standaloneText.val(dataToCopy);
}
var $standalone_width = $('#standalone_width');
var $standalone_height = $('#standalone_height');
var $standalone_text = $('#standalone_text');
$standalone_height.change(function () {
$standaloneHeight.change(function () {
generateEmbedHTML();
});
$standalone_width.change(function () {
$standaloneWidth.change(function () {
generateEmbedHTML();
});
generateEmbedHTML();
function generateEmbedHTML() {
var width = $standalone_width.val();
var height = $standalone_height.val();
dataToCopy = '<iframe src="' + src_link + '" width="' + width + '" height="' + height + '"';
dataToCopy = dataToCopy + ' seamless frameBorder="0" scrolling="no"></iframe>';
$standalone_text.val(dataToCopy);
}
});
$('#viz_type').change(function () {
@@ -253,20 +307,19 @@ function initExploreView() {
});
$('#datasource_id').change(function () {
var url = $(this).find('option:selected').attr('url');
window.location = url;
window.location = $(this).find('option:selected').attr('url');
});
var collapsed_fieldsets = get_collapsed_fieldsets();
for (var i = 0; i < collapsed_fieldsets.length; i++) {
toggle_fieldset($('legend:contains("' + collapsed_fieldsets[i] + '")'), false);
const collapsedFieldsets = getCollapsedFieldsets();
for (let i = 0; i < collapsedFieldsets.length; i++) {
toggleFieldset($('legend:contains("' + collapsedFieldsets[i] + '")'), false);
}
function formatViz(viz) {
var url = '/static/assets/images/viz_thumbnails/' + viz.id + '.png';
var no_img = '/static/assets/images/noimg.png';
const url = `/static/assets/images/viz_thumbnails/${viz.id}.png`;
const noImg = '/static/assets/images/noimg.png';
return $(
'<img class="viz-thumb-option" src="' + url + '" onerror="this.src=\'' + no_img + '\';">' +
'<span>' + viz.text + '</span>'
`<img class="viz-thumb-option" src="${url}" onerror="this.src='${noImg}';">` +
`<span>${viz.text}</span>`
);
}
@@ -288,20 +341,8 @@ function initExploreView() {
$('[data-toggle="tooltip"]').tooltip({ container: 'body' });
$('.ui-helper-hidden-accessible').remove(); // jQuery-ui 1.11+ creates a div for every tooltip
function set_filters() {
['flt', 'having'].forEach(function (prefix) {
for (var i = 1; i < 10; i++) {
var col = px.getParam(prefix + '_col_' + i);
if (col !== '') {
add_filter(i, prefix);
}
}
});
}
set_filters();
function add_filter(i, fieldPrefix) {
var cp = $('#' + fieldPrefix + '0').clone();
function addFilter(i, fieldPrefix) {
const cp = $('#' + fieldPrefix + '0').clone();
$(cp).appendTo('#' + getPanelClass(fieldPrefix) + ' #filters');
$(cp).show();
if (i !== undefined) {
@@ -311,23 +352,38 @@ function initExploreView() {
}
$(cp).find('select').select2();
$(cp).find('.remove').click(function () {
$(this).parent().parent().remove();
$(this)
.parent()
.parent()
.remove();
});
}
$(window).bind('popstate', function (event) {
function setFilters() {
['flt', 'having'].forEach(function (prefix) {
for (let i = 1; i < 10; i++) {
const col = px.getParam(prefix + '_col_' + i);
if (col !== '') {
addFilter(i, prefix);
}
}
});
}
setFilters();
$(window).bind('popstate', function () {
// Browser back button
var returnLocation = history.location || document.location;
const returnLocation = history.location || document.location;
// Could do something more lightweight here, but we're not optimizing
// for the use of the back button anyways
returnLocation.reload();
});
$('#filter_panel #plus').click(function () {
add_filter(undefined, 'flt');
addFilter(undefined, 'flt');
});
$('#having_panel #plus').click(function () {
add_filter(undefined, 'having');
addFilter(undefined, 'having');
});
const queryAndSaveBtnsEl = document.getElementById('js-query-and-save-btns');
@@ -339,8 +395,8 @@ function initExploreView() {
queryAndSaveBtnsEl
);
function create_choices(term, data) {
var filtered = $(data).filter(function () {
function createChoices(term, data) {
const filtered = $(data).filter(function () {
return this.text.localeCompare(term) === 0;
});
if (filtered.length === 0) {
@@ -349,6 +405,7 @@ function initExploreView() {
text: term,
};
}
return {};
}
function initSelectionToValue(element, callback) {
@@ -359,11 +416,11 @@ function initExploreView() {
}
$('.select2_freeform').each(function () {
var parent = $(this).parent();
var name = $(this).attr('name');
var l = [];
var selected = '';
for (var i = 0; i < this.options.length; i++) {
const parent = $(this).parent();
const name = $(this).attr('name');
const l = [];
let selected = '';
for (let i = 0; i < this.options.length; i++) {
l.push({
id: this.options[i].value,
text: this.options[i].text,
@@ -373,10 +430,11 @@ function initExploreView() {
}
}
parent.append(
'<input class="' + $(this).attr('class') + '" name="' + name + '" type="text" value="' + selected + '">'
`<input class="${$(this).attr('class')}" ` +
`name="${name}" type="text" value="${selected}">`
);
$("input[name='" + name + "']").select2({
createSearchChoice: create_choices,
$(`input[name='${name}']`).select2({
createSearchChoice: createChoices,
initSelection: initSelectionToValue,
dropdownAutoWidth: true,
multiple: false,
@@ -386,31 +444,30 @@ function initExploreView() {
});
function prepSaveDialog() {
var setButtonsState = function () {
var add_to_dash = $('input[name=add_to_dash]:checked').val();
if (add_to_dash === 'existing' || add_to_dash === 'new') {
const setButtonsState = function () {
const addToDash = $('input[name=addToDash]:checked').val();
if (addToDash === 'existing' || addToDash === 'new') {
$('.gotodash').removeAttr('disabled');
} else {
$('.gotodash').prop('disabled', true);
}
};
var url = '/dashboardmodelviewasync/api/read';
url += '?_flt_0_owners=' + $('#userid').val();
const url = '/dashboardmodelviewasync/api/read?_flt_0_owners=' + $('#userid').val();
$.get(url, function (data) {
var choices = [];
for (var i = 0; i < data.pks.length; i++) {
const choices = [];
for (let i = 0; i < data.pks.length; i++) {
choices.push({ id: data.pks[i], text: data.result[i].dashboard_title });
}
$('#save_to_dashboard_id').select2({
data: choices,
dropdownAutoWidth: true,
}).on('select2-selecting', function () {
$('#add_to_dash_existing').prop('checked', true);
$('#addToDash_existing').prop('checked', true);
setButtonsState();
});
});
$('input[name=add_to_dash]').change(setButtonsState);
$('input[name=addToDash]').change(setButtonsState);
$("input[name='new_dashboard_name']").on('focus', function () {
$('#add_to_new_dash').prop('checked', true);
setButtonsState();
@@ -430,38 +487,6 @@ function initExploreView() {
prepSaveDialog();
}
function saveSlice() {
var action = $('input[name=rdo_save]:checked').val();
if (action === 'saveas') {
var slice_name = $('input[name=new_slice_name]').val();
if (slice_name === '') {
showModal({
title: 'Error',
body: 'You must pick a name for the new slice',
});
return;
}
document.getElementById('slice_name').value = slice_name;
}
var add_to_dash = $('input[name=add_to_dash]:checked').val();
if (add_to_dash === 'existing' && $('#save_to_dashboard_id').val() === '') {
showModal({
title: 'Error',
body: 'You must pick an existing dashboard',
});
return;
} else if (add_to_dash === 'new' && $('input[name=new_dashboard_name]').val() === '') {
showModal({
title: 'Error',
body: 'Please enter a name for the new dashboard',
});
return;
}
$('#action').val(action);
prepForm();
$('#query').submit();
}
$(document).ready(function () {
const data = $('.slice').data('slice');