-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBeyondTableTop_Numbers_Dialog.user.js
More file actions
168 lines (161 loc) · 5.76 KB
/
BeyondTableTop_Numbers_Dialog.user.js
File metadata and controls
168 lines (161 loc) · 5.76 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ==UserScript==
// @name BeyondTableTop Numbers Dialog
// @namespace armeagle.nl
// @description Adds a dialog popup to any number inputs
// @include http://www.beyondtabletop.com/app*
// @updateURL https://raw.githubusercontent.com/ArmEagle/userscripts/master/BeyondTableTop_Numbers_Dialog.user.js
// @downloadURL https://raw.githubusercontent.com/ArmEagle/userscripts/master/BeyondTableTop_Numbers_Dialog.user.js
// @version 1.05
// @grant none
// ==/UserScript==
(function(){
function NumbersDialog() {
this.input = null;
this.inputStartValue = undefined;
this.dialogInput = null;
this.dialogInputInitial = false; // if true, then content is selected
this.numbersContainer = null;
this.init();
}
NumbersDialog.prototype.buildDialog = function() {
this.numbersContainer = $('<div id="numbers-dialog"/>');
$('body').append(this.numbersContainer);
this.numbersContainer.append($('\
<div class="input"/>\
<div class="numbers-container">\
<div class="b"" data-v="7">7</div>\
<div class="b" data-v="8">8</div>\
<div class="b" data-v="9">9</div>\
<div class="b" data-v="4">4</div>\
<div class="b" data-v="5">5</div>\
<div class="b" data-v="6">6</div>\
<div class="b" data-v="1">1</div>\
<div class="b" data-v="2">2</div>\
<div class="b" data-v="3">3</div>\
<div class="b" data-v="0">0</div>\
<div class="b" data-a="u">▲</div>\
<div class="b" data-a="d">▼</div>\
</div>\
<div class="oper-container">\
<div class="b" data-v="-">-</div>\
<div class="b" data-v="+">+</div>\
<div class="b" data-a="=">=</div>\
<div class="b" data-a="f">ok</div>\
</div>\
'));
this.dialogInput = $('#numbers-dialog .input');
}
NumbersDialog.prototype.open = function() {
this.numbersContainer.addClass('active');
}
NumbersDialog.prototype.close = function() {
this.numbersContainer.removeClass('active');
}
// Focus on an input, open the dialog with given value.
NumbersDialog.prototype.handleFocus = function(that, input, event) {
that.input = input;
that.inputValue = input.val();
that.dialogInput.text(input.val());
that.dialogInput.selectText();
this.dialogInputInitial = true;
this.open();
}
NumbersDialog.prototype.initListeners = function() {
var that = this;
$('body').on('focus', 'input[type=number]', function(event) {
that.handleFocus(that, $(this), event);
});
$('body').on('click', '#numbers-dialog', function(event) {
// not close dialog with click inside
event.stopPropagation();
});
$('body').on('click', function(event) {
// close dialog with click outside, not an input number
if ( ! $(event.target).is('input[type=number]') ) {
that.close();
}
});
// button clicks
$('#numbers-dialog').on('click', '.b', function(event) {
var $button = $(this);
event.preventDefault()
event.stopPropagation();
if ( (bv = $button.data('v')) ) {
that.inputAdd(bv);
} else if ( (av = $button.data('a')) ) {
switch ( av ) {
case 'u': // up/increase - +1
that.eval('+1');
break;
case 'd': // down/decrease
that.eval('-1');
break;
case '=': // equals/eval
that.eval();
break;
case 'f': // ok/finish
that.eval();
that.input.val(that.dialogInput.text());
that.input.trigger('change');
that.close();
break;
}
}
});
}
// Do Maths!
NumbersDialog.prototype.eval = function(append) {
var inputValue = this.dialogInput.text();
if ( append ) {
inputValue = inputValue + append;
}
inputValue = inputValue.replace(/[^0-9\-+]/g, '').replace(/\+\+/g, '+');
this.dialogInput.text(eval(inputValue));
}
NumbersDialog.prototype.inputAdd = function(value) {
// Initial number will replace selection. Other buttons append.
if ( this.dialogInputInitial && (value +'').match(/^[0-9]$/) ) {
this.dialogInput.text(value);
} else {
this.dialogInput.text(this.dialogInput.text() + value);
}
this.dialogInputInitial = false;
}
NumbersDialog.prototype.initCss = function() {
$('head').append($('<style type="text/css">\
#numbers-dialog { display: none; width: 270px; height: 300px; border: 1px solid #666; background: #ddd; position: fixed; margin-top: -150px; margin-left: -135px; top: 50%; left: 50%; }\
#numbers-dialog.active { display: block; }\
#numbers-dialog .input { margin: 5px; width: 240px; height: 30px; padding: 0 10px; line-height: 30px; font-size: 1.5em; border 2px inset #666; background: white; }\
#numbers-dialog .b { width: 50px; height: 50px; margin: 5px; float: left; cursor: pointer; background: #fff; border: 1px solid #666; border-radius: 10px; line-height: 50px; font-size: 2.5em; text-align: center; -webkit-user-select: none; -moz-user-select: none; -khtml-user-select: none; -ms-user-select: none; /* IE10+ */}\
#numbers-dialog .b:hover, #numbers-dialog .b:active { background: #ddd; }\
#numbers-dialog .numbers-container { width: 208px; float: left; }\
#numbers-dialog .oper-container { width: 60px; float: left; }\
input[type="number"]::-webkit-outer-spin-button,input[type="number"]::-webkit-inner-spin-button {-webkit-appearance: none;margin: 0;}\
input[type="number"] {-moz-appearance: textfield;}\
</style>'));
}
NumbersDialog.prototype.init = function() {
this.initCss();
this.buildDialog();
this.initListeners();
}
// Select all text in the element.
$.fn.selectText = function(win) {
el = this.first()[0];
win = win || window;
var doc = win.document, sel, range;
if (win.getSelection && doc.createRange) {
sel = win.getSelection();
range = doc.createRange();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (doc.body.createTextRange) {
range = doc.body.createTextRange();
range.moveToElementText(el);
range.select();
}
};
var nc = new NumbersDialog();
console.log(['Numbers Dialog loaded',nc]);
})();