Giter VIP home page Giter VIP logo

Comments (17)

hperrin avatar hperrin commented on May 16, 2024

I marked this as won't fix because it's pretty easy to accomplish using just standard jQuery. I am going to add this new example to demonstrate how to do it:

<button class="btn source" onclick="var notice = $.pnotify({
    title: 'Click Notice',
    text: 'I wish someone would click me.'
}).click(function(e){
    if ($(e.target).is('.ui-pnotify-closer *, .ui-pnotify-sticker *'))
        return;
    notice.pnotify({type: 'success', text: 'Yay, you clicked me!&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Happy_smiley_face.png/240px-Happy_smiley_face.png&quot; /&gt;&lt;/div&gt;'});
});">Click Notice</button>

from pnotify.

aensley avatar aensley commented on May 16, 2024

That. Is Beautiful. Thank you for the code example! I really appreciate it.

from pnotify.

aensley avatar aensley commented on May 16, 2024

Ok one quick question: Is there any way to reference the $.pnotify object anonymously inside the event handler? The reason I ask is because it is not reasonable to be able to create a separate variable for each notification and keep track of it in my application.

Normally, I would use this or $(this), but in the .click() event handler in your example, this simply refers to the div and I don't have access to the pnotify properties or methods (like .pnotify_remove() for example).

Maybe I'm just missing something really simple. Anyway, I appreciate your help.

from pnotify.

edgardmessias avatar edgardmessias commented on May 16, 2024

My solution to this:

var aaa = $.pnotify();
aaa.click(function(){
  aaa.pnotify_remove();
});

from pnotify.

aensley avatar aensley commented on May 16, 2024

@edgardmessias: That won't work for me. In my app, notifications are being created dynamically, and it is not possible for me to assign them to variables and keep track of them. For what I'm suggesting, I really need an anonymous way to access the pnotify object (i.e. via $(this)).

I've made a hack that works in my specific situation, but it's a little ugly in my opinion:

var closeButtonSelector = '.ui-pnotify-closer *';
$.pnotify({
    title: 'Test notification',
    text: 'Click me to dismiss'
}).click(function(e){
    if($(e.target).is(closeButtonSelector + ', .ui-pnotify-sticker *'))
    {
        return;
    }
    var $n = $(this), closer = $n.siblings(closeButtonSelector);
    if(closer.length > 0)
    {
        // The close button is a sibling
        closer.click();
    }
    else
    {
        // The close button is a descendant
        $n.find(closeButtonSelector).click();
    }
});

Anyway, this isn't a deal breaker for me. It's just something that would be nice. Thank you for your work on this great plugin already.

from pnotify.

hperrin avatar hperrin commented on May 16, 2024

I hate to say it, but the only way I can find to do this now is this ugly mess:

$.grep($(window).data('pnotify'), function(n){
    return n.is(e.currentTarget);
})[0].pnotify({type: 'success', text: 'blah blah'});

Put that in the click function and it will work. I'll definitely make a better way to do this in the next version.

from pnotify.

hperrin avatar hperrin commented on May 16, 2024

Can you wrap the whole thing in an anonymous function?

(function(){
    var notice = $.pnotify({
        title: 'Click Notice',
        text: 'I wish someone would click me.'
    }).click(function(e){
        if ($(e.target).is('.ui-pnotify-closer *, .ui-pnotify-sticker *'))
            return;
        notice.pnotify({type: 'success', text: 'blah blah'});
    });
})();

That way the variable is created only in the anonymous function's scope.

from pnotify.

aensley avatar aensley commented on May 16, 2024

Genius! Yes, that works. Here's my code which does the same as my previous example with much less code.

(function(){
    var notice = $.pnotify({
        title: 'Test notification',
        text: 'Click me to dismiss'
    }).click(function(e){
        if($(e.target).is('.ui-pnotify-closer *, .ui-pnotify-sticker *'))
        {
            return;
        }
        notice.pnotify_remove();
    });
})();

That just feels cleaner and more efficient. Thanks for your help!

from pnotify.

hperrin avatar hperrin commented on May 16, 2024

No problem. :)

from pnotify.

folkyatina avatar folkyatina commented on May 16, 2024

Is there any easy method to close notification by click but for all notices by default?

from pnotify.

tquila-andy avatar tquila-andy commented on May 16, 2024

This is fantastic, thanks. Would be really helpful to promote this on the demo page.

from pnotify.

koppor avatar koppor commented on May 16, 2024

When one assumes that $.find does not take a long time and pnotify creations should kept short, following code adds the close functionality to all pnotifies:

$(document).on("click", ".ui-pnotify", function(e) {
    if (e.originalEvent === undefined) {
        // hit the second time
        // propagate to pnotify
        return true;
    }
    if ($(e.target).is('.ui-pnotify-closer *, .ui-pnotify-sticker *')) {
        // closer or sticker have been directly clicked, just propagate this event
        return true;
    }
    $(this).find(".ui-pnotify-closer").click();
    return false;
});

from pnotify.

TravelingTechGuy avatar TravelingTechGuy commented on May 16, 2024

I realize this may have been addressed long ago, but this is how I handled notification closing. I'm using Socket.io to push notifications to the client, and I wanted to notify the server whenever the user closed ('acknowledged') a notification:

    socket.on('notify', function(data) {
        console.log('notify', data);
        notify(data);
    });

        var notify = function(data) {
            var notification = {
                title: data.time,
                text: data.id + ': ' + data.message,
                type: (data.error) ? 'error' : 'success'
            };
            $.pnotify(notification).on('click', function(e) {
                var action = $(e.target).attr('title').toLowerCase();
                notificationClicked(action, data.id);
            });
        };

        var notificationClicked = function(action, id) {
            if(action === 'close') {
                console.log('notification %s acknowledged', id);
                socket.emit('acknowledged', id);
            }
            else if(action === 'stick') {
                //handle pause click, if needed
            }
            else {
                //handle FFU?
            }
        };

from pnotify.

reybb avatar reybb commented on May 16, 2024

Hello
I try to do the same thing with an array, but it is not working... When I click over the notices they said that notices[i] it is undefined(Firefox) ...

function showNotify (){
for (var i = 0; i < myNotificationList.length; i++) {
notices[i] = $.pnotify({
title: myNotificationList[i]['myTheSuject'],
text: myNotificationList[i]['text'],
type: myNotificationList[i]['type'],
hide: false,
closer: false,
sticker: false
}).attr('id', myNotificationList[i]['id']);
notices[i].click(function (e) {
if ($(e.target).is('.ui-pnotify-closer *, .ui-pnotify-sticker *'))
return;
notices[i].pnotify({
type: 'success',
text: 'Ahhh, Me hiciste click!'
});
});
}
}
};

Could someone tell me what is wrong in the code.

Thank you.

from pnotify.

TravelingTechGuy avatar TravelingTechGuy commented on May 16, 2024

First, it'd help if you can indent and clean the code. Second, where exactly are you defining the array notices?

from pnotify.

reybb avatar reybb commented on May 16, 2024

Sorry, this is as it should be:

code1

from pnotify.

chfw avatar chfw commented on May 16, 2024

Please note that since version 2.1, pnotify_remove() of a PNotify instance is renamed to remove()

from pnotify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.