-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
v2.7.0
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## The Disable Macro | ||
|
||
[Back to the main readme](./README.md). | ||
|
||
The `<<disable>>` macro allows the user to disable (and re-enable) various interactive elements. | ||
|
||
**THE CODE:** [Minified](https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/scripts/minified/disable.min.js). [Pretty](https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/scripts/disable.js). | ||
**DEMO:** [Available](http://macros.twinelab.net/demo?macro=disable). | ||
**GUIDE:** Not available. | ||
|
||
### Macro: `<<disable>>` | ||
|
||
**Syntax**:`<<disable [expression]>>...<</disable>>` | ||
|
||
The first interactive element (such as a button or text input) between the macro tags will be disabled. If an expression is passed to the macro, it will be evaluated. If the evaluated expression yields a truthy result, the interactive element will be disabled. If the expression is falsy, the element will not be disabled. | ||
|
||
**Usage**: | ||
|
||
``` | ||
This button should be disabled: | ||
<<disable>><<button "Submit">><</button>><</disable>> | ||
This textbox should also be disabled: | ||
<<disable 1 === 1>><<textbox "$name" "">><</disable>> | ||
This listbox, too: | ||
<<disable true>><<listbox '$whee'>><<optionsfrom ["1", "2", "3"]>><</listbox>><</disable>> | ||
This button should ''not'' be disabled: | ||
<<disable false>><<button "Submit">><</button>><</disable>> | ||
This textbox should ''not'' be disabled: | ||
<<disable 1 === 0>><<textbox "$name" "">><</disable>> | ||
``` | ||
|
||
## Other usage notes: | ||
|
||
Disabled elements are also given the class `disabled`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(function () { | ||
// disable.js, by chapel, for sugarcube 2 | ||
// v1.0.0 | ||
|
||
'use strict'; | ||
|
||
var cls = 'disabled'; | ||
var interactive = ['button', 'fieldset', 'input', 'menuitem', 'optgroup', 'option', 'select', 'textarea']; | ||
|
||
function getEl (self) { | ||
// get the first interactive element | ||
var $el = $(self).find(interactive.join(',')).first(); | ||
if (!$el[0]) { | ||
$el = $(self).children().eq(0); | ||
if (!$el[0]) { | ||
return $(self); | ||
} | ||
} | ||
return $el; | ||
} | ||
|
||
function changeCls ($el) { | ||
if ($el.ariaIsDisabled()) { | ||
$el.addClass(cls); | ||
} else { | ||
$el.removeClass(cls); | ||
} | ||
} | ||
|
||
function disable ($el, bool) { | ||
if (!($el instanceof $)) { | ||
$el = $($el); | ||
} | ||
$el.ariaDisabled((bool === undefined) ? true : !!bool); | ||
changeCls($el); | ||
return $el; | ||
} | ||
|
||
// no need for JS API as there us a built-in jQuery extension | ||
|
||
Macro.add('disable', { | ||
tags : null, | ||
handler : function () { | ||
var bool, $wrapper = $(document.createElement('span')) | ||
.addClass('macro-' + this.name) | ||
.wiki(this.payload[0].contents); | ||
|
||
try { | ||
bool = this.args.raw.trim() ? !!Scripting.evalJavaScript(this.args.full) : undefined; | ||
} catch (err) { | ||
return this.error("bad evaluation: " + err.message); | ||
} | ||
|
||
disable(getEl($wrapper), bool); | ||
|
||
// output | ||
$(this.output).append($wrapper); | ||
} | ||
}); | ||
|
||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// done.min.js, for SugarCube 2, by Chapel | ||
// v1.0.1, 2020-08-02, 1d6b869ee7a4b36293aaf4003bfe47ffdfbdeba8 | ||
// v1.0.1, 2020-11-24, 9f958db927f80b0101af14e286ea0ad142026739 | ||
;!function(){var a=1;Macro.add("done",{skipArgs:!0,tags:null,handler:function(){var n=this.payload[0].contents.trim();""!==n&&(postdisplay[":chapel-done-macro-"+a]=function(a){delete postdisplay[a],$.wiki(n)},a++)}})}(); | ||
// end done.min.js |