-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-input-history.js
109 lines (107 loc) · 3.75 KB
/
jquery-input-history.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
(function($) {
var storage;
storage = localStorage || {};
return $.widget('ngn.inputHistory', {
_create: function() {
var s, _this = this;
this.storageKey = 'inputHistory.' + this.element.attr('id');
this.h0 = (s = storage[this.storageKey]) ? s.split('\n') : [];
this.h = this.h0.concat(['']);
this.i = this.h0.length;
return this.element.keydown(function(e) {
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false); // ctrl detection
if (key == 13 && ctrl) {
return _this.ctrlenter();
} else if (key == 32 && ctrl) {
return _this.ctrlspace();
} else if (key == 13) {
return _this.enter();
} else if (key == 38) {
return _this.up();
} else if (key == 40) {
return _this.down();
}
});
},
up: function() {
if (this.i > 0) {
this.h[this.i--] = this.element.val();
this.element.val(this.h[this.i]);
}
this._trigger('up');
return false;
},
down: function() {
if (this.i < this.h0.length) {
this.h[this.i++] = this.element.val();
this.element.val(this.h[this.i]);
}
this._trigger('down');
return false;
},
ctrlenter: function() {
var v;
v = this.element.val();
if (v.length > 0 && v[0] != '{') {
v = '{' + v;
}
if (v.length > 0 && v[v.length-1] != '}') {
v += '}';
}
this.element.val(v);
this._trigger('enter');
return false;
},
ctrlspace: function() {
var v = this.element.val();
var url = window.location.search;
const params = new URLSearchParams(url);
var dev = params.get('detail');
if (dev.length > 0) {
var endPos = dev.length;
var cursorPos = this.element.prop('selectionStart');
if (v.indexOf(dev, (cursorPos-dev.length)) == cursorPos-dev.length) {
var nStr = v.split('');
nStr.splice(cursorPos-dev.length, dev.length);
nStr = nStr.join('');
this.element.val(nStr);
endPos = 0 - dev.length;
} else {
var textBefore = v.substring(0, cursorPos);
var textAfter = v.substring(cursorPos, v.length);
this.element.val(textBefore + dev + textAfter);
}
//this.element.focus();
this.element.prop('selectionEnd', cursorPos + endPos);
this._trigger('enter');
}
return false;
},
enter: function() {
var v;
this._trigger('enter');
if (this.i < this.h0.length) {
this.h[this.i] = this.h0[this.i];
}
v = this.element.val();
if (this.i >= 0 && this.i >= this.h0.length - 1 && this.h0[this.h0.length - 1] === v) {
this.h[this.h0.length] = '';
} else {
this.h[this.h0.length] = v;
this.h.push('');
this.h0.push(v);
storage[this.storageKey] = this.h0.join('\n');
}
this.i = this.h0.length;
}
});
}
)(jQuery);
$(document).ready(function() {
$('input.maininput').focus();
$('input.maininput').inputHistory();
if (localStorage.length > 0) {
$("#hdr tr").append("<td style='vertical-align:top'><img src='fhem/images/fhemSVG/checkbox_checked.svg' width='9' height='9' onclick='localStorage.removeItem(\"inputHistory.undefined\");$(this).hide()' style='cursor:pointer' alt='Historie löschen' title='Historie löschen'/></td>");
}
});