forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroller.js
109 lines (95 loc) · 4 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
104
105
106
107
108
109
/**
* 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.scroller()
* .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');
var RandomizerRequiredException = require('../exceptions/randomizerRequired');
return function() {
var document = window.document,
documentElement = document.documentElement,
body = document.body;
function defaultPositionSelector() {
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 })
];
}
function defaultShowAction(scrollX, scrollY) {
var scrollSignal = document.createElement('div');
scrollSignal.style.zIndex = 2000;
scrollSignal.style.border = "3px solid red";
scrollSignal.style.width = (documentElement.clientWidth - 25) + "px";
scrollSignal.style.height = (documentElement.clientHeight - 25) + "px";
scrollSignal.style.position = "absolute";
scrollSignal.style.webkitTransition = 'opacity 1s ease-out';
scrollSignal.style.mozTransition = 'opacity 1s ease-out';
scrollSignal.style.transition = 'opacity 1s ease-out';
scrollSignal.style.left = (scrollX + 10) + 'px';
scrollSignal.style.top = (scrollY + 10) + 'px';
var element = body.appendChild(scrollSignal);
setTimeout(function() {
body.removeChild(element);
}, 1000);
setTimeout(function() {
element.style.opacity = 0;
}, 50);
}
/**
* @mixin
*/
var config = {
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
logger: null,
randomizer: null
};
/**
* @mixes config
*/
function scrollerGremlin() {
if (!config.randomizer) {
throw new RandomizerRequiredException();
}
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;
};
});