From 9e358f7c4b9536dd9fadd3fee3a52305b9a98364 Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 7 Apr 2020 02:59:56 +0300 Subject: Links history --- res/client.js | 93 +++++++++++++++++++++++++----------------- src/client/Buttons.hx | 46 +++++---------------- src/client/InputWithHistory.hx | 54 ++++++++++++++++++++++++ src/client/Main.hx | 9 ++-- 4 files changed, 125 insertions(+), 77 deletions(-) create mode 100644 src/client/InputWithHistory.hx diff --git a/res/client.js b/res/client.js index 68064e2..ca309a3 100644 --- a/res/client.js +++ b/res/client.js @@ -773,47 +773,66 @@ client_Buttons.initChatInput = function(main) { } return; }; - var chatLine = window.document.querySelector("#chatline"); - chatLine.onkeydown = function(e2) { - switch(e2.keyCode) { + new client_InputWithHistory(window.document.querySelector("#chatline"),null,50,function(value) { + main.send({ type : "Message", message : { clientName : "", text : value}}); + return true; + }); +}; +var client_InputWithHistory = function(element,history,maxItems,onEnter) { + this.historyId = -1; + this.element = element; + if(history != null) { + this.history = history; + } else { + this.history = []; + } + this.maxItems = maxItems; + this.onEnter = onEnter; + element.onkeydown = $bind(this,this.onKeyDown); +}; +client_InputWithHistory.__name__ = true; +client_InputWithHistory.prototype = { + onKeyDown: function(e) { + switch(e.keyCode) { case 13: - if(chatLine.value.length == 0) { + if(this.element.value.length == 0) { return; } - main.send({ type : "Message", message : { clientName : "", text : chatLine.value}}); - client_Buttons.personalHistory.push(chatLine.value); - if(client_Buttons.personalHistory.length > 50) { - client_Buttons.personalHistory.shift(); + if(this.onEnter(this.element.value)) { + this.history.push(this.element.value); } - client_Buttons.personalHistoryId = -1; - chatLine.value = ""; + if(this.history.length > this.maxItems) { + this.history.shift(); + } + this.historyId = -1; + this.element.value = ""; break; case 38: - client_Buttons.personalHistoryId--; - if(client_Buttons.personalHistoryId == -2) { - client_Buttons.personalHistoryId = client_Buttons.personalHistory.length - 1; - if(client_Buttons.personalHistoryId == -1) { + this.historyId--; + if(this.historyId == -2) { + this.historyId = this.history.length - 1; + if(this.historyId == -1) { return; } - } else if(client_Buttons.personalHistoryId == -1) { - client_Buttons.personalHistoryId++; + } else if(this.historyId == -1) { + this.historyId++; } - chatLine.value = client_Buttons.personalHistory[client_Buttons.personalHistoryId]; + this.element.value = this.history[this.historyId]; break; case 40: - if(client_Buttons.personalHistoryId == -1) { + if(this.historyId == -1) { return; } - client_Buttons.personalHistoryId++; - if(client_Buttons.personalHistoryId > client_Buttons.personalHistory.length - 1) { - client_Buttons.personalHistoryId = -1; - chatLine.value = ""; + this.historyId++; + if(this.historyId > this.history.length - 1) { + this.historyId = -1; + this.element.value = ""; return; } - chatLine.value = client_Buttons.personalHistory[client_Buttons.personalHistoryId]; + this.element.value = this.history[this.historyId]; break; } - }; + } }; var client_Main = function(host,port) { this.matchNumbers = new EReg("^-?[0-9]+$",""); @@ -923,24 +942,22 @@ client_Main.prototype = { _gthis.addVideoUrl(true); return; }; - window.document.querySelector("#mediaurl").onkeydown = function(e4) { - if(e4.keyCode == 13) { - _gthis.addVideoUrl(true); - } - return; - }; - window.document.querySelector("#ce_queue_next").onclick = function(e5) { + new client_InputWithHistory(window.document.querySelector("#mediaurl"),this.settings.latestLinks,10,function(value) { + _gthis.addVideoUrl(true); + return true; + }); + window.document.querySelector("#ce_queue_next").onclick = function(e4) { _gthis.addIframe(false); return; }; - window.document.querySelector("#ce_queue_end").onclick = function(e6) { + window.document.querySelector("#ce_queue_end").onclick = function(e5) { _gthis.addIframe(true); return; }; - window.document.querySelector("#customembed-title").onkeydown = function(e7) { - if(e7.keyCode == 13) { + window.document.querySelector("#customembed-title").onkeydown = function(e6) { + if(e6.keyCode == 13) { _gthis.addIframe(true); - e7.preventDefault(); + e6.preventDefault(); } return; }; @@ -977,6 +994,8 @@ client_Main.prototype = { return; } mediaUrl.value = ""; + this.settings.latestLinks.push(url); + client_Settings.write(this.settings); var _this_r = new RegExp(",(https?)","g".split("u").join("")); var links = url.replace(_this_r,"|$1").split("|"); this.handleUrlMasks(links); @@ -1072,7 +1091,7 @@ client_Main.prototype = { var data = JSON.parse(e.data); var t = data.type; var t1 = t.charAt(0).toLowerCase() + HxOverrides.substr(t,1,null); - haxe_Log.trace("Event: " + data.type,{ fileName : "src/client/Main.hx", lineNumber : 300, className : "client.Main", methodName : "onMessage", customParams : [data[t1]]}); + haxe_Log.trace("Event: " + data.type,{ fileName : "src/client/Main.hx", lineNumber : 303, className : "client.Main", methodName : "onMessage", customParams : [data[t1]]}); switch(data.type) { case "AddVideo": this.player.addVideoItem(data.addVideo.item,data.addVideo.atEnd); @@ -3039,8 +3058,6 @@ js_Boot.__toStr = ({ }).toString; Lang.langs = new haxe_ds_StringMap(); Lang.ids = ["en","ru"]; Lang.lang = HxOverrides.substr(window.navigator.language,0,2).toLowerCase(); -client_Buttons.personalHistory = []; -client_Buttons.personalHistoryId = -1; client_Settings.isSupported = false; client_players_Youtube.matchId = new EReg("v=([A-z0-9_-]+)",""); client_players_Youtube.matchShort = new EReg("youtu.be/([A-z0-9_-]+)",""); diff --git a/src/client/Buttons.hx b/src/client/Buttons.hx index 2d32d99..8a1264e 100644 --- a/src/client/Buttons.hx +++ b/src/client/Buttons.hx @@ -12,8 +12,6 @@ import js.html.Event; class Buttons { - static final personalHistory:Array = []; - static var personalHistoryId = -1; static var split:Split; static var settings:ClientSettings; @@ -312,40 +310,16 @@ class Buttons { } } - final chatLine:InputElement = cast ge("#chatline"); - chatLine.onkeydown = function(e:KeyboardEvent) { - switch (e.keyCode) { - case 13: // Enter - if (chatLine.value.length == 0) return; - main.send({ - type: Message, - message: { - clientName: "", - text: chatLine.value - } - }); - personalHistory.push(chatLine.value); - if (personalHistory.length > 50) personalHistory.shift(); - personalHistoryId = -1; - chatLine.value = ""; - case 38: // Up - personalHistoryId--; - if (personalHistoryId == -2) { - personalHistoryId = personalHistory.length - 1; - if (personalHistoryId == -1) return; - } else if (personalHistoryId == -1) personalHistoryId++; - chatLine.value = personalHistory[personalHistoryId]; - case 40: // Down - if (personalHistoryId == -1) return; - personalHistoryId++; - if (personalHistoryId > personalHistory.length - 1) { - personalHistoryId = -1; - chatLine.value = ""; - return; - } - chatLine.value = personalHistory[personalHistoryId]; - } - } + new InputWithHistory(cast ge("#chatline"), 50, value -> { + main.send({ + type: Message, + message: { + clientName: "", + text: value + } + }); + return true; + }); } } diff --git a/src/client/InputWithHistory.hx b/src/client/InputWithHistory.hx new file mode 100644 index 0000000..9c5b615 --- /dev/null +++ b/src/client/InputWithHistory.hx @@ -0,0 +1,54 @@ +package client; + +import js.html.KeyboardEvent; +import js.html.InputElement; + +class InputWithHistory { + + final element:InputElement; + final maxItems:Int; + final history:Array; + final onEnter:(value:String)->Bool; + var historyId = -1; + + public function new( + element:InputElement, ?history:Array, maxItems:Int, + onEnter:(value:String)->Bool + ) { + this.element = element; + if (history != null) this.history = history; + else this.history = []; + this.maxItems = maxItems; + this.onEnter = onEnter; + element.onkeydown = onKeyDown; + } + + function onKeyDown(e:KeyboardEvent) { + switch (e.keyCode) { + case 13: // Enter + if (element.value.length == 0) return; + final isAdd = onEnter(element.value); + if (isAdd) history.push(element.value); + if (history.length > maxItems) history.shift(); + historyId = -1; + element.value = ""; + case 38: // Up + historyId--; + if (historyId == -2) { + historyId = history.length - 1; + if (historyId == -1) return; + } else if (historyId == -1) historyId++; + element.value = history[historyId]; + case 40: // Down + if (historyId == -1) return; + historyId++; + if (historyId > history.length - 1) { + historyId = -1; + element.value = ""; + return; + } + element.value = history[historyId]; + } + } + +} diff --git a/src/client/Main.hx b/src/client/Main.hx index e5cd58b..5e114ca 100644 --- a/src/client/Main.hx +++ b/src/client/Main.hx @@ -136,9 +136,10 @@ class Main { ge("#queue_next").onclick = e -> addVideoUrl(false); ge("#queue_end").onclick = e -> addVideoUrl(true); - ge("#mediaurl").onkeydown = (e:KeyboardEvent) -> { - if (e.keyCode == 13) addVideoUrl(true); - } + new InputWithHistory(cast ge("#mediaurl"), settings.latestLinks, 10, value -> { + addVideoUrl(true); + return true; + }); ge("#ce_queue_next").onclick = e -> addIframe(false); ge("#ce_queue_end").onclick = e -> addIframe(true); @@ -189,6 +190,8 @@ class Main { final url = mediaUrl.value; if (url.length == 0) return; mediaUrl.value = ""; + settings.latestLinks.push(url); + Settings.write(settings); final url = ~/,(https?)/g.replace(url, "|$1"); final links = url.split("|"); handleUrlMasks(links); -- cgit v1.2.3