Browse code
adding external access to the modal
fiddlerwoaroof authored on 07/07/2015 02:36:49
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -3,42 +3,57 @@ |
3 | 3 |
$.fn.myjQModal = function() { |
4 | 4 |
'use strict'; |
5 | 5 |
var self=this; |
6 |
+ var $self = $(self); |
|
6 | 7 |
var currentModal = null; |
8 |
+ |
|
9 |
+ if ($self.length === 1 && $self[0].myjQModal !== undefined) { return $self[0].myjQModal; } |
|
10 |
+ |
|
7 | 11 |
$(self).each(function() { |
12 |
+ if (this.myjQModal !== undefined) { return this.myjQModal; } |
|
13 |
+ |
|
8 | 14 |
var target = $(this.dataset.target); |
9 | 15 |
var action = this.dataset.action; |
10 |
- if (action === 'open') { |
|
11 |
- var modal = $('<div></div>'); |
|
12 |
- var wrapper = $('<div class="myjQModal-wrapper"></div>').append(modal); |
|
13 |
- console.log(target); |
|
14 |
- modal.append(target); |
|
15 |
- modal.wrap('<div class="myjQModal">'); |
|
16 |
- $('body').append(wrapper); |
|
17 |
- wrapper.click(function() { |
|
18 |
- wrapper.removeClass('open'); |
|
19 |
- currentModal = null; |
|
20 |
- }); |
|
16 |
+ var modal = $('<div></div>'); |
|
17 |
+ var wrapper = $('<div class="myjQModal-wrapper"></div>').append(modal); |
|
18 |
+ console.log(target); |
|
19 |
+ modal.append(target); |
|
20 |
+ modal.wrap('<div class="myjQModal">'); |
|
21 |
+ $('body').append(wrapper); |
|
22 |
+ wrapper.click(function() { |
|
23 |
+ wrapper.removeClass('open'); |
|
24 |
+ currentModal = null; |
|
25 |
+ }); |
|
21 | 26 |
|
22 |
- if (! target.hasClass('close-on-click')) { |
|
23 |
- modal.click(function(ev) { |
|
24 |
- ev.stopPropagation(); |
|
25 |
- }); |
|
26 |
- } |
|
27 |
+ if (! target.hasClass('close-on-click')) { |
|
28 |
+ modal.click(function(ev) { |
|
29 |
+ ev.stopPropagation(); |
|
30 |
+ }); |
|
27 | 31 |
} |
28 |
- $(this).click(function() { |
|
29 |
- if (action === 'open') { |
|
32 |
+ |
|
33 |
+ this.myjQModal = { |
|
34 |
+ target: target, |
|
35 |
+ action: action, |
|
36 |
+ modal: modal, |
|
37 |
+ wrapper: wrapper, |
|
38 |
+ |
|
39 |
+ open: function() { |
|
30 | 40 |
if (currentModal !== null) { currentModal.removeClass('open'); } |
31 |
- wrapper.addClass('open'); |
|
41 |
+ this.wrapper.addClass('open'); |
|
32 | 42 |
currentModal = wrapper; |
33 |
- } |
|
34 |
- else if (action === 'close') { |
|
43 |
+ }, |
|
44 |
+ close: function() { |
|
35 | 45 |
currentModal = null; |
36 |
- wrapper.removeClass('open'); |
|
46 |
+ this.wrapper.removeClass('open'); |
|
37 | 47 |
} |
48 |
+ }; |
|
49 |
+ |
|
50 |
+ $(this).click(function() { |
|
51 |
+ if (action === 'open') { this.myjQModal.open(); } |
|
52 |
+ else if (action === 'close') { this.myjQModal.close(); } |
|
38 | 53 |
}); |
39 | 54 |
}); |
40 |
- return this; |
|
41 |
- }; |
|
55 |
+ return this; |
|
56 |
+ }; |
|
42 | 57 |
$(document).ready(function() { |
43 | 58 |
$('button[data-action=open]').myjQModal(); |
44 | 59 |
}); |