loldesign_publisher 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +21 -0
  5. data/app/assets/images/grid.png +0 -0
  6. data/app/assets/images/loldesign_publisher/logo.png +0 -0
  7. data/app/assets/images/loldesign_publisher/logo.svg +81 -0
  8. data/app/assets/images/loldesign_publisher/nav_active_link.svg +8 -0
  9. data/app/assets/javascripts/loldesign_publisher/activable.js +51 -0
  10. data/app/assets/javascripts/loldesign_publisher/libs/datepicker-pt-BR.js +37 -0
  11. data/app/assets/javascripts/loldesign_publisher/libs/gumby.init.js +47 -0
  12. data/app/assets/javascripts/loldesign_publisher/libs/gumby.js +258 -0
  13. data/app/assets/javascripts/loldesign_publisher/libs/gumby.min.js +1 -0
  14. data/app/assets/javascripts/loldesign_publisher/libs/jquery-1.10.1.min.js +6 -0
  15. data/app/assets/javascripts/loldesign_publisher/libs/jquery-1.10.1.min.map +1 -0
  16. data/app/assets/javascripts/loldesign_publisher/libs/jquery-2.0.2.min.js +6 -0
  17. data/app/assets/javascripts/loldesign_publisher/libs/jquery-2.0.2.min.map +1 -0
  18. data/app/assets/javascripts/loldesign_publisher/libs/jquery.mask.min.js +7 -0
  19. data/app/assets/javascripts/loldesign_publisher/libs/jquery.mobile.custom.min.js +3 -0
  20. data/app/assets/javascripts/loldesign_publisher/libs/modernizr-2.6.2.min.js +4 -0
  21. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.checkbox.js +101 -0
  22. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.fixed.js +240 -0
  23. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.navbar.js +115 -0
  24. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.radiobtn.js +90 -0
  25. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.retina.js +81 -0
  26. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.skiplink.js +157 -0
  27. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.tabs.js +80 -0
  28. data/app/assets/javascripts/loldesign_publisher/libs/ui/gumby.toggleswitch.js +264 -0
  29. data/app/assets/javascripts/loldesign_publisher/libs/ui/jquery.validation.js +142 -0
  30. data/app/assets/javascripts/loldesign_publisher/main.js +23 -0
  31. data/app/assets/javascripts/loldesign_publisher/plugins.js +4 -0
  32. data/app/assets/javascripts/loldesign_publisher/publisher.js +65 -0
  33. data/app/assets/stylesheets/loldesign_publisher/_base.css.scss +62 -0
  34. data/app/assets/stylesheets/loldesign_publisher/gumby.css +1683 -0
  35. data/app/assets/stylesheets/loldesign_publisher/layouts/_default.html.css.scss +15 -0
  36. data/app/assets/stylesheets/loldesign_publisher/layouts/_form.css.scss +101 -0
  37. data/app/assets/stylesheets/loldesign_publisher/layouts/_header_main.css.scss +20 -0
  38. data/app/assets/stylesheets/loldesign_publisher/layouts/_nav_main.css.scss +30 -0
  39. data/app/assets/stylesheets/loldesign_publisher/layouts/_resource_info.css.scss +40 -0
  40. data/app/assets/stylesheets/loldesign_publisher/modules/_add_resource.css.scss +12 -0
  41. data/app/assets/stylesheets/loldesign_publisher/modules/_btn_cancel.css.scss +13 -0
  42. data/app/assets/stylesheets/loldesign_publisher/modules/_btn_remove.css.scss +13 -0
  43. data/app/assets/stylesheets/loldesign_publisher/modules/_btn_save.css.scss +16 -0
  44. data/app/assets/stylesheets/loldesign_publisher/modules/_btn_show.css.scss +12 -0
  45. data/app/assets/stylesheets/loldesign_publisher/modules/_filter_box.css.scss +35 -0
  46. data/app/assets/stylesheets/loldesign_publisher/modules/_messages.css.scss +20 -0
  47. data/app/assets/stylesheets/loldesign_publisher/modules/_pagination.css.scss +65 -0
  48. data/app/assets/stylesheets/loldesign_publisher/modules/_table_page.css.scss +37 -0
  49. data/app/assets/stylesheets/loldesign_publisher/modules/_title_page.css.scss +54 -0
  50. data/app/assets/stylesheets/loldesign_publisher/publisher.css +7 -0
  51. data/app/controllers/loldesign_publisher/activables_controller.rb +26 -0
  52. data/app/controllers/loldesign_publisher/publisher_controller.rb +4 -0
  53. data/app/helpers/loldesign_publisher/publisher_helper.rb +101 -0
  54. data/app/views/layouts/loldesign_publisher/_header_main.html.erb +8 -0
  55. data/app/views/layouts/loldesign_publisher/_nav_main.html.erb +12 -0
  56. data/app/views/layouts/loldesign_publisher/modules/_add_resource.html.erb +3 -0
  57. data/app/views/layouts/loldesign_publisher/modules/_messages.html.erb +5 -0
  58. data/app/views/layouts/loldesign_publisher/modules/_td_not_found.html.erb +1 -0
  59. data/app/views/layouts/loldesign_publisher/modules/_title_page.html.erb +9 -0
  60. data/app/views/layouts/loldesign_publisher/publisher.html.erb +24 -0
  61. data/config/routes.rb +7 -0
  62. data/lib/generators/loldesign_publisher/install_generator.rb +26 -0
  63. data/lib/generators/loldesign_publisher/templates/loldesign_publisher.yml +12 -0
  64. data/lib/generators/loldesign_publisher/templates/override_publisher.css.scss +5 -0
  65. data/lib/generators/loldesign_publisher/templates/publisher_rails_app.js +3 -0
  66. data/lib/generators/loldesign_publisher/views_generator.rb +16 -0
  67. data/lib/loldesign_publisher.rb +28 -0
  68. data/lib/loldesign_publisher/engine.rb +28 -0
  69. data/lib/loldesign_publisher/version.rb +3 -0
  70. data/lib/tasks/loldesign_publisher_tasks.rake +4 -0
  71. data/public/fonts/icons/entypo.eot +0 -0
  72. data/public/fonts/icons/entypo.ttf +0 -0
  73. data/public/fonts/icons/entypo.woff +0 -0
  74. data/public/grid.js +68 -0
  75. data/spec/controllers/loldesign_publisher/activables_controller_spec.rb +21 -0
  76. data/spec/dummy/README.rdoc +28 -0
  77. data/spec/dummy/Rakefile +6 -0
  78. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  79. data/spec/dummy/app/assets/javascripts/loldesign_publisher/publisher_rails_app.js +3 -0
  80. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  81. data/spec/dummy/app/assets/stylesheets/loldesign_publisher/override_publisher.css.scss +5 -0
  82. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  83. data/spec/dummy/app/controllers/users_controller.rb +10 -0
  84. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  85. data/spec/dummy/app/models/user.rb +2 -0
  86. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  87. data/spec/dummy/app/views/users/_filters_by_from_to.html.erb +8 -0
  88. data/spec/dummy/app/views/users/_user.html.erb +9 -0
  89. data/spec/dummy/app/views/users/index.html.erb +26 -0
  90. data/spec/dummy/app/views/users/new.html.erb +23 -0
  91. data/spec/dummy/bin/bundle +3 -0
  92. data/spec/dummy/bin/rails +4 -0
  93. data/spec/dummy/bin/rake +4 -0
  94. data/spec/dummy/config.ru +4 -0
  95. data/spec/dummy/config/application.rb +29 -0
  96. data/spec/dummy/config/boot.rb +5 -0
  97. data/spec/dummy/config/database.yml +25 -0
  98. data/spec/dummy/config/environment.rb +5 -0
  99. data/spec/dummy/config/environments/development.rb +37 -0
  100. data/spec/dummy/config/environments/production.rb +78 -0
  101. data/spec/dummy/config/environments/test.rb +39 -0
  102. data/spec/dummy/config/initializers/assets.rb +8 -0
  103. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  104. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  105. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  106. data/spec/dummy/config/initializers/inflections.rb +16 -0
  107. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  108. data/spec/dummy/config/initializers/session_store.rb +3 -0
  109. data/spec/dummy/config/initializers/simple_form.rb +145 -0
  110. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  111. data/spec/dummy/config/locales/en.yml +23 -0
  112. data/spec/dummy/config/locales/simple_form.en.yml +26 -0
  113. data/spec/dummy/config/loldesign_publisher.yml +12 -0
  114. data/spec/dummy/config/routes.rb +59 -0
  115. data/spec/dummy/config/secrets.yml +22 -0
  116. data/spec/dummy/db/development.sqlite3 +0 -0
  117. data/spec/dummy/db/migrate/20141126120426_create_users.rb +13 -0
  118. data/spec/dummy/db/schema.rb +26 -0
  119. data/spec/dummy/db/test.sqlite3 +0 -0
  120. data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +13 -0
  121. data/spec/dummy/log/development.log +16 -0
  122. data/spec/dummy/log/test.log +158 -0
  123. data/spec/dummy/public/404.html +67 -0
  124. data/spec/dummy/public/422.html +67 -0
  125. data/spec/dummy/public/500.html +66 -0
  126. data/spec/dummy/public/favicon.ico +0 -0
  127. data/spec/factories/user_factory.rb +10 -0
  128. data/spec/spec_helper.rb +18 -0
  129. metadata +383 -0
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Gumby RadioBtn
3
+ */
4
+ !function($) {
5
+
6
+ 'use strict';
7
+
8
+ function RadioBtn($el) {
9
+
10
+ Gumby.debug('Initializing Radio Button', $el);
11
+
12
+ this.$el = $el;
13
+ this.$input = this.$el.find('input[type=radio]');
14
+
15
+ var scope = this;
16
+
17
+ // listen for click event and custom gumby check event
18
+ this.$el.on(Gumby.click, function(e) {
19
+ // prevent radio button checking, we'll do that manually
20
+ e.preventDefault();
21
+
22
+ // do nothing if radio is disabled
23
+ if (scope.$input.is('[disabled]')) {
24
+ return;
25
+ }
26
+
27
+ // check radio button
28
+ scope.update();
29
+ }).on('gumby.check', function() {
30
+ Gumby.debug('Check event triggered', scope.$el);
31
+ scope.update();
32
+ });
33
+
34
+ // update any prechecked on load
35
+ if(this.$input.prop('checked') || this.$el.hasClass('checked')) {
36
+ scope.update(true);
37
+ }
38
+ }
39
+
40
+ // check radio button, uncheck all others in name group
41
+ RadioBtn.prototype.update = function() {
42
+
43
+ // already checked so no need to update
44
+ if(this.$el.hasClass('checked') && this.$input.prop('checked') && this.$el.find('i.icon-dot').length) {
45
+ return;
46
+ }
47
+
48
+ Gumby.debug('Updating Radio Button group', this.$el);
49
+
50
+ var $span = this.$el.find('span'),
51
+ // the group of radio buttons
52
+ group = 'input[name="'+this.$input.attr('name')+'"]';
53
+
54
+ // uncheck radio buttons in same group - uncheck input, remove checked class, remove <i>
55
+ $('.radio').has(group).removeClass('checked')
56
+ .find('input').prop('checked', false).end()
57
+ .find('i').remove();
58
+
59
+ // check this radio button - check input, add checked class, append <i>
60
+ this.$input.prop('checked', true);
61
+ $span.append('<i class="icon-dot" />');
62
+
63
+ Gumby.debug('Triggering onCheck event', this.$el);
64
+
65
+ this.$el.addClass('checked').trigger('gumby.onCheck');
66
+ };
67
+
68
+ // add initialisation
69
+ Gumby.addInitalisation('radiobtn', function() {
70
+ $('.radio').each(function() {
71
+ var $this = $(this);
72
+ // this element has already been initialized
73
+ if($this.data('isRadioBtn')) {
74
+ return true;
75
+ }
76
+ // mark element as initialized
77
+ $this.data('isRadioBtn', true);
78
+ new RadioBtn($this);
79
+ });
80
+ });
81
+
82
+ // register UI module
83
+ Gumby.UIModule({
84
+ module: 'radiobtn',
85
+ events: ['onCheck', 'check'],
86
+ init: function() {
87
+ Gumby.initialize('radiobtn');
88
+ }
89
+ });
90
+ }(jQuery);
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Gumby Retina
3
+ */
4
+ !function($) {
5
+
6
+ 'use strict';
7
+
8
+ function Retina($el) {
9
+
10
+ Gumby.debug('Initializing Retina', $el);
11
+
12
+ this.$el = $el;
13
+ this.imageSrc = this.$el.attr('src');
14
+ this.retinaSrc = this.fetchRetinaImage();
15
+ this.$retinaImg = $(new Image());
16
+
17
+ var scope = this;
18
+
19
+ // image src not valid
20
+ if(!this.retinaSrc) {
21
+ return false;
22
+ }
23
+
24
+ // load retina image
25
+ this.$retinaImg.attr('src', this.retinaSrc).load(function() {
26
+ scope.retinaImageLoaded();
27
+ }).error(function() {
28
+ Gumby.error('Couln\'t load retina image: '+scope.retinaSrc);
29
+ });
30
+ }
31
+
32
+ // fetch retina src by appending '@2x' to image string before extension
33
+ Retina.prototype.fetchRetinaImage = function() {
34
+ var imgSrc = this.imageSrc,
35
+ index = this.imageSrc.search(/(\.|\/)(gif|jpe?g|png)$/i);
36
+
37
+ // image src is not valid
38
+ if(index < 0) {
39
+ return false;
40
+ }
41
+
42
+ // return retina src
43
+ return imgSrc.substr(0, index) + '@2x' + imgSrc.substr(index, imgSrc.length);
44
+ };
45
+
46
+ // once retina image loaded swap original src
47
+ Retina.prototype.retinaImageLoaded = function() {
48
+ Gumby.debug('Swapping image for retina version', this.$el);
49
+ Gumby.debug('Triggering onRetina event', this.$el);
50
+ this.$el.attr('src', this.$retinaImg.attr('src')).trigger('gumby.onRetina');
51
+ };
52
+
53
+ // add initialisation
54
+ Gumby.addInitalisation('retina', function() {
55
+
56
+ // this module is for retina devices only
57
+ if(!window.devicePixelRatio || window.devicePixelRatio <= 1) {
58
+ return;
59
+ }
60
+
61
+ $('img[data-retina],img[gumby-retina],img[retina]').each(function() {
62
+ var $this = $(this);
63
+ // this element has already been initialized
64
+ if($this.data('isRetina')) {
65
+ return true;
66
+ }
67
+ // mark element as initialized
68
+ $this.data('isRetina', true);
69
+ new Retina($this);
70
+ });
71
+ });
72
+
73
+ // register UI module
74
+ Gumby.UIModule({
75
+ module: 'retina',
76
+ events: ['onRetina'],
77
+ init: function() {
78
+ Gumby.initialize('retina');
79
+ }
80
+ });
81
+ }(jQuery);
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Gumby SkipLink
3
+ */
4
+ !function($) {
5
+
6
+ 'use strict';
7
+
8
+ function SkipLink($el) {
9
+
10
+ Gumby.debug('Initializing Skiplink', $el);
11
+
12
+ this.$el = $el;
13
+ this.targetPos = 0;
14
+ this.duration = 0;
15
+ this.offset = false;
16
+ this.easing = '';
17
+ this.update = false;
18
+
19
+ // set up module based on attributes
20
+ this.setup();
21
+
22
+ var scope = this;
23
+
24
+ // skip to target element on click or trigger of gumby.skipTo event
25
+ this.$el.on(Gumby.click+' gumby.skip', function(e) {
26
+ e.preventDefault();
27
+
28
+ if(e.namespace === 'skip') {
29
+ Gumby.debug('Skip event triggered', scope.$el);
30
+ }
31
+
32
+ // calculate target on each click if update var set to true
33
+ if(scope.update) {
34
+ scope.calculateTarget(scope.skipTo);
35
+
36
+ // skip straight to target
37
+ } else {
38
+ scope.skipTo();
39
+ }
40
+ }).on('gumby.initialize', function() {
41
+ Gumby.debug('Re-initializing Skiplink', scope.$el);
42
+ scope.setup();
43
+ });
44
+ }
45
+
46
+ // set up module based on attributes
47
+ SkipLink.prototype.setup = function() {
48
+ this.duration = Number(Gumby.selectAttr.apply(this.$el, ['duration'])) || 200;
49
+ this.offset = Gumby.selectAttr.apply(this.$el, ['offset']) || false;
50
+ this.easing = Gumby.selectAttr.apply(this.$el, ['easing']) || 'swing';
51
+ this.update = Gumby.selectAttr.apply(this.$el, ['update']) ? true : false;
52
+
53
+ this.calculateTarget();
54
+ };
55
+
56
+ // calculate target px point to skip to
57
+ SkipLink.prototype.calculateTarget = function(cb) {
58
+
59
+ var scope = this,
60
+ target = Gumby.selectAttr.apply(this.$el, ['goto']),
61
+ $target;
62
+
63
+ // 'top' specified so target is 0px
64
+ if(target == 'top') {
65
+ this.targetPos = 0;
66
+
67
+ // px point specified
68
+ } else if($.isNumeric(target)) {
69
+ this.targetPos = Number(target);
70
+ } else {
71
+
72
+ // check for element with target as selector
73
+ $target = $(target);
74
+
75
+ // target does not exist, we need a target
76
+ if(!$target.length) {
77
+ Gumby.error('Cannot find skiplink target: '+target);
78
+ return false;
79
+ }
80
+
81
+ this.targetPos = $target.offset().top;
82
+ }
83
+
84
+ if(cb) {
85
+ cb.apply(this);
86
+ }
87
+ };
88
+
89
+ // animate body, html scrollTop value to target px point
90
+ SkipLink.prototype.skipTo = function() {
91
+
92
+ Gumby.debug('Skipping to target', this.$el);
93
+
94
+ var scope = this;
95
+
96
+ // slide to position of target
97
+ $('html,body').animate({
98
+ 'scrollTop' : this.calculateOffset()
99
+ }, this.duration, this.easing).promise().done(function() {
100
+
101
+ Gumby.debug('Triggering onComplete event', scope.$el);
102
+ scope.$el.trigger('gumby.onComplete');
103
+ });
104
+ };
105
+
106
+ // calculate offset with current target point
107
+ SkipLink.prototype.calculateOffset = function() {
108
+ // no offset so return target here
109
+ if(!this.offset) {
110
+ return this.targetPos;
111
+ }
112
+
113
+ // negative / positive
114
+ var op = this.offset.substr(0, 1),
115
+ off = Number(this.offset.substr(1, this.offset.length));
116
+
117
+ // subtract offset from target position
118
+ if(op === '-') {
119
+ return this.targetPos - off;
120
+ // add offset to target position
121
+ } else if(op === '+') {
122
+ return this.targetPos + off;
123
+ }
124
+ };
125
+
126
+ // add initialisation
127
+ Gumby.addInitalisation('skiplink', function(all) {
128
+ $('.skiplink > a, .skip').each(function() {
129
+ var $this = $(this);
130
+
131
+ // this element has already been initialized
132
+ // and we're only initializing new modules
133
+ if($this.data('isSkipLink') && !all) {
134
+ return true;
135
+
136
+ // this element has already been initialized
137
+ // and we need to reinitialize it
138
+ } else if($this.data('isSkipLink') && all) {
139
+ $this.trigger('gumby.initialize');
140
+ return true;
141
+ }
142
+
143
+ // mark element as initialized
144
+ $this.data('isSkipLink', true);
145
+ new SkipLink($this);
146
+ });
147
+ });
148
+
149
+ // register UI module
150
+ Gumby.UIModule({
151
+ module: 'skiplink',
152
+ events: ['initialize', 'onComplete', 'skip'],
153
+ init: function() {
154
+ Gumby.initialize('skiplink');
155
+ }
156
+ });
157
+ }(jQuery);
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Gumby Tabs
3
+ */
4
+ !function($) {
5
+
6
+ 'use strict';
7
+
8
+ function Tabs($el) {
9
+
10
+ Gumby.debug('Initializing Tabs', $el);
11
+
12
+ this.$el = $el;
13
+ this.$nav = this.$el.find('> ul.tab-nav > li');
14
+ this.$content = this.$el.children('.tab-content');
15
+
16
+ var scope = this;
17
+
18
+ // listen for click event on tab nav and custom gumby set event
19
+ this.$nav.children('a').on(Gumby.click, function(e) {
20
+ e.preventDefault();
21
+ scope.click($(this));
22
+ });
23
+
24
+ // listen for gumby.set value for dynamically set tabs
25
+ this.$el.on('gumby.set', function(e, index) {
26
+ Gumby.debug('Set event triggered', scope.$el);
27
+ scope.set(e, index);
28
+ });
29
+ }
30
+
31
+ // handle tab nav click event
32
+ Tabs.prototype.click = function($this) {
33
+ // index of item to activate
34
+ var index = $this.parent().index();
35
+
36
+ if(this.$nav.eq(index).add(this.$content.eq(index)).hasClass('active')) {
37
+ return;
38
+ }
39
+
40
+ Gumby.debug('Setting active tab to '+index, this.$el);
41
+
42
+ // deactivate other tab navigation and content
43
+ this.$nav.add(this.$content).removeClass('active');
44
+
45
+ // activate this tab nav link and content
46
+ this.$nav.eq(index).add(this.$content.eq(index)).addClass('active');
47
+
48
+ // trigger gumby.change event and pass current active tab index
49
+ Gumby.debug('Triggering onChange event', this.$el);
50
+ this.$el.trigger('gumby.onChange', index);
51
+ };
52
+
53
+ // set specific tab
54
+ Tabs.prototype.set = function(e, index) {
55
+ this.$nav.eq(index).find('a').trigger(Gumby.click);
56
+ };
57
+
58
+ // add initialisation
59
+ Gumby.addInitalisation('tabs', function() {
60
+ $('.tabs').each(function() {
61
+ var $this = $(this);
62
+ // this element has already been initialized
63
+ if($this.data('isTabs')) {
64
+ return true;
65
+ }
66
+ // mark element as initialized
67
+ $this.data('isTabs', true);
68
+ new Tabs($this);
69
+ });
70
+ });
71
+
72
+ // register UI module
73
+ Gumby.UIModule({
74
+ module: 'tabs',
75
+ events: ['onChange', 'set'],
76
+ init: function() {
77
+ Gumby.initialize('tabs');
78
+ }
79
+ });
80
+ }(jQuery);
@@ -0,0 +1,264 @@
1
+ /**
2
+ * Gumby Toggles/Switches
3
+ */
4
+ !function($) {
5
+
6
+ 'use strict';
7
+
8
+ // Toggle constructor
9
+ function Toggle($el) {
10
+ this.$el = $($el);
11
+ this.targets = [];
12
+ this.on = '';
13
+ this.className = '';
14
+ this.self = false;
15
+
16
+ if(this.$el.length) {
17
+ Gumby.debug('Initializing Toggle', $el);
18
+ this.init();
19
+ }
20
+ }
21
+
22
+ // Switch constructor
23
+ function Switch($el) {
24
+ this.$el = $($el);
25
+ this.targets = [];
26
+ this.on = '';
27
+ this.className = '';
28
+ this.self = false;
29
+
30
+ if(this.$el.length) {
31
+ Gumby.debug('Initializing Switch', $el);
32
+ this.init();
33
+ }
34
+ }
35
+
36
+ // intialise toggles, switches will inherit method
37
+ Toggle.prototype.init = function() {
38
+ var scope = this;
39
+
40
+ // set up module based on attributes
41
+ this.setup();
42
+
43
+ // bind to specified event and trigger
44
+ this.$el.on(this.on, function(e) {
45
+ e.preventDefault();
46
+ scope.trigger(scope.triggered);
47
+
48
+ // listen for gumby.trigger to dynamically trigger toggle/switch
49
+ }).on('gumby.trigger', function() {
50
+ Gumby.debug('Trigger event triggered', scope.$el);
51
+ scope.trigger(scope.triggered);
52
+ // re-initialize module
53
+ }).on('gumby.initialize', function() {
54
+ Gumby.debug('Re-initializing '+scope.constructor, $el);
55
+ scope.setup();
56
+ });
57
+ };
58
+
59
+ // set up module based on attributes
60
+ Toggle.prototype.setup = function() {
61
+ this.targets = this.parseTargets();
62
+ this.on = Gumby.selectAttr.apply(this.$el, ['on']) || Gumby.click;
63
+ this.className = Gumby.selectAttr.apply(this.$el, ['classname']) || 'active';
64
+ this.self = Gumby.selectAttr.apply(this.$el, ['self']) === 'false';
65
+ };
66
+
67
+ // parse data-for attribute, switches will inherit method
68
+ Toggle.prototype.parseTargets = function() {
69
+ var targetStr = Gumby.selectAttr.apply(this.$el, ['trigger']),
70
+ secondaryTargets = 0,
71
+ targets = [];
72
+
73
+ // no targets so return false
74
+ if(!targetStr) {
75
+ return false;
76
+ }
77
+
78
+ secondaryTargets = targetStr.indexOf('|');
79
+
80
+ // no secondary targets specified so return single target
81
+ if(secondaryTargets === -1) {
82
+ if(!this.checkTargets([targetStr])) {
83
+ return false;
84
+ }
85
+ return [$(targetStr)];
86
+ }
87
+
88
+ // return array of both targets, split and return 0, 1
89
+ targets = targetStr.split('|');
90
+ if(!this.checkTargets(targets)) {
91
+ return false;
92
+ }
93
+ return targets.length > 1 ? [$(targets[0]), $(targets[1])] : [$(targets[0])];
94
+ };
95
+
96
+ Toggle.prototype.checkTargets = function(targets) {
97
+ var i = 0;
98
+
99
+ for(i; i < targets.length; i++) {
100
+ if(targets[i] && !$(targets[i]).length) {
101
+ Gumby.error('Cannot find '+this.constructor.name+' target: '+targets[i]);
102
+ return false;
103
+ }
104
+ }
105
+
106
+ return true;
107
+ };
108
+
109
+ // call triggered event and pass target data
110
+ Toggle.prototype.triggered = function() {
111
+ // trigger gumby.onTrigger event and pass array of target status data
112
+ Gumby.debug('Triggering onTrigger event', this.$el);
113
+ this.$el.trigger('gumby.onTrigger', [this.$el.hasClass(this.className)]);
114
+ };
115
+
116
+ // Switch object inherits from Toggle
117
+ Switch.prototype = new Toggle();
118
+ Switch.prototype.constructor = Switch;
119
+
120
+ // Toggle specific trigger method
121
+ Toggle.prototype.trigger = function(cb) {
122
+
123
+ Gumby.debug('Triggering Toggle', this.$el);
124
+
125
+ var $target;
126
+
127
+ // no targets just toggle active class on toggle
128
+ if(!this.targets) {
129
+ this.$el.toggleClass(this.className);
130
+
131
+ // combine single target with toggle and toggle active class
132
+ } else if(this.targets.length == 1) {
133
+ this.$el.add(this.targets[0]).toggleClass(this.className);
134
+
135
+ // if two targets check active state of first
136
+ // always combine toggle and first target
137
+ } else if(this.targets.length > 1) {
138
+ if(this.targets[0].hasClass(this.className)) {
139
+ $target = this.targets[0];
140
+
141
+ // add this element to it unless gumby-self set
142
+ if(!this.self) {
143
+ $target = $target.add(this.$el);
144
+ }
145
+
146
+ $target.removeClass(this.className);
147
+ this.targets[1].addClass(this.className);
148
+ } else {
149
+ $target = this.targets[0];
150
+
151
+ // add this element to it unless gumby-self set
152
+ if(!this.self) {
153
+ $target = $target.add(this.$el);
154
+ }
155
+
156
+ $target.addClass(this.className);
157
+ this.targets[1].removeClass(this.className);
158
+ }
159
+ }
160
+
161
+ // call event handler here, applying scope of object Switch/Toggle
162
+ if(cb && typeof cb === 'function') {
163
+ cb.apply(this);
164
+ }
165
+ };
166
+
167
+ // Switch specific trigger method
168
+ Switch.prototype.trigger = function(cb) {
169
+
170
+ Gumby.debug('Triggering Switch', this.$el);
171
+
172
+ var $target;
173
+
174
+ // no targets just add active class to switch
175
+ if(!this.targets) {
176
+ this.$el.addClass(this.className);
177
+
178
+ // combine single target with switch and add active class
179
+ } else if(this.targets.length == 1) {
180
+ $target = this.targets[0];
181
+
182
+ // add this element to it unless gumby-self set
183
+ if(!this.self) {
184
+ $target = $target.add(this.$el);
185
+ }
186
+
187
+ $target.addClass(this.className);
188
+
189
+ // if two targets check active state of first
190
+ // always combine switch and first target
191
+ } else if(this.targets.length > 1) {
192
+ $target = this.targets[0];
193
+
194
+ // add this element to it unless gumby-self set
195
+ if(!this.self) {
196
+ $target = $target.add(this.$el);
197
+ }
198
+
199
+ $target.addClass(this.className);
200
+ this.targets[1].removeClass(this.className);
201
+ }
202
+
203
+ // call event handler here, applying scope of object Switch/Toggle
204
+ if(cb && typeof cb === 'function') {
205
+ cb.apply(this);
206
+ }
207
+ };
208
+
209
+ // add toggle initialisation
210
+ Gumby.addInitalisation('toggles', function(all) {
211
+ $('.toggle').each(function() {
212
+ var $this = $(this);
213
+
214
+ // this element has already been initialized
215
+ // and we're only initializing new modules
216
+ if($this.data('isToggle') && !all) {
217
+ return true;
218
+
219
+ // this element has already been initialized
220
+ // and we need to reinitialize it
221
+ } else if($this.data('isToggle') && all) {
222
+ $this.trigger('gumby.initialize');
223
+ }
224
+
225
+ // mark element as initialized
226
+ $this.data('isToggle', true);
227
+ new Toggle($this);
228
+ });
229
+ });
230
+
231
+ // add switches initialisation
232
+ Gumby.addInitalisation('switches', function(all) {
233
+ $('.switch').each(function() {
234
+ var $this = $(this);
235
+
236
+ // this element has already been initialized
237
+ // and we're only initializing new modules
238
+ if($this.data('isSwitch') && !all) {
239
+ return true;
240
+
241
+ // this element has already been initialized
242
+ // and we need to reinitialize it
243
+ } else if($this.data('isSwitch') && all) {
244
+ $this.trigger('gumby.initialize');
245
+ return true;
246
+ }
247
+
248
+ // mark element as initialized
249
+ $this.data('isSwitch', true);
250
+ new Switch($this);
251
+ });
252
+ });
253
+
254
+ // register UI module
255
+ Gumby.UIModule({
256
+ module: 'toggleswitch',
257
+ events: ['initialize', 'trigger', 'onTrigger'],
258
+ init: function() {
259
+ // Run initialize methods
260
+ Gumby.initialize('switches');
261
+ Gumby.initialize('toggles');
262
+ }
263
+ });
264
+ }(jQuery);