diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..aa73e00 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,24 @@ +{ + "curly" : true, + "eqeqeq" : true, + "immed" : true, + "latedef" : true, + "newcap" : true, + "noarg" : true, + "sub" : true, + "undef" : true, + "boss" : true, + "eqnull" : true, + "node" : true, + "es5" : false, + "globals" : { + "it" : false, + "xit" : false, + "describe" : false, + "xdescribe" : false, + "beforeEach" : false, + "afterEach" : false, + "expect" : false, + "spyOn" : false + } +} diff --git a/Gruntfile.js b/Gruntfile.js index 32d98cd..af8eb31 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -32,13 +32,15 @@ module.exports = function(grunt) { require('load-grunt-tasks')(grunt); // There are basically three phases of building the production theme: + // 0) Testing / linting + grunt.registerTask('test', ['jshint', 'jasmine']); // 1) Javascript preparation (concatenating and uglifying scripts) - // (coming soon) + grunt.registerTask('javascript', ['uglify']); // 2) Stylesheet preparation (SASS, autoprefixing, and minification) // (coming soon) // 3) Appending the most recent git commit to the theme version grunt.registerTask('release', ['gitinfo', 'replace']); // The default task performs all three phases. - grunt.registerTask('default', ['release']); + grunt.registerTask('default', ['test', 'javascript', 'release']); }; diff --git a/functions.php b/functions.php index 464b1bb..d1add67 100644 --- a/functions.php +++ b/functions.php @@ -32,11 +32,11 @@ function add_styles() { add_action( 'wp_enqueue_scripts', 'add_styles' ); /** - * Add LazyLoad and MyScripts for all users + * Add LazyLoad and main javascript for all users */ function add_scripts() { - wp_enqueue_script( 'lazyload', get_stylesheet_directory_uri() . '/js/lazyload.js', array( 'jquery' ), '', true ); - wp_enqueue_script( 'myScripts', get_stylesheet_directory_uri() . '/js/myScripts.js', array( 'lazyload' ), '', true ); + wp_enqueue_script( 'lazyload', get_stylesheet_directory_uri() . '/js/build/jquery.lazyload.min.js', array( 'jquery' ), '', true ); + wp_enqueue_script( 'mitlibnews', get_stylesheet_directory_uri() . '/js/build/mitlibnews.min.js', array( 'lazyload' ), '', true ); } add_action( 'wp_enqueue_scripts', 'add_scripts' ); diff --git a/js/lazyload.js b/js/lazyload.js deleted file mode 100644 index 3e4a5f4..0000000 --- a/js/lazyload.js +++ /dev/null @@ -1,15 +0,0 @@ -(function($,window,document,undefined){var $window=$(window);$.fn.lazyload=function(options){var elements=this;var $container;var settings={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:window,data_attribute:"original",skip_invisible:true,appear:null,load:null,placeholder:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC"};function update(){var counter=0;elements.each(function(){var $this=$(this);if(settings.skip_invisible&&!$this.is(":visible")){return;} -if($.abovethetop(this,settings)||$.leftofbegin(this,settings)){}else if(!$.belowthefold(this,settings)&&!$.rightoffold(this,settings)){$this.trigger("appear");counter=0;}else{if(++counter>settings.failure_limit){return false;}}});} -if(options){if(undefined!==options.failurelimit){options.failure_limit=options.failurelimit;delete options.failurelimit;} -if(undefined!==options.effectspeed){options.effect_speed=options.effectspeed;delete options.effectspeed;} -$.extend(settings,options);} -$container=(settings.container===undefined||settings.container===window)?$window:$(settings.container);if(0===settings.event.indexOf("scroll")){$container.bind(settings.event,function(){return update();});} -this.each(function(){var self=this;var $self=$(self);self.loaded=false;if($self.attr("src")===undefined||$self.attr("src")===false){if($self.is("img")){$self.attr("src",settings.placeholder);}} -$self.one("appear",function(){if(!this.loaded){if(settings.appear){var elements_left=elements.length;settings.appear.call(self,elements_left,settings);} -$("").bind("load",function(){var original=$self.attr("data-"+settings.data_attribute);$self.hide();if($self.is("img")){$self.attr("src",original);}else{$self.css("background-image","url('"+original+"')");} -$self[settings.effect](settings.effect_speed);self.loaded=true;var temp=$.grep(elements,function(element){return!element.loaded;});elements=$(temp);if(settings.load){var elements_left=elements.length;settings.load.call(self,elements_left,settings);}}).attr("src",$self.attr("data-"+settings.data_attribute));}});if(0!==settings.event.indexOf("scroll")){$self.bind(settings.event,function(){if(!self.loaded){$self.trigger("appear");}});}});$window.bind("resize",function(){update();});if((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)){$window.bind("pageshow",function(event){if(event.originalEvent&&event.originalEvent.persisted){elements.each(function(){$(this).trigger("appear");});}});} -$(document).ready(function(){update();});return this;};$.belowthefold=function(element,settings){var fold;if(settings.container===undefined||settings.container===window){fold=(window.innerHeight?window.innerHeight:$window.height())+$window.scrollTop();}else{fold=$(settings.container).offset().top+$(settings.container).height();} -return fold<=$(element).offset().top-settings.threshold;};$.rightoffold=function(element,settings){var fold;if(settings.container===undefined||settings.container===window){fold=$window.width()+$window.scrollLeft();}else{fold=$(settings.container).offset().left+$(settings.container).width();} -return fold<=$(element).offset().left-settings.threshold;};$.abovethetop=function(element,settings){var fold;if(settings.container===undefined||settings.container===window){fold=$window.scrollTop();}else{fold=$(settings.container).offset().top;} -return fold>=$(element).offset().top+settings.threshold+$(element).height();};$.leftofbegin=function(element,settings){var fold;if(settings.container===undefined||settings.container===window){fold=$window.scrollLeft();}else{fold=$(settings.container).offset().left;} -return fold>=$(element).offset().left+settings.threshold+$(element).width();};$.inviewport=function(element,settings){return!$.rightoffold(element,settings)&&!$.leftofbegin(element,settings)&&!$.belowthefold(element,settings)&&!$.abovethetop(element,settings);};$.extend($.expr[":"],{"below-the-fold":function(a){return $.belowthefold(a,{threshold:0});},"above-the-top":function(a){return!$.belowthefold(a,{threshold:0});},"right-of-screen":function(a){return $.rightoffold(a,{threshold:0});},"left-of-screen":function(a){return!$.rightoffold(a,{threshold:0});},"in-viewport":function(a){return $.inviewport(a,{threshold:0});},"above-the-fold":function(a){return!$.belowthefold(a,{threshold:0});},"right-of-fold":function(a){return $.rightoffold(a,{threshold:0});},"left-of-fold":function(a){return!$.rightoffold(a,{threshold:0});}});})(jQuery,window,document); \ No newline at end of file diff --git a/js/mitlibnews.js b/js/mitlibnews.js new file mode 100644 index 0000000..a2ed51c --- /dev/null +++ b/js/mitlibnews.js @@ -0,0 +1,15 @@ +var jQuery; + +(function($) { + + //lazy loading + $("img.img-responsive").lazyload({ + effect : "fadeIn", + effectspeed: 450, + failure_limit: 999999 + }); + + //category force selection of all news + $('input:checkbox[id=in-category-43]').attr('checked',true); + +})(jQuery); diff --git a/js/myScripts.js b/js/myScripts.js deleted file mode 100644 index 7d5ed16..0000000 --- a/js/myScripts.js +++ /dev/null @@ -1,35 +0,0 @@ - - -(function($) { - - //lazy loading - $("img.img-responsive").lazyload({ - effect : "fadeIn", - effectspeed: 450 , - failure_limit: 999999 - }); - - //category force selection of all news - $('input:checkbox[id=in-category-43]').attr('checked',true); - -})(jQuery); - - - - - - - - - - - - - - - - - - - - diff --git a/js/src/mitlibnews.loader.js b/js/src/mitlibnews.loader.js new file mode 100644 index 0000000..b6b74af --- /dev/null +++ b/js/src/mitlibnews.loader.js @@ -0,0 +1,3 @@ +function Loader(params) { + "use strict"; +} \ No newline at end of file diff --git a/js/tests/mitlibnews.loader.spec.js b/js/tests/mitlibnews.loader.spec.js new file mode 100644 index 0000000..f104e78 --- /dev/null +++ b/js/tests/mitlibnews.loader.spec.js @@ -0,0 +1,5 @@ +describe("Loader test suite", function() { + it("expects tautologies", function() { + expect(true).toBe(true); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index f86127d..43f77c4 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "mitlibraries-news", - "version": "1.3.0", + "version": "2.0.0", "description": "A Wordpress child theme that descends from mitlibraries-parent", - "main": "index.js", + "main": "mitlibnews.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -22,10 +22,14 @@ "devDependencies": { "glob": "^7.0.5", "grunt": "^1.0.1", + "grunt-contrib-jasmine": "^1.0.3", + "grunt-contrib-jshint": "^1.0.0", + "grunt-contrib-uglify": "^2.0.0", "grunt-gitinfo": "^0.1.8", "grunt-replace": "^1.0.1" }, "dependencies": { + "jquery-lazyload": "^1.9.7", "load-grunt-tasks": "^3.5.0" } } diff --git a/tasks/options/jasmine.js b/tasks/options/jasmine.js new file mode 100644 index 0000000..5c4f360 --- /dev/null +++ b/tasks/options/jasmine.js @@ -0,0 +1,6 @@ +module.exports = { + src: 'js/src/**/*.js', + options: { + specs: 'js/tests/**/*.js' + } +} diff --git a/tasks/options/jshint.js b/tasks/options/jshint.js new file mode 100644 index 0000000..c13a152 --- /dev/null +++ b/tasks/options/jshint.js @@ -0,0 +1,9 @@ +module.exports = { + all: [ + 'Gruntfile.js', + 'js/mitlibnews.js' + ], + options: { + jshintrc: '.jshintrc' + } +} diff --git a/tasks/options/uglify.js b/tasks/options/uglify.js new file mode 100644 index 0000000..133ce61 --- /dev/null +++ b/tasks/options/uglify.js @@ -0,0 +1,10 @@ +module.exports = { + core: { + src: 'js/mitlibnews.js', + dest: 'js/build/mitlibnews.min.js' + }, + lazyLoad: { + src: 'node_modules/jquery-lazyload/jquery.lazyload.js', + dest: 'js/build/jquery.lazyload.min.js' + } +}