Skip to content

Commit

Permalink
Disable buttons until enabled (#1298)
Browse files Browse the repository at this point in the history
* When JavaScript is disabled no point in showing active
* Reordered a bit in case Ace is missing for any reason... keep disabled

Post #437

Auto-merge
  • Loading branch information
Martii authored Dec 1, 2017
1 parent 2538695 commit 0190b3b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 90 deletions.
181 changes: 96 additions & 85 deletions views/includes/scripts/scriptEditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
'use strict';

$(document).ready(function () {
var editor = ace.edit("editor");
var editor = null;
var wrap = $('#wrap');
var beautify = $('#beautify');
var rawSource = editor.getSession().getValue();
var rawSource = null;
var isBlocking = null;
var hasChanged = false;
var placeholder = null;
Expand All @@ -17,8 +17,7 @@
var now = new Date();
var year = now.getFullYear();

editor.$blockScrolling = Infinity;

//
function hasRelative(aPrefix) {
var hasCalc = null;
var hasUnitV = null;
Expand Down Expand Up @@ -128,89 +127,101 @@
aEv.target.blur();
}

editor.setTheme("ace/theme/dawn");
editor.getSession().setMode("ace/mode/javascript");

{{#readOnly}}editor.setReadOnly(true);{{/readOnly}}
{{^readOnly}}
// TODO: Submit using AJAX
$('#submit_code').click(function () {
$('#source').val(editor.getValue());
$('#code_form').submit();
});
{{/readOnly}}

// Some older and newer browser JavaScript work-around for #136
if (!hasOurRelative()) {
$("#editor").height(calcHeight());

if (window.addEventListener) {
window.addEventListener('resize', onresize, false);
} else if (window.attachEvent) {
window.attachEvent('resize', onresize);
//
if (typeof ace !== 'undefined') {
placeholder = (
isLib
? [
'// ==UserScript==',
'// @namespace https://openuserjs.org/users/' + username,
'// @exclude *',
'',
'// ==UserLibrary==',
'// @name Getting Started with a User Library',
'// @description ' +
'Showing the current basic and recommended format for a Library script.',
'// @copyright ' + year + ', ' + username
+ ' (https://openuserjs.org/users/' + username + ')',
'// @license OSI-SPDX-Short-Identifier',
'// @version 0.0.0',
'',
'// ==/UserScript==',
'',
'// ==/UserLibrary==',
'',
'// ==OpenUserJS==',
'// @author ' + username,
'// ==/OpenUserJS==',
'',
'/**',
' *',
' * Please begin typing or paste your User Library now.',
' *',
' */'
] : [
'// ==UserScript==',
'// @namespace https://openuserjs.org/users/' + username,
'// @name Getting Started with a User Script',
'// @description ' +
'Showing the current basic and recommended format for a User script.',
'// @copyright ' + year + ', ' + username
+ ' (https://openuserjs.org/users/' + username + ')',
'// @license OSI-SPDX-Short-Identifier',
'// @version 0.0.0',
'// @include https://www.example.com/*',
'// @grant none',
'// ==/UserScript==',
'',
'// ==OpenUserJS==',
'// @author ' + username,
'// ==/OpenUserJS==',
'',
'/**',
' *',
' * Please begin typing or paste your User script now.',
' *',
' */'
]
).join('\n');

editor = ace.edit("editor");
rawSource = editor.getSession().getValue();

editor.$blockScrolling = Infinity;
wrap.removeAttr('disabled');
beautify.removeAttr('disabled');

$('#submit_code').removeAttr('disabled');

editor.setTheme("ace/theme/dawn");
editor.getSession().setMode("ace/mode/javascript");

{{#readOnly}}editor.setReadOnly(true);{{/readOnly}}
{{^readOnly}}
// TODO: Submit using AJAX
$('#submit_code').click(function () {
$('#source').val(editor.getValue());
$('#code_form').submit();
});
{{/readOnly}}

// Some older and newer browser JavaScript work-around for #136
if (!hasOurRelative()) {
$("#editor").height(calcHeight());

if (window.addEventListener) {
window.addEventListener('resize', onresize, false);
} else if (window.attachEvent) {
window.attachEvent('resize', onresize);
}
}
}

placeholder = (
isLib
? [
'// ==UserScript==',
'// @namespace https://openuserjs.org/users/' + username,
'// @exclude *',
'',
'// ==UserLibrary==',
'// @name Getting Started with a User Library',
'// @description ' +
'Showing the current basic and recommended format for a Library script.',
'// @copyright ' + year + ', ' + username
+ ' (https://openuserjs.org/users/' + username + ')',
'// @license OSI-SPDX-Short-Identifier',
'// @version 0.0.0',
'',
'// ==/UserScript==',
'',
'// ==/UserLibrary==',
'',
'// ==OpenUserJS==',
'// @author ' + username,
'// ==/OpenUserJS==',
'',
'/**',
' *',
' * Please begin typing or paste your User Library now.',
' *',
' */'
] : [
'// ==UserScript==',
'// @namespace https://openuserjs.org/users/' + username,
'// @name Getting Started with a User Script',
'// @description ' +
'Showing the current basic and recommended format for a User script.',
'// @copyright ' + year + ', ' + username
+ ' (https://openuserjs.org/users/' + username + ')',
'// @license OSI-SPDX-Short-Identifier',
'// @version 0.0.0',
'// @include https://www.example.com/*',
'// @grant none',
'// ==/UserScript==',
'',
'// ==OpenUserJS==',
'// @author ' + username,
'// ==/OpenUserJS==',
'',
'/**',
' *',
' * Please begin typing or paste your User script now.',
' *',
' */'
]
).join('\n');

editor.on('input', oninput);
editor.on('change', onchange);
wrap.on('click', onwrap);
beautify.on('click', onbeautify);
setTimeout(oninput, 250);
editor.on('input', oninput);
editor.on('change', onchange);
wrap.on('click', onwrap);
beautify.on('click', onbeautify);
setTimeout(oninput, 250);
}
});

})();
Expand Down
10 changes: 5 additions & 5 deletions views/pages/scriptViewSourcePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
<input type="hidden" name="url" value="{{{url}}}" />
{{^owner}}<input type="hidden" name="original" value="{{original}}" />{{/owner}}
<div class="btn-toolbar">
<button class="btn btn-success" type="button" id="wrap">Wrap</button>
<button class="btn btn-success" type="button" id="beautify">Beautify</button>
<button class="btn btn-success pull-right" type="submit" id="submit_code">{{#owner}}<i class="fa fa-fw fa-save"></i>{{/owner}}{{^owner}}<i class="fa fa-fw fa-code-fork"></i>{{/owner}} Submit Code{{^owner}} as Fork{{/owner}}</button>
<button class="btn btn-success" type="button" id="wrap" disabled="disabled">Wrap</button>
<button class="btn btn-success" type="button" id="beautify" disabled="disabled">Beautify</button>
<button class="btn btn-success pull-right" type="submit" id="submit_code" disabled="disabled">{{#owner}}<i class="fa fa-fw fa-save"></i>{{/owner}}{{^owner}}<i class="fa fa-fw fa-code-fork"></i>{{/owner}} Submit Code{{^owner}} as Fork{{/owner}}</button>
</div>
</form>
{{/readOnly}}
{{#readOnly}}
<div class="btn-toolbar">
<button class="btn btn-success" type="button" id="wrap">Wrap</button>
<button class="btn btn-success" type="button" id="beautify">Beautify</button>
<button class="btn btn-success" type="button" id="wrap" disabled="disabled">Wrap</button>
<button class="btn btn-success" type="button" id="beautify" disabled="disabled">Beautify</button>
</div>
{{/readOnly}}
</div>
Expand Down

0 comments on commit 0190b3b

Please sign in to comment.