pnotify-rails 1.2.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,143 @@
1
+ // Desktop
2
+ // Uses AMD or browser globals for jQuery.
3
+ (function (factory) {
4
+ if (typeof define === 'function' && define.amd) {
5
+ // AMD. Register as a module.
6
+ define('pnotify.desktop', ['jquery', 'pnotify'], factory);
7
+ } else {
8
+ // Browser globals
9
+ factory(jQuery, PNotify);
10
+ }
11
+ }(function($, PNotify){
12
+ var permission;
13
+ var notify = function(title, options){
14
+ // Memoize based on feature detection.
15
+ if ("Notification" in window) {
16
+ notify = function (title, options) {
17
+ return new Notification(title, options);
18
+ };
19
+ } else if ("mozNotification" in navigator) {
20
+ notify = function (title, options) {
21
+ // Gecko < 22
22
+ return navigator.mozNotification
23
+ .createNotification(title, options.body, options.icon)
24
+ .show();
25
+ };
26
+ } else if ("webkitNotifications" in window) {
27
+ notify = function (title, options) {
28
+ return window.webkitNotifications.createNotification(
29
+ options.icon,
30
+ title,
31
+ options.body
32
+ );
33
+ };
34
+ } else {
35
+ notify = function (title, options) {
36
+ return null;
37
+ };
38
+ }
39
+ return notify(title, options);
40
+ };
41
+
42
+
43
+ PNotify.prototype.options.desktop = {
44
+ // Display the notification as a desktop notification.
45
+ desktop: false,
46
+ // The URL of the icon to display. If false, no icon will show. If null, a default icon will show.
47
+ icon: null,
48
+ // Using a tag lets you update an existing notice, or keep from duplicating notices between tabs.
49
+ // If you leave tag null, one will be generated, facilitating the "update" function.
50
+ // see: http://www.w3.org/TR/notifications/#tags-example
51
+ tag: null
52
+ };
53
+ PNotify.prototype.modules.desktop = {
54
+ tag: null,
55
+ icon: null,
56
+ genNotice: function(notice, options){
57
+ if (options.icon === null) {
58
+ this.icon = "http://sciactive.com/pnotify/includes/desktop/"+notice.options.type+".png";
59
+ } else if (options.icon === false) {
60
+ this.icon = null;
61
+ } else {
62
+ this.icon = options.icon;
63
+ }
64
+ if (this.tag === null || options.tag !== null) {
65
+ this.tag = options.tag === null ? "PNotify-"+Math.round(Math.random() * 1000000) : options.tag;
66
+ }
67
+ notice.desktop = notify(notice.options.title, {
68
+ icon: this.icon,
69
+ body: notice.options.text,
70
+ tag: this.tag
71
+ });
72
+ if (!("close" in notice.desktop)) {
73
+ notice.desktop.close = function(){
74
+ notice.desktop.cancel();
75
+ };
76
+ }
77
+ notice.desktop.onclick = function(){
78
+ notice.elem.trigger("click");
79
+ };
80
+ notice.desktop.onclose = function(){
81
+ if (notice.state !== "closing" && notice.state !== "closed") {
82
+ notice.remove();
83
+ }
84
+ };
85
+ },
86
+ init: function(notice, options){
87
+ if (!options.desktop)
88
+ return;
89
+ permission = PNotify.desktop.checkPermission();
90
+ if (permission != 0)
91
+ return;
92
+ this.genNotice(notice, options);
93
+ },
94
+ update: function(notice, options, oldOpts){
95
+ if (permission != 0 || !options.desktop)
96
+ return;
97
+ this.genNotice(notice, options);
98
+ },
99
+ beforeOpen: function(notice, options){
100
+ if (permission != 0 || !options.desktop)
101
+ return;
102
+ notice.elem.css({'left': '-10000px', 'display': 'none'});
103
+ },
104
+ afterOpen: function(notice, options){
105
+ if (permission != 0 || !options.desktop)
106
+ return;
107
+ notice.elem.css({'left': '-10000px', 'display': 'none'});
108
+ if ("show" in notice.desktop) {
109
+ notice.desktop.show();
110
+ }
111
+ },
112
+ beforeClose: function(notice, options){
113
+ if (permission != 0 || !options.desktop)
114
+ return;
115
+ notice.elem.css({'left': '-10000px', 'display': 'none'});
116
+ },
117
+ afterClose: function(notice, options){
118
+ if (permission != 0 || !options.desktop)
119
+ return;
120
+ notice.elem.css({'left': '-10000px', 'display': 'none'});
121
+ notice.desktop.close();
122
+ }
123
+ };
124
+ PNotify.desktop = {
125
+ permission: function(){
126
+ if (typeof Notification !== "undefined" && "requestPermission" in Notification) {
127
+ Notification.requestPermission();
128
+ } else if ("webkitNotifications" in window) {
129
+ window.webkitNotifications.requestPermission();
130
+ }
131
+ },
132
+ checkPermission: function(){
133
+ if (typeof Notification !== "undefined" && "permission" in Notification) {
134
+ return (Notification.permission == "granted" ? 0 : 1);
135
+ } else if ("webkitNotifications" in window) {
136
+ return window.webkitNotifications.checkPermission();
137
+ } else {
138
+ return 1;
139
+ }
140
+ }
141
+ };
142
+ permission = PNotify.desktop.checkPermission();
143
+ }));
@@ -0,0 +1,189 @@
1
+ // History
2
+ // Uses AMD or browser globals for jQuery.
3
+ (function (factory) {
4
+ if (typeof define === 'function' && define.amd) {
5
+ // AMD. Register as a module.
6
+ define('pnotify.history', ['jquery', 'pnotify'], factory);
7
+ } else {
8
+ // Browser globals
9
+ factory(jQuery, PNotify);
10
+ }
11
+ }(function($, PNotify){
12
+ var history_menu,
13
+ history_handle_top;
14
+ $(function(){
15
+ $("body").on("pnotify.history-all", function(){
16
+ // Display all notices. (Disregarding non-history notices.)
17
+ $.each(PNotify.notices, function(){
18
+ if (this.modules.history.inHistory) {
19
+ if (this.elem.is(":visible")) {
20
+ // The hide variable controls whether the history pull down should
21
+ // queue a removal timer.
22
+ if (this.options.hide)
23
+ this.queueRemove();
24
+ } else if (this.open)
25
+ this.open();
26
+ }
27
+ });
28
+ }).on("pnotify.history-last", function(){
29
+ var pushTop = (PNotify.prototype.options.stack.push === "top");
30
+
31
+ // Look up the last history notice, and display it.
32
+ var i = (pushTop ? 0 : -1);
33
+
34
+ var notice;
35
+ do {
36
+ if (i === -1)
37
+ notice = PNotify.notices.slice(i);
38
+ else
39
+ notice = PNotify.notices.slice(i, i+1);
40
+ if (!notice[0])
41
+ return false;
42
+
43
+ i = (pushTop ? i + 1 : i - 1);
44
+ } while (!notice[0].modules.history.inHistory || notice[0].elem.is(":visible"));
45
+ if (notice[0].open)
46
+ notice[0].open();
47
+ });
48
+ });
49
+ PNotify.prototype.options.history = {
50
+ // Place the notice in the history.
51
+ history: true,
52
+ // Display a pull down menu to redisplay previous notices.
53
+ menu: false,
54
+ // Make the pull down menu fixed to the top of the viewport.
55
+ fixed: true,
56
+ // Maximum number of notifications to have onscreen.
57
+ maxonscreen: Infinity,
58
+ // The various displayed text, helps facilitating internationalization.
59
+ labels: {
60
+ redisplay: "Redisplay",
61
+ all: "All",
62
+ last: "Last"
63
+ }
64
+ };
65
+ PNotify.prototype.modules.history = {
66
+ // The history variable controls whether the notice gets redisplayed
67
+ // by the history pull down.
68
+ inHistory: false,
69
+
70
+ init: function(notice, options){
71
+ // Make sure that no notices get destroyed.
72
+ notice.options.destroy = false;
73
+
74
+ this.inHistory = options.history;
75
+
76
+ if (options.menu) {
77
+ // If there isn't a history pull down, create one.
78
+ if (typeof history_menu === "undefined") {
79
+ history_menu = $("<div />", {
80
+ "class": "ui-pnotify-history-container "+notice.styles.hi_menu,
81
+ "mouseleave": function(){
82
+ history_menu.animate({top: "-"+history_handle_top+"px"}, {duration: 100, queue: false});
83
+ }
84
+ })
85
+ .append($("<div />", {"class": "ui-pnotify-history-header", "text": options.labels.redisplay}))
86
+ .append($("<button />", {
87
+ "class": "ui-pnotify-history-all "+notice.styles.hi_btn,
88
+ "text": options.labels.all,
89
+ "mouseenter": function(){
90
+ $(this).addClass(notice.styles.hi_btnhov);
91
+ },
92
+ "mouseleave": function(){
93
+ $(this).removeClass(notice.styles.hi_btnhov);
94
+ },
95
+ "click": function(){
96
+ $(this).trigger("pnotify.history-all");
97
+ return false;
98
+ }
99
+ }))
100
+ .append($("<button />", {
101
+ "class": "ui-pnotify-history-last "+notice.styles.hi_btn,
102
+ "text": options.labels.last,
103
+ "mouseenter": function(){
104
+ $(this).addClass(notice.styles.hi_btnhov);
105
+ },
106
+ "mouseleave": function(){
107
+ $(this).removeClass(notice.styles.hi_btnhov);
108
+ },
109
+ "click": function(){
110
+ $(this).trigger("pnotify.history-last");
111
+ return false;
112
+ }
113
+ }))
114
+ .appendTo("body");
115
+
116
+ // Make a handle so the user can pull down the history tab.
117
+ var handle = $("<span />", {
118
+ "class": "ui-pnotify-history-pulldown "+notice.styles.hi_hnd,
119
+ "mouseenter": function(){
120
+ history_menu.animate({top: "0"}, {duration: 100, queue: false});
121
+ }
122
+ })
123
+ .appendTo(history_menu);
124
+
125
+ // Get the top of the handle.
126
+ console.log(handle.offset());
127
+ history_handle_top = handle.offset().top + 2;
128
+ // Hide the history pull down up to the top of the handle.
129
+ history_menu.css({top: "-"+history_handle_top+"px"});
130
+
131
+ // Apply the fixed styling.
132
+ if (options.fixed) {
133
+ history_menu.addClass('ui-pnotify-history-fixed');
134
+ }
135
+ }
136
+ }
137
+ },
138
+ update: function(notice, options){
139
+ // Update values for history menu access.
140
+ this.inHistory = options.history;
141
+ if (options.fixed && history_menu) {
142
+ history_menu.addClass('ui-pnotify-history-fixed');
143
+ } else if (history_menu) {
144
+ history_menu.removeClass('ui-pnotify-history-fixed');
145
+ }
146
+ },
147
+ beforeOpen: function(notice, options){
148
+ // Remove oldest notifications leaving only options.maxonscreen on screen
149
+ if (PNotify.notices && (PNotify.notices.length > options.maxonscreen)) {
150
+ // Oldest are normally in front of array, or if stack.push=="top" then
151
+ // they are at the end of the array! (issue #98)
152
+ var el;
153
+ if (notice.options.stack.push !== "top")
154
+ el = PNotify.notices.slice(0, PNotify.notices.length - options.maxonscreen);
155
+ else
156
+ el = PNotify.notices.slice(options.maxonscreen, PNotify.notices.length);
157
+
158
+ $.each(el, function(){
159
+ if (this.remove)
160
+ this.remove();
161
+ });
162
+ }
163
+ }
164
+ };
165
+ $.extend(PNotify.styling.jqueryui, {
166
+ hi_menu: "ui-state-default ui-corner-bottom",
167
+ hi_btn: "ui-state-default ui-corner-all",
168
+ hi_btnhov: "ui-state-hover",
169
+ hi_hnd: "ui-icon ui-icon-grip-dotted-horizontal"
170
+ });
171
+ $.extend(PNotify.styling.bootstrap2, {
172
+ hi_menu: "well",
173
+ hi_btn: "btn",
174
+ hi_btnhov: "",
175
+ hi_hnd: "icon-chevron-down"
176
+ });
177
+ $.extend(PNotify.styling.bootstrap3, {
178
+ hi_menu: "well",
179
+ hi_btn: "btn btn-default",
180
+ hi_btnhov: "",
181
+ hi_hnd: "glyphicon glyphicon-chevron-down"
182
+ });
183
+ $.extend(PNotify.styling.fontawesome, {
184
+ hi_menu: "well",
185
+ hi_btn: "btn btn-default",
186
+ hi_btnhov: "",
187
+ hi_hnd: "fa fa-chevron-down"
188
+ });
189
+ }));
@@ -0,0 +1,151 @@
1
+ // Nonblock
2
+ // Uses AMD or browser globals for jQuery.
3
+ (function (factory) {
4
+ if (typeof define === 'function' && define.amd) {
5
+ // AMD. Register as a module.
6
+ define('pnotify.nonblock', ['jquery', 'pnotify'], factory);
7
+ } else {
8
+ // Browser globals
9
+ factory(jQuery, PNotify);
10
+ }
11
+ }(function($, PNotify){
12
+ // Some useful regexes.
13
+ var re_on = /^on/,
14
+ re_mouse_events = /^(dbl)?click$|^mouse(move|down|up|over|out|enter|leave)$|^contextmenu$/,
15
+ re_ui_events = /^(focus|blur|select|change|reset)$|^key(press|down|up)$/,
16
+ re_html_events = /^(scroll|resize|(un)?load|abort|error)$/;
17
+ // Fire a DOM event.
18
+ var dom_event = function(e, orig_e){
19
+ var event_object;
20
+ e = e.toLowerCase();
21
+ if (document.createEvent && this.dispatchEvent) {
22
+ // FireFox, Opera, Safari, Chrome
23
+ e = e.replace(re_on, '');
24
+ if (e.match(re_mouse_events)) {
25
+ // This allows the click event to fire on the notice. There is
26
+ // probably a much better way to do it.
27
+ $(this).offset();
28
+ event_object = document.createEvent("MouseEvents");
29
+ event_object.initMouseEvent(
30
+ e, orig_e.bubbles, orig_e.cancelable, orig_e.view, orig_e.detail,
31
+ orig_e.screenX, orig_e.screenY, orig_e.clientX, orig_e.clientY,
32
+ orig_e.ctrlKey, orig_e.altKey, orig_e.shiftKey, orig_e.metaKey, orig_e.button, orig_e.relatedTarget
33
+ );
34
+ } else if (e.match(re_ui_events)) {
35
+ event_object = document.createEvent("UIEvents");
36
+ event_object.initUIEvent(e, orig_e.bubbles, orig_e.cancelable, orig_e.view, orig_e.detail);
37
+ } else if (e.match(re_html_events)) {
38
+ event_object = document.createEvent("HTMLEvents");
39
+ event_object.initEvent(e, orig_e.bubbles, orig_e.cancelable);
40
+ }
41
+ if (!event_object) return;
42
+ this.dispatchEvent(event_object);
43
+ } else {
44
+ // Internet Explorer
45
+ if (!e.match(re_on)) e = "on"+e;
46
+ event_object = document.createEventObject(orig_e);
47
+ this.fireEvent(e, event_object);
48
+ }
49
+ };
50
+
51
+
52
+ // This keeps track of the last element the mouse was over, so
53
+ // mouseleave, mouseenter, etc can be called.
54
+ var nonblock_last_elem;
55
+ // This is used to pass events through the notice if it is non-blocking.
56
+ var nonblock_pass = function(notice, e, e_name){
57
+ notice.elem.css("display", "none");
58
+ var element_below = document.elementFromPoint(e.clientX, e.clientY);
59
+ notice.elem.css("display", "block");
60
+ var jelement_below = $(element_below);
61
+ var cursor_style = jelement_below.css("cursor");
62
+ notice.elem.css("cursor", cursor_style !== "auto" ? cursor_style : "default");
63
+ // If the element changed, call mouseenter, mouseleave, etc.
64
+ if (!nonblock_last_elem || nonblock_last_elem.get(0) != element_below) {
65
+ if (nonblock_last_elem) {
66
+ dom_event.call(nonblock_last_elem.get(0), "mouseleave", e.originalEvent);
67
+ dom_event.call(nonblock_last_elem.get(0), "mouseout", e.originalEvent);
68
+ }
69
+ dom_event.call(element_below, "mouseenter", e.originalEvent);
70
+ dom_event.call(element_below, "mouseover", e.originalEvent);
71
+ }
72
+ dom_event.call(element_below, e_name, e.originalEvent);
73
+ // Remember the latest element the mouse was over.
74
+ nonblock_last_elem = jelement_below;
75
+ };
76
+
77
+
78
+ PNotify.prototype.options.nonblock = {
79
+ // Create a non-blocking notice. It lets the user click elements underneath it.
80
+ nonblock: false,
81
+ // The opacity of the notice (if it's non-blocking) when the mouse is over it.
82
+ nonblock_opacity: .2
83
+ };
84
+ PNotify.prototype.modules.nonblock = {
85
+ // This lets us update the options available in the closures.
86
+ myOptions: null,
87
+
88
+ init: function(notice, options){
89
+ var that = this;
90
+ this.myOptions = options;
91
+ notice.elem.on({
92
+ "mouseenter": function(e){
93
+ if (that.myOptions.nonblock) e.stopPropagation();
94
+ if (that.myOptions.nonblock) {
95
+ // If it's non-blocking, animate to the other opacity.
96
+ notice.elem.stop().animate({"opacity": that.myOptions.nonblock_opacity}, "fast");
97
+ }
98
+ },
99
+ "mouseleave": function(e){
100
+ if (that.myOptions.nonblock) e.stopPropagation();
101
+ nonblock_last_elem = null;
102
+ notice.elem.css("cursor", "auto");
103
+ // Animate back to the normal opacity.
104
+ if (that.myOptions.nonblock && notice.animating !== "out")
105
+ notice.elem.stop().animate({"opacity": notice.options.opacity}, "fast");
106
+ },
107
+ "mouseover": function(e){
108
+ if (that.myOptions.nonblock) e.stopPropagation();
109
+ },
110
+ "mouseout": function(e){
111
+ if (that.myOptions.nonblock) e.stopPropagation();
112
+ },
113
+ "mousemove": function(e){
114
+ if (that.myOptions.nonblock) {
115
+ e.stopPropagation();
116
+ nonblock_pass(notice, e, "onmousemove");
117
+ }
118
+ },
119
+ "mousedown": function(e){
120
+ if (that.myOptions.nonblock) {
121
+ e.stopPropagation();
122
+ e.preventDefault();
123
+ nonblock_pass(notice, e, "onmousedown");
124
+ }
125
+ },
126
+ "mouseup": function(e){
127
+ if (that.myOptions.nonblock) {
128
+ e.stopPropagation();
129
+ e.preventDefault();
130
+ nonblock_pass(notice, e, "onmouseup");
131
+ }
132
+ },
133
+ "click": function(e){
134
+ if (that.myOptions.nonblock) {
135
+ e.stopPropagation();
136
+ nonblock_pass(notice, e, "onclick");
137
+ }
138
+ },
139
+ "dblclick": function(e){
140
+ if (that.myOptions.nonblock) {
141
+ e.stopPropagation();
142
+ nonblock_pass(notice, e, "ondblclick");
143
+ }
144
+ }
145
+ });
146
+ },
147
+ update: function(notice, options){
148
+ this.myOptions = options;
149
+ }
150
+ };
151
+ }));