Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearXNG JavaScript Style Guide #538

Merged
merged 5 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

(json-mode
. ((eval . (progn
(setq-local js-indent-level 2)
(setq-local js-indent-level 4)
(flycheck-checker . json-python-json)))))

(js-mode
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ MANAGE += py.build py.clean
MANAGE += pyenv pyenv.install pyenv.uninstall
MANAGE += pypi.upload pypi.upload.test
MANAGE += test.yamllint test.pylint test.pep8 test.unit test.coverage test.robot test.clean
MANAGE += themes.all themes.oscar themes.simple pygments.less
MANAGE += themes.all themes.oscar themes.simple themes.simple.test pygments.less
MANAGE += static.build.commit static.build.drop static.build.restore
MANAGE += nvm.install nvm.clean nvm.status nvm.nodejs

Expand Down
11 changes: 11 additions & 0 deletions manage
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ themes.simple() {
dump_return $?
}

themes.simple.test() {
build_msg TEST "theme: simple"
if ! nvm.min_node "${NODE_MINIMUM_VERSION}"; then
info_msg "install Node.js by NVM"
nvm.nodejs
fi
npm --prefix searx/static/themes/simple install
npm --prefix searx/static/themes/simple run test
dump_return $?
}

PYLINT_FILES=()
while IFS= read -r line; do
PYLINT_FILES+=("$line")
Expand Down
8 changes: 4 additions & 4 deletions searx/static/themes/__common__/js/image_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


(function (w, d) {
function ImageLayout(container_selector, results_selector, img_selector, verticalMargin, horizontalMargin, maxHeight) {
function ImageLayout (container_selector, results_selector, img_selector, verticalMargin, horizontalMargin, maxHeight) {
this.container_selector = container_selector;
this.results_selector = results_selector;
this.img_selector = img_selector;
Expand Down Expand Up @@ -57,7 +57,7 @@
}
}

return (width - images.length * this.verticalMargin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
return (width - images.length * this.verticalMargin) / r; // have to round down because Firefox will automatically roundup value with number of decimals > 3
};

ImageLayout.prototype._setSize = function (images, height) {
Expand Down Expand Up @@ -147,12 +147,12 @@
var results_nodes = d.querySelectorAll(this.results_selector);
var results_length = results_nodes.length;

function img_load_error(event) {
function img_load_error (event) {
// console.log("ERROR can't load: " + event.originalTarget.src);
event.originalTarget.src = w.searxng.static_path + w.searxng.theme.img_load_error;
}

function throttleAlign() {
function throttleAlign () {
if (obj.isAlignDone) {
obj.isAlignDone = false;
setTimeout(function () {
Expand Down
16 changes: 16 additions & 0 deletions searx/static/themes/simple/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,21 @@
"ecmaVersion": 12
},
"rules": {
"indent": ["error", 2],
return42 marked this conversation as resolved.
Show resolved Hide resolved
"keyword-spacing": ["error", { "before": true, "after": true }],
"no-trailing-spaces": 2,
"space-before-function-paren": ["error", "always"],
"space-infix-ops": "error",
"comma-spacing": ["error", { "before": false, "after": true }],
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"curly": ["error", "multi-line"],
"block-spacing": ["error", "always"],
"comma-spacing": ["error", { "before": false, "after": true }],
"dot-location": ["error", "property"],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
"spaced-comment": ["error", "always", {
"line": { "markers": ["*package", "!", "/", ",", "="] },
"block": { "balanced": true, "markers": ["*package", "!", ",", ":", "::", "flow-include"], "exceptions": ["*"] }
}]
}
}
71 changes: 36 additions & 35 deletions searx/static/themes/simple/gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*jshint esversion: 6 */
/* SPDX-License-Identifier: AGPL-3.0-or-later */

module.exports = function(grunt) {
module.exports = function (grunt) {

const eachAsync = require('each-async');

Expand All @@ -27,9 +27,11 @@ module.exports = function(grunt) {
eslint: {
options: {
overrideConfigFile: '.eslintrc.json',
failOnError: false
failOnError: true,
fix: grunt.option('fix')
},
target: [
'gruntfile.js',
'svg4web.svgo.js',
'src/js/main/*.js',
'src/js/head/*.js',
Expand Down Expand Up @@ -111,7 +113,7 @@ module.exports = function(grunt) {
new (require('less-plugin-clean-css'))()
],
sourceMap: true,
sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map';},
sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map'; },
outputSourceFiles: true,
},
files: {
Expand Down Expand Up @@ -176,37 +178,37 @@ module.exports = function(grunt) {
},
});

grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function() {
grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function () {
const ejs = require('ejs'), svgo = require('svgo');
const icons = {}
for(const iconName in this.data.src) {
const svgFileName = this.data.src[iconName];
try {
const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
const svgoResult = svgo.optimize(svgContent, {
path: svgFileName,
multipass: true,
plugins: [
{
name: "removeTitle",
},
{
name: "removeXMLNS",
},
{
name: "addAttributesToSVGElement",
params: {
attributes: [
{ "aria-hidden": "true" }
]
}
}
]
});
icons[iconName] = svgoResult.data.replace("'", "\\'");
} catch (err) {
grunt.log.error(err);
}
for (const iconName in this.data.src) {
const svgFileName = this.data.src[iconName];
try {
const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
const svgoResult = svgo.optimize(svgContent, {
path: svgFileName,
multipass: true,
plugins: [
{
name: "removeTitle",
},
{
name: "removeXMLNS",
},
{
name: "addAttributesToSVGElement",
params: {
attributes: [
{ "aria-hidden": "true" }
]
}
}
]
});
icons[iconName] = svgoResult.data.replace("'", "\\'");
} catch (err) {
grunt.log.error(err);
}
}
const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
{%- set icons = {
Expand Down Expand Up @@ -266,13 +268,12 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-image');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-stylelint');
grunt.loadNpmTasks('grunt-eslint');

grunt.registerTask('test', ['jshint']);
grunt.registerTask('test', ['eslint']);

grunt.registerTask('default', [
'eslint',
Expand Down
3 changes: 2 additions & 1 deletion searx/static/themes/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"grunt": "~1.4.1",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-cssmin": "^4.0.0",
"grunt-contrib-jshint": "~3.1.1",
"grunt-contrib-less": "~3.0.0",
"grunt-contrib-uglify": "~5.0.1",
"grunt-xmlmin": "~0.1.8",
Expand All @@ -29,7 +28,9 @@
"scripts": {
"all": "npm install && grunt",
"build": "grunt",
"test": "grunt test",
"eslint": "grunt eslint",
"eslint-fix": "grunt eslint --fix",
"watch": "grunt watch",
"webfont": "grunt webfont",
"clean": "rm -Rf node_modules package-lock.json ion.less",
Expand Down
58 changes: 29 additions & 29 deletions searx/static/themes/simple/src/js/head/00_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/* global DocumentTouch:readonly */
(function(w, d) {
'use strict';
(function (w, d) {
'use strict';

// add data- properties
var script = d.currentScript || (function() {
var scripts = d.getElementsByTagName('script');
return scripts[scripts.length - 1];
})();
// add data- properties
var script = d.currentScript || (function () {
var scripts = d.getElementsByTagName('script');
return scripts[scripts.length - 1];
})();

// try to detect touch screen
w.searxng = {
touch: (("ontouchstart" in w) || w.DocumentTouch && document instanceof DocumentTouch) || false,
method: script.getAttribute('data-method'),
autocompleter: script.getAttribute('data-autocompleter') === 'true',
search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true',
infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true',
hotkeys: script.getAttribute('data-hotkeys') === 'true',
static_path: script.getAttribute('data-static-path'),
translations: JSON.parse(script.getAttribute('data-translations')),
theme : {
// image that is displayed if load of <img src='...'> failed
img_load_error: 'img/img_load_error.svg'
}
};

// update the css
var hmtlElement = d.getElementsByTagName("html")[0];
hmtlElement.classList.remove('no-js');
hmtlElement.classList.add('js');
if (w.searxng.touch) {
hmtlElement.classList.add('touch');
// try to detect touch screen
w.searxng = {
touch: (("ontouchstart" in w) || w.DocumentTouch && document instanceof DocumentTouch) || false,
method: script.getAttribute('data-method'),
autocompleter: script.getAttribute('data-autocompleter') === 'true',
search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true',
infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true',
hotkeys: script.getAttribute('data-hotkeys') === 'true',
static_path: script.getAttribute('data-static-path'),
translations: JSON.parse(script.getAttribute('data-translations')),
theme: {
// image that is displayed if load of <img src='...'> failed
img_load_error: 'img/img_load_error.svg'
}
};

// update the css
var hmtlElement = d.getElementsByTagName("html")[0];
hmtlElement.classList.remove('no-js');
hmtlElement.classList.add('js');
if (w.searxng.touch) {
hmtlElement.classList.add('touch');
}
})(window, document);