通知 Notify

全局通知,作为消息提示的增强版,能够自动淡出。

示例

基本形式

<button class="u-btn" on-click={this.show()}>Default</button>
<button class="u-btn" on-click={this.showWithoutTitle()}>Without Title</button>
let component = new RGUI.Component({
    template,
    show() {
        RGUI.Notify.show('This is a message.', 'Notify');
    },
    showWithoutTitle() {
        RGUI.Notify.show('This is a message without title.');
    },
});

状态扩展

<button class="u-btn u-btn-info" on-click={this.show('info')}>info</button>
<button class="u-btn u-btn-success" on-click={this.show('success')}>success</button>
<button class="u-btn u-btn-warning" on-click={this.show('warning')}>warning</button>
<button class="u-btn u-btn-error" on-click={this.show('error')}>error</button>
let component = new RGUI.Component({
    template,
    show(state) {
        RGUI.Notify[state](state + ' message', 'Notify');
    },
});

位置扩展

<button class="u-btn" on-click={this.show(0)}>top-center</button>
<button class="u-btn" on-click={this.show(1)}>top-left</button>
<button class="u-btn" on-click={this.show(2)}>top-right</button>
<button class="u-btn" on-click={this.show(3)}>bottom-center</button>
<button class="u-btn" on-click={this.show(4)}>bottom-left</button>
<button class="u-btn" on-click={this.show(5)}>bottom-right</button>
let component = new RGUI.Component({
    template,
    config() {
        this.notifies = [
            new RGUI.Notify({ data: { position: 'top-center' } }),
            new RGUI.Notify({ data: { position: 'top-left' } }),
            new RGUI.Notify({ data: { position: 'top-right' } }),
            new RGUI.Notify({ data: { position: 'bottom-center' } }),
            new RGUI.Notify({ data: { position: 'bottom-left' } }),
            new RGUI.Notify({ data: { position: 'bottom-right' } }),
        ];
    },
    show(index) {
        var notify = this.notifies[index];
        notify.show('position: ' + notify.data.position, 'Notify');
    },
});

嵌入文档流

上面的notify都是以fixed的形式固定在浏览器中,也可以将Notify嵌入文档流。先将notify注入到需要的位置,然后设置notify的position="static"

<p><button class="u-btn u-btn-primary" on-click={this.show()}>static</button></p>
<notify ref="notify" position="static" duration="0" />
let component = new RGUI.Component({
    template,
    show() {
        this.$refs.notify.show('static notify');
    },
});

消息停留时间

可以通过设置notify的duration参数设置所有消息的停留时间,也可以在show的时候单独设置该条消息的停留时间,单位为毫秒。

<button class="u-btn" on-click={this.show(500)}>0.5s</button>
<button class="u-btn" on-click={this.show(1000)}>1s</button>
<button class="u-btn" on-click={this.show(2000)}>2s</button>
<button class="u-btn" on-click={this.show(0)}>常驻</button>
let component = new RGUI.Component({
    template,
    show(duration) {
        RGUI.Notify.show('duration: ' + duration + ' ms', 'Notify', duration);
    },
});

始终显示一条

single设置为true,可以让notify始终只显示一条消息。

<button class="u-btn u-btn-info" on-click={this.show('info')}>Info</button>
<button class="u-btn u-btn-success" on-click={this.show('success')}>Success</button>
<button class="u-btn u-btn-warning" on-click={this.show('warning')}>Warning</button>
<button class="u-btn u-btn-error" on-click={this.show('error')}>Error</button>
let component = new RGUI.Component({
    template,
    config() {
        this.notify = new RGUI.Notify({ data: {single: true} });
    },
    number: 1,
    show: function(state) {
        this.notify[state]('message ' + this.number, 'Notify');
        this.number++;
    }
});

API

Class

Notify

继承自

Options

new Notify()

参数类型默认值数据流向描述
dataObject绑定属性
data.positionstring'top-right'outside => inside通知的位置,可选参数:`top-center`、`top-left`、`top-right`、`bottom-center`、`bottom-left`、`bottom-right`、`static`
data.durationnumber3000outside => inside每条通知默认的停留毫秒数,如果为0,则表示通知常驻不消失。
data.singlebooleanfalseoutside => inside是否始终显示一条
data.visiblebooleantrueoutside => inside是否显示
data.classstring''outside => inside补充class

Methods

notify.show(content[, title][, duration][, state])

弹出一个通知

参数类型默认值描述
contentstring''通知内容
titlestring''通知标题
durationnumbernotify.duration该条通知的停留毫秒数。如果为0,则表示通知常驻不消失。如果不填,则使用notify默认的duration。
statestring''通知状态,可选参数:`info`、`success`、`warning`、`error`
返回值类型描述
itemobject返回弹出的通知项

notify.close(item)

关闭某条通知

参数类型默认值描述
itemobject需要关闭的通知项
返回值类型描述
无返回值

notify.closeAll()

关闭所有通知

参数类型默认值描述
无参数
返回值类型描述
无返回值

notify.[info|success|warning|error](content[, title][, duration])

弹出特殊类型的通知。为show方法的简写方式。

参数类型默认值描述
contentstring''通知内容
titlestring''通知标题
durationnumbertoast.duration该条通知的停留毫秒数。如果为0,则表示通知常驻不消失。如果不填,则使用toast默认的duration。
返回值类型描述
无返回值