sciactive / pnotify

Beautiful JavaScript notifications with Web Notifications support.
https://sciactive.com/pnotify/
Apache License 2.0
3.65k stars 513 forks source link

Not worked with webpacker #320

Closed zw963 closed 6 years ago

zw963 commented 6 years ago

webpacker is a ruby on rails wrapper for webpack, so, i think webpacker config is same as webpack, what i expect is output a nice look tips on browser top-right corner.

following is my config for pnotify.

let PNotify = require("pnotify");
window.PNotify = PNotify;
require('pnotify/dist/pnotify.buttons.js');
require('pnotify/dist/pnotify.desktop.js');

$(function () {
    PNotify.reload(window);
});

I try any i know es6 load method to try to make this work, but not.

following is firefox log:

new PNotify({ title: "温馨提示", text: "保存成功!", delay: 5000, type: "success" })
{…}
animTimer: 497
animating: "in"
container: Object { 0: div.brighttheme.ui-pnotify-container.brighttheme-success.ui-pnotify-shadow, length: 1, prevObject: […] }
elem: Object [ div.ui-pnotify.ui-pnotify-fade-normal.ui-pnotify-move.ui-pnotify-in.ui-pnotify-fade-in ]
modules: Object { buttons: {…}, desktop: {…} }
options: Object { title: "温馨提示", title_escape: false, text: "保存成功!", … }
state: "opening"
styles: Object { container: "brighttheme", notice: "brighttheme-notice", notice_icon: "brighttheme-icon-notice", … }
text_container: Object { 0: div.ui-pnotify-text, length: 1, prevObject: […] }
timer: null
timerHide: false
title_container: Object { 0: h4.ui-pnotify-title, length: 1, prevObject: […] }
zw963 commented 6 years ago

It worked now, because not import style files.

But, still not trigger for Rails applications with turbolinks enabled.

turbolinks have it's own version document.ready, we need invoke PNotify js init code in special callback to make it worked.

following is a example

let PNotify = require('pnotify');
require('pnotify/dist/pnotify.buttons');
window.PNotify = PNotify;

function init () {
    $(".ui.dropdown").dropdown();
    DragScroll.reset();
    // Like DragScroll.reset(), we need load that myself for turbolinks to work.
    // But following trigger code is not worked.
    PNotify.reload(window);
}

if (Turbolinks.supported) {
    // trubolinks use following callback to trigger js init code.
    document.addEventListener("turbolinks:load", function () {
        init();
    });
} else {
    $(function () {
        init();
    });
}
hperrin commented 6 years ago

Nice workaround. :)