From e687f5c290d30d32c9c32c4ae5c7b9085697ab87 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sun, 30 Nov 2014 13:21:58 +0100 Subject: [PATCH] [enh][oscar_template] split js-files and compile them together --- .gitignore | 1 + .travis.yml | 2 + Makefile | 4 + searx/static/oscar/.gitignore | 1 + searx/static/oscar/README.rst | 17 +++ searx/static/oscar/gruntfile.js | 51 +++++++++ searx/static/oscar/js/searx.min.js | 2 + .../oscar/js/searx_src/00_requirejs_config.js | 23 ++++ .../oscar/js/searx_src/autocompleter.js | 37 ++++++ .../oscar/js/searx_src/element_modifiers.js | 66 +++++++++++ .../{scripts.js => searx_src/leaflet_map.js} | 106 +++++------------- searx/static/oscar/package.json | 15 +++ searx/templates/oscar/base.html | 2 +- searx/templates/oscar/search_full.html | 2 +- 14 files changed, 248 insertions(+), 81 deletions(-) create mode 100644 searx/static/oscar/.gitignore create mode 100644 searx/static/oscar/README.rst create mode 100644 searx/static/oscar/gruntfile.js create mode 100644 searx/static/oscar/js/searx.min.js create mode 100644 searx/static/oscar/js/searx_src/00_requirejs_config.js create mode 100644 searx/static/oscar/js/searx_src/autocompleter.js create mode 100644 searx/static/oscar/js/searx_src/element_modifiers.js rename searx/static/oscar/js/{scripts.js => searx_src/leaflet_map.js} (69%) create mode 100644 searx/static/oscar/package.json diff --git a/.gitignore b/.gitignore index 0f347d81a..08cf582aa 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ setup.cfg *.pyc */*.pyc +*~ bin/ build/ diff --git a/.travis.yml b/.travis.yml index 1bf0be330..ec2a3ca3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ before_install: - "export DISPLAY=:99.0" - "sh -e /etc/init.d/xvfb start" - npm install -g less + - cd searx/static/oscar;npm install install: - "make" - pip install coveralls @@ -12,6 +13,7 @@ script: - "make tests" - "make robot" - "make styles" + - "make grunt" - make coverage after_success: coveralls diff --git a/Makefile b/Makefile index 54f8a10fa..f9882e5ac 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ $(python): tests: .installed.cfg @bin/test + @grunt test --gruntfile searx/static/oscar/gruntfile.js robot: .installed.cfg @bin/robot @@ -48,6 +49,9 @@ styles: @lessc -x searx/static/oscar/less/bootstrap/bootstrap.less > searx/static/oscar/css/bootstrap.min.css @lessc -x searx/static/oscar/less/oscar/oscar.less > searx/static/oscar/css/oscar.min.css +grunt: + @grunt --gruntfile searx/static/oscar/gruntfile.js + locales: @pybabel compile -d searx/translations diff --git a/searx/static/oscar/.gitignore b/searx/static/oscar/.gitignore new file mode 100644 index 000000000..c2658d7d1 --- /dev/null +++ b/searx/static/oscar/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/searx/static/oscar/README.rst b/searx/static/oscar/README.rst new file mode 100644 index 000000000..7a1800add --- /dev/null +++ b/searx/static/oscar/README.rst @@ -0,0 +1,17 @@ +install dependencies +~~~~~~~~~~~~~~~~~~~~ + +run this command in the directory ``searx/static/oscar`` + +``npm install`` + +compile sources +~~~~~~~~~~~~~~~ + +run this command in the directory ``searx/static/oscar`` + +``grunt`` + +or in the root directory: + +``make grunt`` diff --git a/searx/static/oscar/gruntfile.js b/searx/static/oscar/gruntfile.js new file mode 100644 index 000000000..79da491c4 --- /dev/null +++ b/searx/static/oscar/gruntfile.js @@ -0,0 +1,51 @@ +module.exports = function(grunt) { + + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + concat: { + options: { + separator: ';' + }, + dist: { + src: ['js/searx_src/*.js'], + dest: 'js/searx.js' + } + }, + uglify: { + options: { + banner: '/*! oscar/searx.min.js | <%= grunt.template.today("dd-mm-yyyy") %> | https://github.com/asciimoo/searx */\n' + }, + dist: { + files: { + 'js/searx.min.js': ['<%= concat.dist.dest %>'] + } + } + }, + jshint: { + files: ['gruntfile.js', 'js/searx_src/*.js'], + options: { + // options here to override JSHint defaults + globals: { + jQuery: true, + console: true, + module: true, + document: true + } + } + }, + watch: { + files: ['<%= jshint.files %>'], + tasks: ['jshint'] + } + }); + + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-watch'); + grunt.loadNpmTasks('grunt-contrib-concat'); + + grunt.registerTask('test', ['jshint']); + + grunt.registerTask('default', ['jshint', 'concat', 'uglify']); + +}; diff --git a/searx/static/oscar/js/searx.min.js b/searx/static/oscar/js/searx.min.js new file mode 100644 index 000000000..69cb816ae --- /dev/null +++ b/searx/static/oscar/js/searx.min.js @@ -0,0 +1,2 @@ +/*! oscar/searx.min.js | 30-11-2014 | https://github.com/asciimoo/searx */ +requirejs.config({baseUrl:"/static/oscar/js",paths:{app:"../app"}}),searx.autocompleter&&(searx.searchResults=new Bloodhound({datumTokenizer:Bloodhound.tokenizers.obj.whitespace("value"),queryTokenizer:Bloodhound.tokenizers.whitespace,remote:"/autocompleter?q=%QUERY"}),searx.searchResults.initialize()),$(document).ready(function(){searx.autocompleter&&$("#q").typeahead(null,{name:"search-results",displayKey:function(a){return a},source:searx.searchResults.ttAdapter()})}),$(document).ready(function(){$("#q.autofocus").focus(),$(".select-all-on-click").click(function(){$(this).select()}),$(".btn-collapse").click(function(){var a=$(this).data("btn-text-collapsed"),b=$(this).data("btn-text-not-collapsed");""!==a&&""!==b&&(new_html=$(this).hasClass("collapsed")?$(this).html().replace(a,b):$(this).html().replace(b,a),$(this).html(new_html))}),$(".btn-toggle .btn").click(function(){var a="btn-"+$(this).data("btn-class"),b=$(this).data("btn-label-default"),c=$(this).data("btn-label-toggled");""!==c&&(new_html=$(this).hasClass("btn-default")?$(this).html().replace(b,c):$(this).html().replace(c,b),$(this).html(new_html)),$(this).toggleClass(a),$(this).toggleClass("btn-default")})}),$(document).ready(function(){$(".searx_overpass_request").on("click",function(a){var b="https://overpass-api.de/api/interpreter?data=",c=b+"[out:json][timeout:25];(",d=");out meta;",e=$(this).data("osm-id"),f=$(this).data("osm-type"),g=$(this).data("result-table"),h="#"+$(this).data("result-table-loadicon"),i=["addr:city","addr:country","addr:housenumber","addr:postcode","addr:street"];if(e&&f&&g){g="#"+g;var j=null;switch(f){case"node":j=c+"node("+e+");"+d;break;case"way":j=c+"way("+e+");"+d;break;case"relation":j=c+"relation("+e+");"+d}if(j){$.ajax(j).done(function(a){if(a&&a.elements&&a.elements[0]){var b=a.elements[0],c=$(g).html();for(var d in b.tags)if(null===b.tags.name||-1==i.indexOf(d)){switch(c+=""+d+"",d){case"phone":case"fax":c+=''+b.tags[d]+"";break;case"email":c+=''+b.tags[d]+"";break;case"website":case"url":c+=''+b.tags[d]+"";break;case"wikidata":c+=''+b.tags[d]+"";break;case"wikipedia":if(-1!=b.tags[d].indexOf(":")){c+=''+b.tags[d]+"";break}default:c+=b.tags[d]}c+=""}$(g).html(c),$(g).removeClass("hidden"),$(h).addClass("hidden")}}).fail(function(){$(h).html($(h).html()+'

could not load data!

')})}}$(this).off(a)}),$(".searx_init_map").on("click",function(a){var b=$(this).data("leaflet-target"),c=$(this).data("map-lon"),d=$(this).data("map-lat"),e=$(this).data("map-zoom"),f=$(this).data("map-boundingbox"),g=$(this).data("map-geojson");require(["leaflet-0.7.3.min"],function(){f&&(southWest=L.latLng(f[0],f[2]),northEast=L.latLng(f[1],f[3]),map_bounds=L.latLngBounds(southWest,northEast)),L.Icon.Default.imagePath="/static/oscar/img/map";{var a=L.map(b),h="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",i='Map data © OpenStreetMap contributors',j=new L.TileLayer(h,{minZoom:1,maxZoom:19,attribution:i}),k="http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg",l='Map data © OpenStreetMap contributors | Tiles Courtesy of MapQuest ',m=new L.TileLayer(k,{minZoom:1,maxZoom:18,subdomains:"1234",attribution:l}),n="http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg",o='Map data © OpenStreetMap contributors | Tiles Courtesy of MapQuest | Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency';new L.TileLayer(n,{minZoom:1,maxZoom:11,subdomains:"1234",attribution:o})}map_bounds?setTimeout(function(){a.fitBounds(map_bounds,{maxZoom:17})},0):c&&d&&(e?a.setView(new L.LatLng(d,c),e):a.setView(new L.LatLng(d,c),8)),a.addLayer(m);var p={"OSM Mapnik":j,MapQuest:m};L.control.layers(p).addTo(a),g&&L.geoJson(g).addTo(a)}),$(this).off(a)})}); \ No newline at end of file diff --git a/searx/static/oscar/js/searx_src/00_requirejs_config.js b/searx/static/oscar/js/searx_src/00_requirejs_config.js new file mode 100644 index 000000000..36767843b --- /dev/null +++ b/searx/static/oscar/js/searx_src/00_requirejs_config.js @@ -0,0 +1,23 @@ +/** + * searx is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * searx is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with searx. If not, see < http://www.gnu.org/licenses/ >. + * + * (C) 2014 by Thomas Pointhuber, + */ + +requirejs.config({ + baseUrl: '/static/oscar/js', + paths: { + app: '../app' + } +}); diff --git a/searx/static/oscar/js/searx_src/autocompleter.js b/searx/static/oscar/js/searx_src/autocompleter.js new file mode 100644 index 000000000..561bff35a --- /dev/null +++ b/searx/static/oscar/js/searx_src/autocompleter.js @@ -0,0 +1,37 @@ +/** + * searx is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * searx is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with searx. If not, see < http://www.gnu.org/licenses/ >. + * + * (C) 2014 by Thomas Pointhuber, + */ + +if(searx.autocompleter) { + searx.searchResults = new Bloodhound({ + datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), + queryTokenizer: Bloodhound.tokenizers.whitespace, + remote: '/autocompleter?q=%QUERY' + }); + searx.searchResults.initialize(); +} + +$(document).ready(function(){ + if(searx.autocompleter) { + $('#q').typeahead(null, { + name: 'search-results', + displayKey: function(result) { + return result; + }, + source: searx.searchResults.ttAdapter() + }); + } +}); diff --git a/searx/static/oscar/js/searx_src/element_modifiers.js b/searx/static/oscar/js/searx_src/element_modifiers.js new file mode 100644 index 000000000..088bd7a46 --- /dev/null +++ b/searx/static/oscar/js/searx_src/element_modifiers.js @@ -0,0 +1,66 @@ +/** + * searx is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * searx is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with searx. If not, see < http://www.gnu.org/licenses/ >. + * + * (C) 2014 by Thomas Pointhuber, + */ + +$(document).ready(function(){ + /** + * focus element if class="autofocus" and id="q" + */ + $('#q.autofocus').focus(); + + /** + * select full content on click if class="select-all-on-click" + */ + $(".select-all-on-click").click(function () { + $(this).select(); + }); + + /** + * change text during btn-collapse click if possible + */ + $('.btn-collapse').click(function() { + var btnTextCollapsed = $(this).data('btn-text-collapsed'); + var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed'); + + if(btnTextCollapsed !== '' && btnTextNotCollapsed !== '') { + if($(this).hasClass('collapsed')) { + new_html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed); + } else { + new_html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed); + } + $(this).html(new_html); + } + }); + + /** + * change text during btn-toggle click if possible + */ + $('.btn-toggle .btn').click(function() { + var btnClass = 'btn-' + $(this).data('btn-class'); + var btnLabelDefault = $(this).data('btn-label-default'); + var btnLabelToggled = $(this).data('btn-label-toggled'); + if(btnLabelToggled !== '') { + if($(this).hasClass('btn-default')) { + new_html = $(this).html().replace(btnLabelDefault, btnLabelToggled); + } else { + new_html = $(this).html().replace(btnLabelToggled, btnLabelDefault); + } + $(this).html(new_html); + } + $(this).toggleClass(btnClass); + $(this).toggleClass('btn-default'); + }); +}); diff --git a/searx/static/oscar/js/scripts.js b/searx/static/oscar/js/searx_src/leaflet_map.js similarity index 69% rename from searx/static/oscar/js/scripts.js rename to searx/static/oscar/js/searx_src/leaflet_map.js index bc671e309..88af1e712 100644 --- a/searx/static/oscar/js/scripts.js +++ b/searx/static/oscar/js/searx_src/leaflet_map.js @@ -1,74 +1,21 @@ /** - _ _ _ -| |__ ___ ___ | |_ ___| |_ _ __ __ ___ __ -| '_ \ / _ \ / _ \| __/ __| __| '__/ _` \ \/ / -| |_) | (_) | (_) | |_\__ | |_| | | (_| |> < -|_.__/ \___/ \___/ \__|___/\__|_| \__,_/_/\_\.js - -*/ - -requirejs.config({ -baseUrl: '/static/oscar/js', -paths: { -app: '../app' -} -}); - -if(searx.autocompleter) { - searx.searchResults = new Bloodhound({ - datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), - queryTokenizer: Bloodhound.tokenizers.whitespace, - remote: '/autocompleter?q=%QUERY' - }); - searx.searchResults.initialize(); -} + * searx is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * searx is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with searx. If not, see < http://www.gnu.org/licenses/ >. + * + * (C) 2014 by Thomas Pointhuber, + */ $(document).ready(function(){ - $('.btn-toggle .btn').click(function() { - var btnClass = 'btn-' + $(this).data('btn-class'); - var btnLabelDefault = $(this).data('btn-label-default'); - var btnLabelToggled = $(this).data('btn-label-toggled'); - if(btnLabelToggled != '') { - if($(this).hasClass('btn-default')) { - - var html = $(this).html().replace(btnLabelDefault, btnLabelToggled); - } else { - var html = $(this).html().replace(btnLabelToggled, btnLabelDefault); - } - $(this).html(html); - } - $(this).toggleClass(btnClass); - $(this).toggleClass('btn-default'); - }); - - $('.btn-collapse').click(function() { - var btnTextCollapsed = $(this).data('btn-text-collapsed'); - var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed'); - - if(btnTextCollapsed != '' && btnTextNotCollapsed != '') { - if($(this).hasClass('collapsed')) { - var html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed); - } else { - var html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed); - } - $(this).html(html); - } - }); - - $(".select-all-on-click").click(function () { - $(this).select(); - }); - - if(searx.autocompleter) { - $('#q').typeahead(null, { - name: 'search-results', - displayKey: function(result) { - return result; - }, - source: searx.searchResults.ttAdapter() - }); - } - $(".searx_overpass_request").on( "click", function( event ) { var overpass_url = "https://overpass-api.de/api/interpreter?data="; var query_start = overpass_url + "[out:json][timeout:25];("; @@ -80,7 +27,7 @@ $(document).ready(function(){ var result_table_loadicon = "#" + $(this).data('result-table-loadicon'); // tags which can be ignored - var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ] + var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ]; if(osm_id && osm_type && result_table) { result_table = "#" + result_table; @@ -102,11 +49,11 @@ $(document).ready(function(){ //alert(query); var ajaxRequest = $.ajax( query ) .done(function( html) { - if(html && html['elements'] && html['elements'][0]) { - var element = html['elements'][0]; + if(html && html.elements && html.elements[0]) { + var element = html.elements[0]; var newHtml = $(result_table).html(); for (var row in element.tags) { - if(element.tags["name"] == null || osm_ignore_tags.indexOf(row) == -1) { + if(element.tags.name === null || osm_ignore_tags.indexOf(row) == -1) { newHtml += "" + row + ""; switch(row) { case "phone": @@ -125,11 +72,12 @@ $(document).ready(function(){ break; case "wikipedia": if(element.tags[row].indexOf(":") != -1) { - newHtml += "" + element.tags[row] + ""; + newHtml += "" + element.tags[row] + ""; break; } + /* jshint ignore:start */ default: + /* jshint ignore:end */ newHtml += element.tags[row]; break; } @@ -143,7 +91,7 @@ $(document).ready(function(){ }) .fail(function() { $(result_table_loadicon).html($(result_table_loadicon).html() + "

could not load data!

"); - }) + }); } } @@ -158,12 +106,12 @@ $(document).ready(function(){ var map_zoom = $(this).data('map-zoom'); var map_boundingbox = $(this).data('map-boundingbox'); var map_geojson = $(this).data('map-geojson'); - + require(['leaflet-0.7.3.min'], function(leaflet) { if(map_boundingbox) { - var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]), - northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]), - map_bounds = L.latLngBounds(southWest, northEast); + southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]); + northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]); + map_bounds = L.latLngBounds(southWest, northEast); } // TODO hack diff --git a/searx/static/oscar/package.json b/searx/static/oscar/package.json new file mode 100644 index 000000000..945b7943d --- /dev/null +++ b/searx/static/oscar/package.json @@ -0,0 +1,15 @@ +{ + "devDependencies": { + "grunt": "~0.4.5", + "grunt-contrib-uglify": "~0.6.0", + "grunt-contrib-watch" : "~0.6.1", + "grunt-contrib-concat" : "~0.5.0", + "grunt-contrib-jshint" : "~0.10.0" + }, + + "scripts": { + "build": "npm install && grunt", + "start": "grunt watch", + "test": "grunt" + } +} diff --git a/searx/templates/oscar/base.html b/searx/templates/oscar/base.html index 6fde942ac..e46024d17 100644 --- a/searx/templates/oscar/base.html +++ b/searx/templates/oscar/base.html @@ -73,6 +73,6 @@ {% if autocomplete %}{% endif %} - + diff --git a/searx/templates/oscar/search_full.html b/searx/templates/oscar/search_full.html index f44d48adf..673f23969 100644 --- a/searx/templates/oscar/search_full.html +++ b/searx/templates/oscar/search_full.html @@ -2,7 +2,7 @@