Last active
January 4, 2016 15:05
-
-
Save po8rewq/dd6af2416b75747dcd91 to your computer and use it in GitHub Desktop.
Simple JQuery notification with Haxe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import js.JQuery; | |
import js.JQuery.JQueryHelper.*; | |
class JqNotification | |
{ | |
var _element : JQuery; | |
var _note : Dynamic; | |
var _options : NotificationOptions; | |
public function new(pId: String, pOptions: NotificationOptions) | |
{ | |
_element = J(pId); | |
_note = J("<div class=\"alert\"></div>"); | |
_options = { | |
type: pOptions.type == null ? 'success' : pOptions.type, | |
closable: pOptions.closable == null ? true : pOptions.closable, | |
autoClose: pOptions.autoClose == null ? true : pOptions.autoClose, | |
message: pOptions.message, | |
onClosed: pOptions.onClosed, | |
delay: pOptions.delay == null ? 3000 : pOptions.delay | |
} | |
_note.addClass('in').addClass( "fade" ); | |
_note.addClass('alert-' + _options.type); | |
if( _options.message.html ) | |
_note.html( _options.message.text ); | |
else | |
_note.text( _options.message.text ); | |
if( _options.closable ) | |
{ | |
var link = J("<a class=\"close pull-right\" href=\"#\">×</a>"); | |
link.click(function(e){ | |
e.preventDefault(); | |
onClose(); | |
}); | |
_note.prepend(link); | |
} | |
} | |
private function onClose() | |
{ | |
_note.remove(); | |
if( _options.onClosed != null ) | |
_options.onClosed(); | |
} | |
public function show() | |
{ | |
_element.append( _note ); | |
if( _options.autoClose ) | |
_note.slideDown('slow').delay( _options.delay ).slideToggle('slow', onClose); | |
} | |
public function hide(pNow: Bool = false) | |
{ | |
if(pNow) | |
onClose(); | |
else | |
_note.delay( _options.delay ).slideToggle('slow', onClose); | |
} | |
/** | |
* In case you need to use the object | |
*/ | |
public function getAlertElement():JQuery | |
{ | |
return _note; | |
} | |
} | |
typedef NotificationOptions = { | |
var message : {html: Bool, text: String}; // Text to show on alert, you can use either html or text. HTML will override text. | |
@:optional var delay : Int; // Delay to show / hide the notification @default 3000 | |
@:optional var type : String; // Alert style, omit alert- from style name. | |
@:optional var closable : Bool; // Allow alert to be closable through a close icon. | |
@:optional var autoClose : Bool; // Fade alert out after a certain delay (in ms) @default : true | |
@:optional var onClosed : Void->Void; // Called after alert closes. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment