This repository contains the sample code for the Legacy Code Challenge episodes of James Shore's Let's Code: Test-Driven JavaScript screencast. Let's Code: Test-Driven JavaScript is a screencast series focused on rigorous, professional JavaScript development.
The Legacy Code Challenge episodes are about refactoring and adding tests to legacy code. The episodes use Afshin Mehrabani's Intro.js as an example. This repository is a fork of Intro.js.
The first episode focused on reverse engineering the design of Intro.js and creating a test strategy. The source code for that episode is on the lab6 branch and the notes from that work can be found in todo.txt.
The second episode introduced automating “pinning tests” to Intro.js. Pinning tests are rough tests designed to tell you when you’ve changed the behavior of your code in some way. The source code for that episode is on the lab7 branch. The pinning tests can be found in the tests directory.
The third episode used the pinning tests to act as a safety net for refactoring _showElement()
, one of Intro.js's key functions. Refactoring is a technique for changing the design of existing code without changing its behavior. The source code for that episode is on the lab8 branch. The refactored code can be found in Intro.js in lines 429-698.
The fourth episode factored the scrollToTargetElement()
function into a testable module, introduced Karma for cross-browser unit testing, and implemented a few demonstration unit tests. The source code for that episode is on the lab9 branch. The example tests can be found in _scroll_test.js and the extracted module can be found in scroll.js.
Further testing and refactoring is left as an exercise for the viewer.
(Start by cloning the repository and checking out the master branch.)
- Run
./jake.sh karma
to start Karma server. - Point a web browser at
http://localhost:9876
. Firefox 29 is known to work. - Run
./jake.sh
to lint and run the unit tests.
If you're using Windows, you can run jake
instead of ./jake.sh
. However, the code has not been tested on Windows.
(Start by cloning the repository and checking out the master branch.)
- Run
./jake.sh pinningTest
.
Note that the pinning tests are machine-specific. They'll probably fail when you run them. To make the pinning tests work on your machine, you need to rewrite the tests' approvals. This will cause the pinning tests to consider the current state of the code to be the "known good" reference.
To rewrite the pinning tests’ approvals:
- Open pinning_tests.js.
- Change the
RESET_APPROVALS
constant on line 11 totrue
. - Run
./jake.sh pinningTest
to rewrite the approvals. The test will report the rewrites as failures. - Change the
RESET_APPROVALS
constant back tofalse
. - Run
./jake.sh pinningTest
to run the pinning tests. They should pass.
The original Intro.js readme follows.
Better introductions for websites and features with a step-by-step guide for your projects.
You can obtain your local copy of Intro.js from:
1) This github repository, using git clone https://github.com/usablica/intro.js.git
2) Using bower bower install intro.js --save
3) Download it from CDN (1, 2)
Intro.js can be added to your site in three simple steps:
1) Include intro.js
and introjs.css
(or the minified version for production) in your page. Use introjs-rtl.min.css
for Right-to-Left language support.
CDN hosted files are available at jsDelivr (click Show More) & cdnjs.
2) Add data-intro
and data-step
to your HTML elements.
For example:
<a href='http://google.com/' data-intro='Hello step one!'></a>
See all attributes here.
3) Call this JavaScript function:
introJs().start();
Optionally, pass one parameter to introJs
function to limit the presentation section.
For example introJs(".introduction-farm").start();
runs the introduction only for elements with class='introduction-farm'
.
###introJs([targetElm])
Creating an introJs object.
Available since: v0.1.0
Parameters:
- targetElm : String (optional)
Should be defined to start introduction for specific element, for example:
#intro-farm
.
Returns:
- introJs object.
Example:
introJs() //without selector, start introduction for whole page
introJs("#intro-farm") //start introduction for element id='intro-farm'
###introJs.start()
Start the introduction for defined element(s).
Available since: v0.1.0
Returns:
- introJs object.
Example:
introJs().start()
###introJs.goToStep(step)
Go to specific step of introduction.
Available since: v0.3.0
Parameters:
- step : Number
Returns:
- introJs object.
Example:
introJs().goToStep(2).start(); //starts introduction from step 2
###introJs.nextStep()
Go to next step of introduction.
Available since: v0.7.0
Returns:
- introJs object.
Example:
introJs().start().nextStep();
###introJs.previousStep()
Go to previous step of introduction.
Available since: v0.7.0
Returns:
- introJs object.
Example:
introJs().goToStep(3).start().previousStep(); //starts introduction from step 2
###introJs.exit()
Exit the introduction.
Available since: v0.3.0
Returns:
- introJs object.
Example:
introJs().exit()
###introJs.setOption(option, value)
Set a single option to introJs object.
Available since: v0.3.0
Parameters:
-
option : String Option key name.
-
value : String/Number Value of the option.
Returns:
- introJs object.
Example:
introJs().setOption("skipLabel", "Exit");
###introJs.setOptions(options)
Set a group of options to the introJs object.
Available since: v0.3.0
Parameters:
- options : Object Object that contains option keys with values.
Returns:
- introJs object.
Example:
introJs().setOptions({ 'skipLabel': 'Exit', 'tooltipPosition': 'right' });
###introJs.refresh()
To refresh and order layers manually
Available since: v0.5.0
Returns:
- introJs object.
Example:
introJs().refresh();
###introJs.oncomplete(providedCallback)
Set callback for when introduction completed.
Available since: v0.2.0
Parameters:
- providedCallback : Function
Returns:
- introJs object.
Example:
introJs().oncomplete(function() {
alert("end of introduction");
});
###introJs.onexit(providedCallback)
Set callback to exit of introduction. Exit also means pressing ESC
key and clicking on the overlay layer by the user.
Available since: v0.2.0
Parameters:
- providedCallback : Function
Returns:
- introJs object.
Example:
introJs().onexit(function() {
alert("exit of introduction");
});
###introJs.onchange(providedCallback)
Set callback to change of each step of introduction. Given callback function will be called after completing each step. The callback function receives the element of the new step as an argument.
Available since: v0.3.0
Parameters:
- providedCallback : Function
Returns:
- introJs object.
Example:
introJs().onchange(function(targetElement) {
alert("new step");
});
###introJs.onbeforechange(providedCallback)
Given callback function will be called before starting a new step of introduction. The callback function receives the element of the new step as an argument.
Available since: v0.4.0
Parameters:
- providedCallback : Function
Returns:
- introJs object.
Example:
introJs().onbeforechange(function(targetElement) {
alert("before new step");
});
###introJs.onafterchange(providedCallback)
Given callback function will be called after starting a new step of introduction. The callback function receives the element of the new step as an argument.
Available since: v0.7.0
Parameters:
- providedCallback : Function
Returns:
- introJs object.
Example:
introJs().onafterchange(function(targetElement) {
alert("after new step");
});
###Attributes:
data-intro
: The tooltip text of stepdata-step
: Optionally define the number (priority) of stepdata-tooltipClass
: Optionally define a CSS class for tooltipdata-position
: Optionally define the position of tooltip,top
,left
,right
orbottom
. Default isbottom
###Options:
steps
: For defining steps using JSON configuration (see this example)nextLabel
: Next button labelprevLabel
: Previous button labelskipLabel
: Skip button labeldoneLabel
: Done button labeltooltipPosition
: Default tooltip positiontooltipClass
: Adding CSS class to all tooltipsexitOnEsc
: Exit introduction when pressing Escape button,true
orfalse
exitOnOverlayClick
: Exit introduction when clicking on overlay layer,true
orfalse
showStepNumbers
: Show steps number in the red circle or not,true
orfalse
keyboardNavigation
: Navigating with keyboard or not,true
orfalse
showButtons
: Show introduction navigation buttons or not,true
orfalse
showBullets
: Show introduction bullets or not,true
orfalse
scrollToElement
: Auto scroll to highlighted element if it's outside of viewport,true
orfalse
See setOption to see an example.
If you are using the rails asset pipeline you can use the introjs-rails gem.
You can simply use this project for Yii framework: https://github.com/moein7tl/Yii-IntroJS
Here you can find an IntroJs integration for Drupal: https://drupal.org/sandbox/alexanderfb/2061829
For AngularJS, you can use the directives in angular-intro.js.
You can use IntroJS inside your Wordpress, here is a good article by SitePoint: http://www.sitepoint.com/creating-intro-js-powered-tours-wordpress/
Here is a under construction plugin for Wordpress: https://github.com/newoldmedia/intro.js-wordpress
First you should install nodejs
and npm
, then first run this command: npm install
to install all dependencies.
Now you can run this command to minify all static resources:
make build
Want to learn faster and easier? Here we have Instant IntroJs, Packt Publishing.
- Add introduction without focusing on elements
- Fix problems with
position: fixed
and other positions - Provide more examples
-
v0.7.0 - 2014-02-07
- Add
onafterchange
event - Add scrolling to element option
- Add
nextStep
andpreviousStep
functions publicly - Add
_cloneObject
method to prevent data overwriting - Fix null elements problem with programmatic definition
- Fix issues with single-step introductions
- Fix top margin problem on hidden elements
- Fix stacking context problem caused by element opacity
- Fix call exit() on null elements
- Update documentation and add more details on CDN servers and RTL example
- Add
-
v0.6.0 - 2013-11-13
- Add step bullets with navigating
- Add option to hide introduction navigating buttons
- Make keyboard navigation optional
- Making
data-step
optional with elements - Fix scroll issue when scrolling down to elements bigger than window
- Fix Chrome version 30.0.1599.101 issue with hiding step numbers
- Fix incorrect calling onExit callback when user clicks on overlay layer
- Fix coding styles and improvement in performance
-
v0.5.0 - 2013-07-19
- Add CSS class option for tooltips (And tooltip buttons also)
- Add RTL version
- Ability to add HTML codes in tooltip content
- Ability to add DOM object and CSS selector in programmatic API (So you can use jQuery selector engine)
- Add
refresh()
method to refresh and order layers manually - Show tooltip buttons only when introduction steps are more than one
- Fix
onbeforechange
event bug and pass correct object in parameters - Fix
Null element exception
in some browsers - And add more examples
-
v0.4.0 - 2013-05-20
- Add multi-page introduction example
- Add programmatic introduction definition
- Cooler introduction background!
- Remove IE specific css file and embed IE support to main css file (property fallback)
- Update introduction position on window resize (Also support tablet/mobile devices rotation)
- Disable buttons on the first and start of introduction (Skip and Done button)
- Add
onbeforechange
callback - Add
showStepNumbers
option to show/hide step numbers - Add
exitOnEsc
andexitOnOverlayClick
options - Fix bad tooltip position calculating problem
- Fix a bug when using
!important
in element css properties - Fix a bug in
onexit
behavior - Code refactoring
-
v0.3.0 - 2013-03-28
- Adding support for CommonJS, RequireJS AMD and Browser Globals.
- Add
goToStep
function to go to specific step of introduction. - Add
onchange
callback. - Add
exit
function to exit from introduction. - Adding options with
setOption
andsetOptions
functions. - More IE compatibility.
- Fix
min-width
bug with tooltip box. - Code cleanup + Better coding style.
-
v0.2.1 - 2013-03-20
- Fix keydown event unbinding bug.
-
v0.2.0 - 2013-03-20
- Ability to define tooltip position with
data-position
attribute - Add
onexit
andoncomplete
callback - Better scrolling functionality
- Redesign navigating buttons + add previous button
- Fix overlay layer bug in wide monitors
- Fix show element for elements with position
absolute
orrelative
- Add
enter
key for navigating in steps - Code refactoring
- Ability to define tooltip position with
-
v0.1.0 - 2013-03-16
- First commit.
Afshin Mehrabani
Copyright (C) 2012 Afshin Mehrabani ([email protected])
Some portions copyright (C) 2014 Titanium I.T. LLC ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.