Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
169 lines (128 sloc) 5.42 KB
part of m4d_dialog;
// Store strings for class names defined by this component that are used in
// Dart. This allows us to simply change it in one place should we
// decide to modify at a later date.
//class _MaterialSnackbarCssClasses {
//
// final String WSK_SNACKBAR_CONTAINER = 'mdl-snackbar__container';
//
// final String IS_VISIBLE = 'is-visible';
// final String IS_HIDDEN = 'is-hidden';
//
// const _MaterialSnackbarCssClasses();
//}
class _SnackbarConfig extends DialogConfig {
_SnackbarConfig() : super(rootTagInTemplate: "mdl-snackbar",
closeOnBackDropClick: false,
autoClosePossible: true,
openAnimation: new MdlAnimation.fromStock(StockAnimation.NoOp));
}
/// Position on Screen or in container
class SnackbarPosition {
bool _top = false;
bool _right = false;
bool _bottom = true;
bool left = true;
bool get top => _top || bottom ? _top : true;
bool get right => _right || left ? _right : true;
bool get bottom => _bottom && _top ? false : _bottom;
set top(bool value) => _top = value;
set bottom(bool value) => _bottom = value;
set right(bool value) => _right = value;
}
/// MaterialSnackbarComponent
class MaterialSnackbar extends MaterialDialog {
static final Logger _logger = new Logger('mdldialog.MaterialSnackbar');
static const String DEFAULT_CONFIRM_BUTTON = "OK";
static const int LONG_DELAY = 3500;
static const int SHORT_DELAY = 2000;
/// If Snackbar has a confirmButton this is set to a valid dialog-ID
String _confirmationID = "";
/// Position on Screen or in container
final SnackbarPosition position = new SnackbarPosition();
String text = "";
String confirmButton = "";
int timeout = SHORT_DELAY;
MaterialSnackbar() : super(new _SnackbarConfig()) {
_config.onCloseCallbacks.add(_onCloseCallback);
//lambdas["classes"] = _snackbarClasses;
}
MaterialSnackbar call(final String text, { final String confirmButton: "" }) {
Validate.notBlank(text);
Validate.notNull(confirmButton);
Validate.isTrue(_confirmationID.isEmpty,"A Snackbar waits for confirmation, but the next one is already in the queue! ($_confirmationID)");
this.text = text;
this.confirmButton = confirmButton;
return this;
}
/// The current Snackbar waits for user interaction
bool get waitingForConfirmation => _confirmationID.isNotEmpty;
@override
/// if there is already a Snackbar open - it will be closed
Future<MdlDialogStatus> show({ Duration timeout, FutureOr onDialogInit(final String dialogId) }) {
Validate.isTrue(!waitingForConfirmation,"There is alread a Snackbar waiting for confirmation!!!!");
return close(MdlDialogStatus.CLOSED_VIA_NEXT_SHOW).then( (_) {
if(!_hasConfirmButton) {
if(timeout == null) {
timeout = new Duration(milliseconds: SHORT_DELAY);
}
return super.show(timeout: timeout);
}
return super.show(onDialogInit: _init );
});
}
// - EventHandler -----------------------------------------------------------------------------
void _onClose() {
Validate.notBlank(_confirmationID, "onClose must have a _confirmationID set - but was blank");
close(MdlDialogStatus.CONFIRMED);
}
// - Template ---------------------------------------------------------------------------------
@override
String get template => """
<div class="mdl-snackbar ${_snackbarClasses()}">
<span class="mdl-snackbar__flex">${text}</span>
${_hasConfirmButton ? '''
<button class="mdl-button mdl-js-button mdl-button--colored" data-mdl-click="onClose()" autofocus>
${confirmButton}
</button>''' : ''}
</div>
""";
@override
Map<String, Function> get events {
return <String,Function>{
"onClose" : () => _onClose()
};
}
// - private ----------------------------------------------------------------------------------
void _onCloseCallback(final MaterialDialog dialogElement, final MdlDialogStatus status) {
_logger.fine("onCloseCallback, ID: ${dialogElement.id}, $status, ConfirmationID: $_confirmationID");
if(_confirmationID.isNotEmpty && dialogElement.id == _confirmationID) {
_clearConfirmationCheck();
}
}
/// Its important to know the ID of the dialog that needs a confirmation - otherwise another
/// dialog could reset the {_needsConfirmation} flag
Future _init(final String id) async {
Validate.notBlank(id);
_confirmationID = id;
}
void _clearConfirmationCheck() {
_confirmationID = "";
}
/// The template checks it it should show a button or not
bool get _hasConfirmButton => confirmButton != null && confirmButton.isNotEmpty;
String _snackbarClasses() {
final List<String> classes = new List<String>();
void _addIf(final List<String> classes,final bool check,final String classToAdd) {
if(check) {
classes.add(classToAdd);
}
}
_addIf(classes,position.top,"mdl-snackbar--top");
_addIf(classes,position.right,"mdl-snackbar--right");
_addIf(classes,position.left,"mdl-snackbar--left");
_addIf(classes,position.bottom,"mdl-snackbar--bottom");
_addIf(classes,waitingForConfirmation,"waiting-for-confirmation");
return classes.join(" ");
}
}