nifty-dialog 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
data/lib/nifty/dialog.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
window.Nifty ||= {}
|
2
|
+
window.Nifty.Dialog =
|
3
|
+
|
4
|
+
# The numerical ID to start showing dialogs from
|
5
|
+
startingID: 1
|
6
|
+
|
7
|
+
# A callback reference which is run on content set if set.
|
8
|
+
onSetContent: null
|
9
|
+
|
10
|
+
# Stores all behaviors
|
11
|
+
behaviors: {}
|
12
|
+
|
13
|
+
# Open a new dialog which will accept a number of possible options.
|
14
|
+
#
|
15
|
+
# id => the ID to assign to this dialog (prefixed with 'niftyDialog-' )
|
16
|
+
#
|
17
|
+
# url => open a dialog containing the HTML at the given URL. When
|
18
|
+
# displaying using a URL, the dialog will open immediately
|
19
|
+
# and containing a spinner until the data is loaded.
|
20
|
+
#
|
21
|
+
# html => a string of HTML which contains the HTML which should be
|
22
|
+
# displayed in the dialog.
|
23
|
+
#
|
24
|
+
# afterLoad => a callback to execute after the dialog has been loaded.
|
25
|
+
#
|
26
|
+
# width => the width of the dialog (in px, defaults to CSS-provided)
|
27
|
+
#
|
28
|
+
# offset => specifies a vertical offset (in px)
|
29
|
+
#
|
30
|
+
# class => the CSS class to assign to this dialog
|
31
|
+
#
|
32
|
+
# behavior => the name of a behavior set to be invoked on dialog open/close.
|
33
|
+
# Behaviors can be setup using the Nifty.Dialog.addBehavior method.
|
34
|
+
#
|
35
|
+
open: (options={})->
|
36
|
+
# set a dialog ID for this dialog
|
37
|
+
dialogsOpen = $('div.niftyDialog').length
|
38
|
+
dialogID = if dialogsOpen == 0 then this.startingID else (dialogsOpen * 10) + this.startingID
|
39
|
+
|
40
|
+
# create a template and assign the ID
|
41
|
+
dialogTemplate = $("<div class='niftyDialog #{options.class}' id='niftyDialog-#{options.id}'></div>")
|
42
|
+
dialogTemplate.data('dialogID', dialogID)
|
43
|
+
|
44
|
+
# insert the dialog into the page
|
45
|
+
insertedDialog = dialogTemplate.appendTo($('body'))
|
46
|
+
insertedDialog.css('z-index', 2000 + dialogID)
|
47
|
+
|
48
|
+
# set the content on the dialog
|
49
|
+
insertedDialog.data('options', options)
|
50
|
+
|
51
|
+
overlayClass = ''
|
52
|
+
overlayClass = 'invisible' if dialogID > 1
|
53
|
+
theOverlay = $("<div class='niftyOverlay #{overlayClass}'></div>").insertBefore(insertedDialog).css('z-index', 2000 + dialogID - 1)
|
54
|
+
theOverlay.fadeIn('fast')
|
55
|
+
|
56
|
+
# if we have a width, set the width for the dialog
|
57
|
+
if options.width?
|
58
|
+
insertedDialog.css('width', "#{options.width}px")
|
59
|
+
insertedDialog.css('margin-left', "-#{options.width / 2}px")
|
60
|
+
|
61
|
+
if options.offset?
|
62
|
+
insertedDialog.css('margin-top', "#{options.offset}px")
|
63
|
+
|
64
|
+
# Set the closing action for the inserted dialog to close dialog
|
65
|
+
# and fade out the appropriate overlay
|
66
|
+
insertedDialog.data 'closeAction', =>
|
67
|
+
options.onClose.call(null, insertedDialog, options) if options.onClose?
|
68
|
+
if options.behavior? && behavior = this.behaviors[options.behavior]
|
69
|
+
behavior.onClose.call(null, insertedDialog, options) if behavior.onClose?
|
70
|
+
insertedDialog.fadeOut 'fast', -> insertedDialog.remove()
|
71
|
+
theOverlay.fadeOut 'fast', -> theOverlay.remove()
|
72
|
+
|
73
|
+
# Set that clicking on the dialog's overlay will close the dialog
|
74
|
+
theOverlay.on 'click', -> insertedDialog.data('closeAction').call()
|
75
|
+
|
76
|
+
# load in the content
|
77
|
+
if options.url?
|
78
|
+
# if loading from a URL, do this
|
79
|
+
insertedDialog.addClass 'ajax'
|
80
|
+
insertedDialog.addClass 'loading'
|
81
|
+
$.ajax
|
82
|
+
url: options.url
|
83
|
+
success: (data)=> this.displayDialog(insertedDialog, data)
|
84
|
+
|
85
|
+
else if options.html?
|
86
|
+
this.displayDialog(insertedDialog, options.html)
|
87
|
+
|
88
|
+
else
|
89
|
+
# anything else won't work
|
90
|
+
console.log "Dialog could not be displayed. Invalid options passed."
|
91
|
+
console.log options
|
92
|
+
return false
|
93
|
+
|
94
|
+
# Add a behaviour callback which will be executed
|
95
|
+
addBehavior: (options)->
|
96
|
+
if options.name?
|
97
|
+
this.behaviors[options.name] = options
|
98
|
+
true
|
99
|
+
else
|
100
|
+
console.log "Must pass a 'name' option to the addBehavior method."
|
101
|
+
false
|
102
|
+
|
103
|
+
# Complete the opening of a dialog with the given HTML
|
104
|
+
displayDialog: (dialog, content)->
|
105
|
+
dialog.html(content)
|
106
|
+
dialog.fadeIn('fast')
|
107
|
+
dialog.removeClass 'loading'
|
108
|
+
options = dialog.data('options')
|
109
|
+
if options.behavior? && behavior = this.behaviors[options.behavior]
|
110
|
+
behavior.onLoad.call(null, dialog, options) if behavior.onLoad?
|
111
|
+
options.afterLoad.call(null, dialog) if options.afterLoad?
|
112
|
+
this.onSetContent(null, dialog) if this.onSetContent?
|
113
|
+
|
114
|
+
# This method will replace the contents of the nearest dialog (or the one with the
|
115
|
+
# given ID if one is given).
|
116
|
+
setContent: (content, id = null)->
|
117
|
+
dialog = if id == null then $('div.niftyDialog:last') else $("div.niftyDialog#dialog-#{id}")
|
118
|
+
if dialog.length
|
119
|
+
dialog.html(content)
|
120
|
+
options = dialog.data('options')
|
121
|
+
if options.behavior? && behavior = this.behaviors[options.behavior]
|
122
|
+
behavior.onSetContent.call(null, dialog, options) if behavior.onSetContent?
|
123
|
+
this.onSetContent(null, dialog) if this.onSetContent?
|
124
|
+
|
125
|
+
# Create a new overlay
|
126
|
+
createOverlay: (options)->
|
127
|
+
overlay = $("<div class='niftyOverlay invisible'></div>")
|
128
|
+
overlay.insertBefore(options.behind)
|
129
|
+
overlay.css("z-index", options.behind.css('z-index') - 1)
|
130
|
+
overlay.on 'click', ->
|
131
|
+
options.close.call(overlay)
|
132
|
+
overlay.fadeOut 'fast', ->
|
133
|
+
overlay.remove()
|
134
|
+
overlay.fadeIn('fast')
|
135
|
+
|
136
|
+
# Closes the top dialgo in the dialog stack
|
137
|
+
closeTopDialog: ->
|
138
|
+
if $('div.niftyDialog').length
|
139
|
+
$('div.niftyDialog:last').data('closeAction').call()
|
140
|
+
|
141
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
//
|
2
|
+
// Overlay
|
3
|
+
//
|
4
|
+
div.niftyOverlay {
|
5
|
+
position:fixed;
|
6
|
+
background:rgba(0,0,0,0.5);
|
7
|
+
top:0;
|
8
|
+
left:0;
|
9
|
+
width:100%;
|
10
|
+
height:4000px;
|
11
|
+
display:none;
|
12
|
+
z-index:50;
|
13
|
+
}
|
14
|
+
|
15
|
+
//
|
16
|
+
// An invisible verison of the overlay
|
17
|
+
//
|
18
|
+
div.niftyOverlay.invisible {
|
19
|
+
background:transparent;
|
20
|
+
}
|
21
|
+
|
22
|
+
//
|
23
|
+
// Dialog
|
24
|
+
//
|
25
|
+
div.niftyDialog {
|
26
|
+
display:none;
|
27
|
+
position:fixed;
|
28
|
+
top:50%;
|
29
|
+
top:100px;
|
30
|
+
left:50%;
|
31
|
+
margin-left:-250px;
|
32
|
+
width:500px;
|
33
|
+
background:#fff;
|
34
|
+
box-shadow:0 0 70px rgb(0,0,0);
|
35
|
+
h2 {
|
36
|
+
background:#333333;
|
37
|
+
color:#fff;
|
38
|
+
padding:10px 15px;
|
39
|
+
font-weight:600;
|
40
|
+
background-repeat:no-repeat;
|
41
|
+
background-position:95% 17px;
|
42
|
+
background-size:16px;
|
43
|
+
font-size:0.9em;
|
44
|
+
}
|
45
|
+
}
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nifty-dialog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A javascript library for working with dialogs
|
15
|
+
email:
|
16
|
+
- adam@niftyware.io
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/nifty/dialog/version.rb
|
22
|
+
- lib/nifty/dialog.rb
|
23
|
+
- vendor/assets/javascripts/nifty/dialog.coffee
|
24
|
+
- vendor/assets/stylesheets/nifty/dialog.scss
|
25
|
+
homepage: https://github.com/niftyware/dialog
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.23
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: A javascript library for working with dialogs
|
49
|
+
test_files: []
|