From 999033ec154848adf5d261e187b18825fd93d28c Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 28 Jan 2018 13:03:11 -0700 Subject: [PATCH] New menu sorting, fix up default SGR --- art/themes/luciano_blocktronics/MATRIX.ANS | Bin 4797 -> 5117 bytes core/horizontal_menu_view.js | 2 +- core/menu_view.js | 33 +++++++++++++++++++-- core/vertical_menu_view.js | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/art/themes/luciano_blocktronics/MATRIX.ANS b/art/themes/luciano_blocktronics/MATRIX.ANS index 4e18372379762657be552e9bb01e2e8df65401ee..14219543d7a87cf9e911adada5c27c3a5f07c8bd 100644 GIT binary patch delta 884 zcmZ8fyG{Z@6b%sMO#~8~MKTEzO$>3D#RbGht+hdoLP-1or?J4qM>N(pdTmH(EUat< zYU5W}?IzpV8xlLkxihoL4$aQq$2oV-nfqS)U7puEdb6rG%^Hns9aE`%(&4VbWXKZ>NN6-6pIlSm!Ukw1dkbEdX;DJF zu~qm=786)9BttIMF7a5d2fIMELK#5k>&1R;d8a|@mXo45gR({7Fcbpi^hXLKbPlbc z<4L*@%yVI!*P`m?SQH8wu_)cl3deF<#KL`M{|4El^R^u)u5H_{LzL}foMJPjIBvQ~ hdN#|H<6tA1$+~i}C7eGIgNp}P&7KCIa?sB%{R4tV2Ot0d delta 567 zcmYjOJxjx25XQ7k+w@CQ5F~!QpjEdfO`En<#rkd)CyQu<;Gk2@;vk49qPQ011{EB2 zaaM#srjGA2! zLjU<-Y`LsEnqB2YHcO5dP+aCQ#ykuzrifV_R;9jF;qW6hVLdX67$zUIqO~!?da)A; z8EJW&zPj?BT=yHor~D#Ug7hrDk){-f^hpK`uLUdVwSm{#_s z8i!luDP6Jt{K)_-8no)CH%5n-jTLyB%*R0~g z7XE`|rubLUhgRlvhETtPG2)ZV5Wvwe%*-pkTZUg9&320ZJS#L1jsEAP(Cl)_!9Hle zzcmvAZn9SseofUuQ&BawhpF60JUqkTJ2xw3Xid}|8b7Bt;YeK!D7f~6+TILzD>*_a o%o9R*F7wY66b9EL{d7Y#h>lNSkbB{I$p;U*`D3ZA!dHIb7Z850UjP6A diff --git a/core/horizontal_menu_view.js b/core/horizontal_menu_view.js index ee1d5318..02f9c06e 100644 --- a/core/horizontal_menu_view.js +++ b/core/horizontal_menu_view.js @@ -71,7 +71,7 @@ function HorizontalMenuView(options) { text = focusItem ? focusItem.text : item.text; sgr = ''; } else if(this.complexItems) { - text = pipeToAnsi(formatString(item.focused ? this.focusItemFormat : this.itemFormat, item)); + text = pipeToAnsi(formatString(item.focused && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item)); sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()); } else { text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); diff --git a/core/menu_view.js b/core/menu_view.js index 56899077..f15491cf 100644 --- a/core/menu_view.js +++ b/core/menu_view.js @@ -64,6 +64,8 @@ util.inherits(MenuView, View); MenuView.prototype.setItems = function(items) { if(Array.isArray(items)) { + this.sorted = false; + // // Items can be an array of strings or an array of objects. // @@ -91,13 +93,38 @@ MenuView.prototype.setItems = function(items) { }); if(this.complexItems) { - this.itemFormat = this.itemFormat || '{text}'; - this.focusItemFormat = this.focusItemFormat || this.itemFormat; + this.itemFormat = this.itemFormat || '{text}'; } } }; +MenuView.prototype.setSort = function(sort) { + if(this.sorted || !Array.isArray(this.items) || 0 === this.items.length) { + return; + } + + const key = true === sort ? 'text' : sort; + if('text' !== sort && !this.complexItems) { + return; // need a valid sort key + } + + this.items.sort( (a, b) => { + const a1 = a[key]; + const b1 = b[key]; + if(!a1) { + return -1; + } + if(!b1) { + return 1; + } + return a1.localeCompare( b1, { sensitivity : false, numeric : true } ); + }); + + this.sorted = true; +}; + MenuView.prototype.removeItem = function(index) { + this.sorted = false; this.items.splice(index, 1); if(this.focusItems) { @@ -203,6 +230,8 @@ MenuView.prototype.setPropertyValue = function(propName, value) { case 'focusItemFormat' : this[propName] = value; break; + + case 'sort' : this.setSort(value); break; } MenuView.super_.prototype.setPropertyValue.call(this, propName, value); diff --git a/core/vertical_menu_view.js b/core/vertical_menu_view.js index e1d6cd17..6f083193 100644 --- a/core/vertical_menu_view.js +++ b/core/vertical_menu_view.js @@ -71,7 +71,7 @@ function VerticalMenuView(options) { text = focusItem ? focusItem.text : item.text; sgr = ''; } else if(this.complexItems) { - text = pipeToAnsi(formatString(item.focused ? this.focusItemFormat : this.itemFormat, item)); + text = pipeToAnsi(formatString(item.focused && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item)); sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()); } else { text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);