Skip to content

Fix admin issues from #79 #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions switchboard/admin/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
.switchboard a:hover { color: #fff; background-color: #666; text-decoration: none; }
.switchboard a:active { color: #fff; background-color: #555; text-decoration: none; }
.switchboard .hidden { display: none; }
.switchboard .grayed-out { opacity: 0.2; }
.switchboard .gray-slight { opacity: 0.7; }
.switchboard .btn,
.switchboard .btn:link,
.switchboard .btn:visited {
Expand Down Expand Up @@ -373,7 +375,7 @@
</script>

<script type="text/x-handlebars-template" id="switchData">
<div id="id_{{key}}" class="switch" data-switch-key="{{key}}" data-switch-label="{{label}}" data-switch-description="{{description}}" data-switch-status="{{status}}">
<div id="id_{{key}}" class="switch" data-switch-key="{{key}}" data-switch-label="{{label}}" data-switch-description="{{description}}" data-switch-status="{{status}}" style="order:{{ cssOrder }}">
<div class="status">
<label for="status_{{key}}">Status</label>
<select name="status_{{key}}">
Expand Down Expand Up @@ -474,23 +476,13 @@
</script>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.8/handlebars.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.js"></script>
<script type="text/javascript">
/*!
* string_score.js: String Scoring Algorithm 0.1.10
*
* http://joshaven.com/string_score
* https://github.com/joshaven/string_score
*
* Copyright (C) 2009-2011 Joshaven Potter <[email protected]>
* Special thanks to all of the contributors listed here https://github.com/joshaven/string_score
* MIT license: http://www.opensource.org/licenses/mit-license.php
*
* Date: Tue Mar 1 2011
*/
String.prototype.score=function(m,s){if(this==m){return 1}if(m==""){return 0}var f=0,q=m.length,g=this,p=g.length,o,k,e=1,j;for(var d=0,r,n,h,a,b,l;d<q;++d){h=m.charAt(d);a=g.indexOf(h.toLowerCase());b=g.indexOf(h.toUpperCase());l=Math.min(a,b);n=(l>-1)?l:Math.max(a,b);if(n===-1){if(s){e+=1-s;continue}else{return 0}}else{r=0.1}if(g[n]===h){r+=0.1}if(n===0){r+=0.6;if(d===0){o=1}}else{if(g.charAt(n-1)===" "){r+=0.8}}g=g.substring(n+1,p);f+=r}k=f/q;j=((k*(q/p))+k)/2;j=j/e;if(o&&(j+0.15<1)){j+=0.15}return j};
// String Scoring Algorithm 0.1.22 | (c) 2009-2015 Joshaven Potter <[email protected]>
// MIT License: http://opensource.org/licenses/MIT | https://github.com/joshaven/string_score
String.prototype.score=function(e,f){if(this===e)return 1;if(""===e)return 0;var d=0,a,g=this.toLowerCase(),n=this.length,h=e.toLowerCase(),k=e.length,b;a=0;var l=1,m,c;f&&(m=1-f);if(f)for(c=0;c<k;c+=1)b=g.indexOf(h[c],a),-1===b?l+=m:(a===b?a=.7:(a=.1," "===this[b-1]&&(a+=.8)),this[b]===e[c]&&(a+=.1),d+=a,a=b+1);else for(c=0;c<k;c+=1){b=g.indexOf(h[c],a);if(-1===b)return 0;a===b?a=.7:(a=.1," "===this[b-1]&&(a+=.8));this[b]===e[c]&&(a+=.1);d+=a;a=b+1}d=.5*(d/n+d/k)/l;h[0]===g[0]&&.85>d&&(d+=.15);return d};
</script>
<script type="text/javascript">
/*global alert, confirm, jQuery, Handlebars, SWITCHBOARD */
Expand Down Expand Up @@ -724,9 +716,9 @@
$('.switches', $sb).on('submit', '.conditions-form form', function(e) {
e.preventDefault();
var $form = $(this);

var $switch = $form.parents('.switch:first');
var data = {
key: $form.parents('.switch:first').attr('data-switch-key'),
key: $switch.attr('data-switch-key'),
id: $form.attr('data-switch'),
field: $form.attr('data-field')
};
Expand All @@ -744,6 +736,7 @@
});

api(SWITCHBOARD.addCondition, data, function (swtch) {
swtch.cssOrder = $switch.css('order');
var result = templates.switchData(swtch);
$('.switches .switch[data-switch-key="' + data.key + '"]', $sb).replaceWith(result);
});
Expand All @@ -753,15 +746,16 @@
e.preventDefault();

var $el = $(this).parents('span:first');

var $switch = $el.parents('.switch:first');
var data = {
key: $el.parents('.switch:first').attr('data-switch-key'),
key: $switch.attr('data-switch-key'),
id: $el.attr('data-switch'),
field: $el.attr('data-field'),
value: $el.attr('data-value')
};

api(SWITCHBOARD.delCondition, data, function (swtch) {
swtch.cssOrder = $switch.css('order');
var result = templates.switchData(swtch);
$('.switches .switch[data-switch-key="' + data.key + '"]').replaceWith(result);
});
Expand Down Expand Up @@ -814,6 +808,7 @@
},

function (swtch) {
swtch.cssOrder = -999999;
var result = templates.switchData(swtch);

if (action === 'add') {
Expand Down Expand Up @@ -842,8 +837,8 @@

$('input[type=search]').keyup(function () {
var query = $(this).val();
$('.switches .switch', $sb).removeClass('hidden').css('order', 0);
if (!query) {
$('.switches .switch', $sb).removeClass('grayed-out').removeClass('gray-slight').css('order', 0);
return;
}
$('.switches .switch', $sb).each(function (_, el) {
Expand All @@ -855,15 +850,15 @@
score += $el.attr('data-switch-description').score(query) * 5;
}
score += $('.conditions', $el).text().score(query);
if (score === 0) {
$el.addClass('hidden');
}
// $el.toggleClass('hidden', score === 0); // hiding (in any way) the first time triggers very poor performance in Firefox's FormAutofillHeuristics.sys.mjs
$el.toggleClass('grayed-out', score === 0);
$el.toggleClass('gray-slight', score < 2 && score !== 0);
$el.css('order', Math.round(-score*1000)); // smallest first; convert to large ints
});
});

window.addEventListener("keydown", (e) => {
if (!$('input[type=search]').is(':focus') && (e.code === 'F3' || e.code === 'Slash' || ((e.ctrlKey || e.metaKey) && e.code === 'KeyF'))) {
if (!$('input[type=search]').is(':focus') && (e.code === 'F3' || (e.code === 'Slash' && document.activeElement === document.body) || ((e.ctrlKey || e.metaKey) && e.code === 'KeyF'))) {
e.preventDefault();
$('input[type=search]').focus();
}
Expand Down