git.fiddlerwoaroof.com
Browse code

Handle edge-cases better, don't auto-init

Ed Langley authored on 18/03/2016 16:58:19
Showing 1 changed files
... ...
@@ -2,61 +2,68 @@
2 2
 
3 3
   var currentModal = null;
4 4
   $.fn.myjQModal = function() {
5
-    'use strict';
6
-    var self=this;
7
-    var $self = $(self);
8
-
9
-    if ($self.length === 1 && $self[0].myjQModal !== undefined) { return $self[0].myjQModal; }
10
-
11
-    $(self).each(function() {
12
-      if (this.myjQModal !== undefined) { return this.myjQModal; }
13
-
14
-      var target = $(this.dataset.target);
15
-      var action = this.dataset.action;
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
-      });
5
+      'use strict';
6
+      var self=this;
7
+      var $self = $(self);
8
+
9
+      if ($self.length === 1 && $self[0].myjQModal !== undefined) { return $self[0].myjQModal; }
10
+
11
+      $(self).each(function() {
12
+          if (this.myjQModal === undefined) {
13
+              var target = $(this.dataset.target);
14
+              var targetModal = target.data('myjQModal');
15
+              var action = this.dataset.action;
16
+              var wrapper = target.closest('.myjQModal-wrapper');
17
+
18
+              if (targetModal === undefined) {
19
+                  var modal = $('<div></div>');
20
+                  wrapper = $('<div class="myjQModal-wrapper"></div>').append(modal);
21
+
22
+                  modal.append(target);
23
+                  modal.wrap('<div class="myjQModal">');
24
+                  $('body').append(wrapper);
25
+
26
+                  wrapper.click(function() {
27
+                      wrapper.removeClass('open');
28
+                      currentModal = null;
29
+                  });
30
+
31
+                  if (! target.hasClass('close-on-click')) {
32
+                      modal.click(function(ev) {
33
+                          ev.stopPropagation();
34
+                      });
35
+                  }
36
+
37
+                  target.removeClass('preinit');
38
+              }
39
+
40
+              this.myjQModal = {
41
+                  target: target,
42
+                  action: action,
43
+                  wrapper: wrapper,
44
+
45
+                  open: function() {
46
+                      this.wrapper.addClass('open');
47
+                      if (currentModal !== null) { currentModal.removeClass('open'); }
48
+                      currentModal = wrapper;
49
+                  },
50
+                  close: function() {
51
+                      currentModal = null;
52
+                      this.wrapper.removeClass('open');
53
+                  }
54
+              };
55
+
56
+              if (targetModal === undefined) {
57
+                  target.data('myjQModal', this.myjQModal);
58
+              }
26 59
 
27
-      if (! target.hasClass('close-on-click')) {
28
-        modal.click(function(ev) {
29
-          ev.stopPropagation();
30
-        });
31
-      }
32
-
33
-      target.myjQModal = this.myjQModal = {
34
-        target: target,
35
-        action: action,
36
-        modal: modal,
37
-        wrapper: wrapper,
38
-
39
-        open: function() {
40
-          this.wrapper.addClass('open');
41
-          if (currentModal !== null) { currentModal.removeClass('open'); }
42
-          currentModal = wrapper;
43
-        },
44
-        close: function() {
45
-          currentModal = null;
46
-          this.wrapper.removeClass('open');
47
-        }
48
-      };
49
-
50
-      $(this).click(function() {
51
-          console.log('this should happen');
52
-        if (action === 'open') { this.myjQModal.open(); }
53
-        else if (action === 'close') { this.myjQModal.close(); }
60
+              $(this).click(function() {
61
+                  console.log('this should happen');
62
+                  if (action === 'open') { this.myjQModal.open(); }
63
+                  else if (action === 'close') { this.myjQModal.close(); }
64
+              });
65
+          };
54 66
       });
55
-    });
56 67
       return this;
57
-    };
58
-  $(document).ready(function() {
59
-      console.log('boo');
60
-    $('button[data-type=modalTrigger]').myjQModal();
61
-  });
68
+  };
62 69
 }( jQuery ));