forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroller.js
103 lines (90 loc) · 3.77 KB
/
scroller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* The scroller gremlin scrolls the viewport to reveal another part of the document
*
* var scrollerGremlin = gremlins.species.scroller();
* horde.gremlin(scrollerGremlin);
*
* The scrollerGremlin gremlin can be customized as follows:
*
* scrollerGremlin.positionSelector(function() { // return a random position to scroll to });
* scrollerGremlin.showAction(function(element) { // show the gremlin activity on screen });
* scrollerGremlin.logger(loggerObject); // inject a logger
* scrollerGremlin.randomizer(randomizerObject); // inject a randomizer
*
* Example usage:
*
* horde.gremlin(gremlins.species.clicker()
* .positionSelector(function() {
* // only click in the app
* var $list = $('#todoapp');
* var offset = $list.offset();
* return [
* parseInt(Math.random() * $list.outerWidth() + offset.left),
* parseInt(Math.random() * ($list.outerHeight() + $('#info').outerHeight()) + offset.top)
* ];
* })
* )
*/
define(function(require) {
"use strict";
var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
return function() {
var document = window.document,
documentElement = document.documentElement,
body = document.body;
var defaultPositionSelector = function() {
var documentWidth = Math.max(body.scrollWidth, body.offsetWidth, documentElement.scrollWidth, documentElement.offsetWidth, documentElement.clientWidth),
documentHeight = Math.max(body.scrollHeight, body.offsetHeight, documentElement.scrollHeight, documentElement.offsetHeight, documentElement.clientHeight);
return [
config.randomizer.natural({ max: documentWidth - documentElement.clientWidth }),
config.randomizer.natural({ max: documentHeight - documentElement.clientHeight })
];
};
var defaultShowAction = function(scrollX, scrollY) {
var clickSignal = document.createElement('div');
clickSignal.style.border = "3px solid red";
clickSignal.style.width = (documentElement.clientWidth - 25) + "px";
clickSignal.style.height = (documentElement.clientHeight - 25) + "px";
clickSignal.style.position = "absolute";
clickSignal.style.webkitTransition = 'opacity 1s ease-out';
clickSignal.style.mozTransition = 'opacity 1s ease-out';
clickSignal.style.transition = 'opacity 1s ease-out';
clickSignal.style.left = (scrollX + 10) + 'px';
clickSignal.style.top = (scrollY + 10) + 'px';
var element = body.appendChild(clickSignal);
setTimeout(function() {
body.removeChild(element);
}, 1000);
setTimeout(function() {
element.style.opacity = 0;
}, 50);
};
/**
* @mixin
*/
var config = {
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
logger: {},
randomizer: new Chance()
};
/**
* @mixes config
*/
function scrollerGremlin() {
var position = config.positionSelector(),
scrollX = position[0],
scrollY = position[1];
window.scrollTo(scrollX, scrollY);
if (typeof config.showAction == 'function') {
config.showAction(scrollX, scrollY);
}
if (typeof config.logger.log == 'function') {
config.logger.log('gremlin', 'scroller ', 'scroll to', scrollX, scrollY);
}
}
configurable(scrollerGremlin, config);
return scrollerGremlin;
};
});