      1 (function($, rf) {
      2
      3     rf.ui = rf.ui || {};
      4
      5     var defaultOptions = {
      6         styleClass: '',
      7         nonblocking: false,
      8         nonblockingOpacity: 0.2,
      9         showHistory: false,
     10         animationSpeed: 'slow',
     11         opacity: '1',
     12         showShadow: false,
     13         showCloseButton: true,
     14         appearAnimation: 'fade',
     15         hideAnimation: 'fade',
     16         sticky: false,
     17         stayTime: 8000,
     18         delay: 0
     19     };
     20
     21     var defaultStackId = "org.richfaces.notifyStack.default";
     22
     23     var events = "click dblclick  keydown keypress keyup mousedown mousemove mouseout mouseover mouseup";
     24
     25     var propertyTranslation = {
     26         'summary':'pnotify_title',
     27         'detail': 'pnotify_text',
     28         'styleClass': 'pnotify_addclass',
     29         'nonblocking': 'pnotify_nonblock',
     30         'nonblockingOpacity': 'pnotify_nonblock_opacity',
     31         'showHistory': 'pnotify_history',
     32         'animation': 'pnotify_animation',
     33         'appearAnimation': 'effect_in',
     34         'hideAnimation': 'effect_out',
     35         'animationSpeed': 'pnotify_animate_speed',
     36         'opacity': 'pnotify_opacity',
     37         'showShadow': 'pnotify_shadow',
     38         'showCloseButton': 'pnotify_closer',
     39         'sticky': 'pnotify_hide',
     40         'stayTime': 'pnotify_delay'
     41     };
     42
     43     var severityClasses = ["rf-ntf-inf", "rf-ntf-wrn", "rf-ntf-err", "rf-ntf-ftl"];
     44
     45     var translateProperties = function(target, source, translation) {
     46         for (var attr in source) {
     47             var targetAttr = translation[attr] != null ? translation[attr] : attr;
     48             target[targetAttr] = source[attr];
     49             if (target[targetAttr] instanceof Object) {
     50                 target[targetAttr] = $.extend({}, target[targetAttr], translation);
     51             }
     52         }
     53         return target;
     54     };
     55
     56     var getDefaultStack = function() {
     57         if (!document.getElementById(defaultStackId)) {
     58             var stackElement = $('<span id="' + defaultStackId + '" class="rf-ntf-stck" />');
     59             $('body').append(stackElement);
     60             new rf.ui.NotifyStack(defaultStackId);
     61         }
     62         return getStack(defaultStackId);
     63     };
     64
     65     var getStack = function(stackId) {
     66         if (!stackId) {
     67             return getDefaultStack();
     68         }
     69         return rf.component(stackId).getStack();
     70     };
     71
     72
     73     var array_remove = function(array, from, to) {
     74         var rest = array.slice((to || from) + 1 || array.length);
     75         array.length = from < 0 ? array.length + from : from;
     76         return array.push.apply(array, rest);
     77     };
     78
     79     rf.ui.Notify = function(options) {
     80         var options = $.extend({}, defaultOptions, options);
     81
     82         if (typeof options.severity == "number") {
     83             var severity = severityClasses[options.severity];
     84             options.styleClass = options.styleClass ? severity + " " + options.styleClass : severity;
     85         }
     86
     87         var pnotifyOptions = translateProperties({}, options, propertyTranslation);
     88
     89         var display = function() {
     90             var stack = getStack(options.stackId);
     91             pnotifyOptions.pnotify_stack = stack;
     92             pnotifyOptions.pnotify_addclass += ' rf-ntf-pos-' + stack.position;
     93             pnotifyOptions.pnotify_after_close = function(pnotify) {
     94                 var index = $.inArray(pnotify, stack.notifications);
     95                 if (index >= 0) {
     96                     array_remove(stack.notifications, index);
     97                 }
     98             }
     99             var pnotify = $.pnotify(pnotifyOptions);
    100             pnotify.on(events, function(e) {
    101                 if (options['on' + e.type]) {
    102                     options['on' + e.type].call(this, e);
    103                 }
    104             });
    105             stack.addNotification(pnotify);
    106         }
    107
    108         if (options.sticky !== null) {
    109             pnotifyOptions.pnotify_hide = !options.sticky;
    110         }
    111
    112         $(document).ready(function() {
    113             if (options.delay) {
    114                 setTimeout(function() {
    115                     display();
    116                 }, options.delay);
    117             } else {
    118                 display();
    119             }
    120         });
    121     };
    122
    123 })(RichFaces.jQuery, RichFaces);