This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
window.js
102 lines (79 loc) · 2.09 KB
/
window.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// stub out the window object for testing.
define([
'underscore',
'backbone'
],
function (_, Backbone) {
'use strict';
function WindowMock() {
var self = this;
this.translator = window.translator;
this.location = {
href: window.location.href,
search: window.location.search,
pathname: '/'
};
this.document = {
title: window.document.title
};
this.history = {
back: function () {
self.history.back.called = true;
}
};
this.navigator = {
userAgent: window.navigator.userAgent
};
this.console = window.console;
}
_.extend(WindowMock.prototype, Backbone.Events, {
dispatchEvent: function (event) {
var msg = event.detail.command || event.detail.message;
var listenerEvent = {
data: {
type: msg,
content: event.detail.data
}
};
this.trigger(msg, listenerEvent);
if (! this.dispatchedEvents) {
this.dispatchedEvents = {};
}
if (typeof msg === 'object') {
this.dispatchedEvents[msg.command] = msg;
} else {
this.dispatchedEvents[msg] = true;
}
},
isEventDispatched: function (eventName) {
return !! (this.dispatchedEvents && this.dispatchedEvents[eventName]);
},
addEventListener: function(msg, callback, bubbles) {
this.on(msg, callback);
},
removeEventListener: function(msg, callback, bubbles) {
this.off(msg, callback);
},
CustomEvent: function(command, data) {
return data;
},
scrollTo: function(x, y) {
},
setTimeout: function (callback, timeoutMS) {
this._isTimeoutSet = true;
return 'timeout';
},
isTimeoutSet: function () {
return !!this._isTimeoutSet;
},
clearTimeout: function (timeout) {
},
navigator: {
language: 'en-US'
}
});
return WindowMock;
});