railslider 0.0.1 → 0.1.0

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.
@@ -1,5 +1,9 @@
1
- require "railslider/version"
1
+ require 'railslider/version'
2
+ require 'railslider/image'
3
+ require 'railslider/engine'
2
4
 
3
5
  module Railslider
4
6
  # Your code goes here...
5
7
  end
8
+
9
+
@@ -0,0 +1,9 @@
1
+ # This gem contains an engine class which inherits from Rails::Engine. By doing this, Rails is
2
+ # informed that the directory for this gem may contain assets and the app/assets, lib/assets
3
+ # and vendor/assets directories of this engine are added to the search path of Sprockets.
4
+ # Official reference: http://guides.rubyonrails.org/asset_pipeline.html#adding-assets-to-your-gems
5
+
6
+ module Railslider
7
+ class Engine < ::Rails::Engine
8
+ end
9
+ end
@@ -0,0 +1,177 @@
1
+ module Railslider
2
+ #
3
+ class Image
4
+
5
+ # image use if such is not specified
6
+ DEMO_IMAGES = ['/assets/demo/1.jpg',
7
+ '/assets/demo/2.jpg',
8
+ '/assets/demo/3.jpg',
9
+ '/assets/demo/4.jpg',
10
+ '/assets/demo/5.jpg']
11
+
12
+ # class attributes
13
+ attr_accessor :id, # elements id
14
+ #
15
+ :images_urls, # array holding images urls
16
+ #
17
+ :effect # initial effect to be used:
18
+ #
19
+ # flips: flip01, flip02, flip03, flip04
20
+ # rotations: rotation01, rotation02, rotation03, rotation04, rotation05
21
+ # multi-flips: multi-flip01, multi-flip02, multi-flip03
22
+ # cubes: cube01, cube02, cube03, cube04
23
+ # unfolds: unfold01, unfold02
24
+ # others: other01, other02, other03 ,other04 ,other05 ,other06 ,other07
25
+
26
+ # extracting input parameters and using defaults if necessary
27
+ def initialize(parameters = {})
28
+ @id = parameters.fetch(:id, 'demo')
29
+ @images_urls = parameters.fetch(:images_urls, DEMO_IMAGES)
30
+
31
+ @effect = parameters.fetch(:effect, 'flip01')
32
+ end
33
+
34
+ # rendering images without rails slider effects
35
+ def to_s
36
+ @result_html = ''
37
+ @images_urls.each do |url|
38
+ @result_html += "<img src=\"#{url}\"/>"
39
+ end
40
+ @result_html.html_safe
41
+ end
42
+
43
+ # rendering images with rails slider effects
44
+ def render
45
+ @result_html = ''
46
+ @result_html += "<div class=\"rs-container #{self.class.name}\" id=\"#{@id}\">"
47
+ @result_html += render_controls.gsub("<option value=\"rs-#{@effect}\">",
48
+ "<option value=\"rs-#{@effect}\" selected=\"selected\">")
49
+ @result_html += "<div class=\"rs-wrapper\">"
50
+ @result_html += "<div class=\"rs-shadow\"></div>"
51
+ @result_html += '<div class="rs-images">'
52
+ @images_urls.each do |url|
53
+ @result_html += "<img src=\"#{url}\"/>"
54
+ end
55
+ @result_html += '</div>'
56
+ @result_html += '<div class="rs-cover">'
57
+ @result_html += '<a class="rs-animation-command rs-pause" href="javascript:void(0)">Play/Pause</a>'
58
+ @result_html += "<img src=\"#{@images_urls.first}\"/>"
59
+ @result_html += '</div>'
60
+ @result_html += '<div class="rs-transition">'
61
+ @result_html += render_flips
62
+ @result_html += render_multi_flips
63
+ @result_html += render_cubes
64
+ @result_html += render_unfolds
65
+ @result_html += '</div>'
66
+ @result_html += '</div>'
67
+ @result_html += render_bullets
68
+ @result_html += '</div>'
69
+
70
+ @result_html.html_safe
71
+ end
72
+
73
+ def render_controls
74
+ "
75
+ <div class=\"rs-controls\">
76
+ <label for=\"rs-effects\">
77
+ <select class=\"rs-effects\">
78
+ <optgroup label=\"rs-flips\">
79
+ <option value=\"rs-flip01\">flip 01</option>
80
+ <option value=\"rs-flip02\">flip 02</option>
81
+ <option value=\"rs-flip03\">flip 03</option>
82
+ <option value=\"rs-flip04\">flip 04</option>
83
+ </optgroup>
84
+ <optgroup label=\"rs-rotations\">
85
+ <option value=\"rs-rotation01\">rotation 01</option>
86
+ <option value=\"rs-rotation02\">rotation 02</option>
87
+ <option value=\"rs-rotation03\">rotation 03</option>
88
+ <option value=\"rs-rotation04\">rotation 04</option>
89
+ <option value=\"rs-rotation05\">rotation 05</option>
90
+ </optgroup>
91
+ <optgroup label=\"rs-multi-flips\">
92
+ <option value=\"rs-multi-flip01\">multi-flip 01</option>
93
+ <option value=\"rs-multi-flip02\">multi-flip 02</option>
94
+ <option value=\"rs-multi-flip03\">multi-flip 03</option>
95
+ </optgroup>
96
+ <optgroup label=\"rs-cubes\">
97
+ <option value=\"rs-cube01\">Cube 01</option>
98
+ <option value=\"rs-cube02\">Cube 02</option>
99
+ <option value=\"rs-cube03\">Cube 03</option>
100
+ <option value=\"rs-cube04\">Cube 04</option>
101
+ </optgroup>
102
+ <optgroup label=\"rs-unfolds\">
103
+ <option value=\"rs-unfold01\">Unfold 01</option>
104
+ <option value=\"rs-unfold02\">Unfold 02</option>
105
+ </optgroup>
106
+ <optgroup label=\"rs-others\">
107
+ <option value=\"rs-other01\">Other 01</option>
108
+ <option value=\"rs-other02\">Other 02</option>
109
+ <option value=\"rs-other03\">Other 03</option>
110
+ <option value=\"rs-other04\">Other 04</option>
111
+ <option value=\"rs-other05\">Other 05</option>
112
+ <option value=\"rs-other06\">Other 06</option>
113
+ <option value=\"rs-other07\">Other 07</option>
114
+ </optgroup>
115
+ </select>
116
+ </label>
117
+ <a class=\"rs-next\" href=\"#\">next</a>
118
+ <a class=\"rs-prev\" href=\"#\">back</a>
119
+ </div>
120
+ "
121
+ end
122
+
123
+ def render_flips
124
+ "
125
+ <div class=\"rs-flip rs-card\">
126
+ <div class=\"rs-front\"></div>
127
+ <div class=\"rs-back\"></div>
128
+ </div>
129
+ "
130
+ end
131
+
132
+ def render_multi_flips
133
+ @result_html = ''
134
+ for index in 1 .. 7
135
+ @result_html +=
136
+ "
137
+ <div class=\"rs-multi-flip rs-card rs-flip#{index}\">
138
+ <div class=\"rs-front\"></div>
139
+ <div class=\"rs-back\"></div>
140
+ </div>
141
+ "
142
+ end
143
+ @result_html
144
+ end
145
+
146
+ def render_cubes
147
+ "
148
+ <div class=\"rs-cube rs-cube-front rs-cube-face rs-front\"></div>
149
+ <div class=\"rs-cube rs-cube-top rs-cube-face rs-back\"></div>
150
+ <div class=\"rs-cube rs-cube-bottom rs-cube-face rs-back\"></div>
151
+ <div class=\"rs-cube rs-cube-right rs-cube-face rs-back\"></div>
152
+ <div class=\"rs-cube rs-cube-left rs-cube-face rs-back\"></div>
153
+ "
154
+ end
155
+
156
+ def render_unfolds
157
+ "
158
+ <div class=\"rs-unfold rs-front rs-front1\"></div>
159
+ <div class=\"rs-unfold rs-front rs-front2\"></div>
160
+ <div class=\"rs-unfold rs-front rs-front3\"></div>
161
+ <div class=\"rs-unfold rs-back rs-back1\"></div>
162
+ <div class=\"rs-unfold rs-back rs-back2\"></div>
163
+ <div class=\"rs-unfold rs-back rs-back3\"></div>
164
+ "
165
+ end
166
+
167
+ def render_bullets
168
+ @result_html = ''
169
+ @result_html += '<ul class="rs-bullets">'
170
+
171
+ @images_urls.each_with_index do |url, index|
172
+ @result_html += "<li><a data-position=\"#{index+1}\" href=\"#\">hidden text</a></li>"
173
+ end
174
+ @result_html += '</ul>'
175
+ end
176
+ end
177
+ end
@@ -1,3 +1,3 @@
1
1
  module Railslider
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Railslider::VERSION
9
9
  spec.authors = ['gotqn']
10
10
  spec.email = ['george_27@abv.bg']
11
- spec.description = 'Do not download it - just reserving the name'
12
- spec.summary = 'The first version will be released soon'
13
- spec.homepage = ''
11
+ spec.description = 'Introduces simple image slider for rails applications using CSS3 animations for image transitions.'
12
+ spec.summary = 'The idea is inspired by [this great codedrops article] (http://tympanus.net/codrops/2011/12/19/experimental-css3-animations-for-image-transitions/).'
13
+ spec.homepage = 'https://github.com/gotqn/railslider'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -0,0 +1,290 @@
1
+ //Checking if jQuery function is loaded
2
+ (function IsjQueryLoaded(){
3
+
4
+ if(window.jQuery){
5
+
6
+ $(function(){
7
+
8
+ //Checking if "RailSlider" class is already created
9
+ if(typeof RailSlider === 'undefined' || jQuery.isEmptyObject(RailSlider.prototype)){
10
+
11
+ // constructor function
12
+ RailSlider = function(params){
13
+
14
+ this.containerID = params['containerID'];
15
+ this.teWrapper = $('#' + this.containerID + ' .rs-wrapper');
16
+ this.rsCover = $('#' + this.containerID + ' div.rs-cover');
17
+ this.rsImages = $('#' + this.containerID + ' div.rs-images > img');
18
+ this.imagesCount = $(this.rsImages).length;
19
+ this.currentImg = 0;
20
+ this.navNext = $('#' + this.containerID + ' .rs-next');
21
+ this.navPrev = $('#' + this.containerID + ' .rs-prev');
22
+ //holds all animation options
23
+ this.rsEffect = $('#' + this.containerID + ' .rs-effects');
24
+ //holds selection animation option
25
+ this.type= $(this.rsEffect).val();
26
+ //holds animation option used during transition
27
+ this.rsTransition = $('#' + this.containerID + ' .rs-transition');
28
+ //flag pointing if transition is active
29
+ this.animated = false;
30
+ //bullets
31
+ this.bullets = $('#' + this.containerID + ' .rs-bullets a');
32
+ //auto sliding settings
33
+ this.isAutoSlidingEnabled = true;
34
+ this.direction = "next"; //"next" or "prev"
35
+ this.timeInterval = 5000; // time interval im milliseconds
36
+ this.isAutoSlidingOn = false;
37
+ this.autoSlidingID = null; // "id" returned by "setInterval()" function
38
+ //others
39
+ this.controls = $('#' + this.containerID + ' .rs-controls');
40
+ this.effectSelector = '';
41
+ };
42
+
43
+ RailSlider.prototype = {
44
+
45
+ constructor: RailSlider,
46
+ hasPerspective: Modernizr.csstransforms3d,
47
+
48
+ init: function(){
49
+
50
+ //set transition animation option
51
+ this.rsTransition.addClass(this.type);
52
+
53
+ this.navNext.on('click', $.proxy(function(){
54
+
55
+ //checking if browser supports 3D animations and an animation is not already running
56
+ if(!(this.hasPerspective && this.animated)){
57
+ this.animated = true;
58
+ this.showNext('next');
59
+ }
60
+
61
+ //calling "event.stopPropagation()" and "event.preventDefault()"
62
+ return false;
63
+
64
+ }, this));
65
+
66
+ this.navPrev.on('click', $.proxy(function(){
67
+
68
+ //checking if browser supports 3D animations and an animation is not already running
69
+ if(!(this.hasPerspective && this.animated)){
70
+ this.animated = true;
71
+ this.showNext('prev');
72
+ }
73
+
74
+ //calling "event.stopPropagation()" and "event.preventDefault()"
75
+ return false;
76
+
77
+ }, this));
78
+
79
+ if(this.hasPerspective){
80
+
81
+ this.teWrapper.on({
82
+
83
+ 'animationstart webkitAnimationStart oAnimationStart MSAnimationStart': $.proxy(function(event){
84
+
85
+ //ignoring "play" and "pause" buttons animations
86
+ if(event.originalEvent.animationName == 'play-button-in' || event.originalEvent.animationName == 'pause-button-in'){
87
+ return false;
88
+ }else{
89
+ this.rsEffect.prop('disabled', true);
90
+ return false;
91
+ }
92
+ }, this),
93
+ 'animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd': $.proxy(function(event){
94
+
95
+ //ignoring "play" and "pause" buttons animations
96
+ if(event.originalEvent.animationName == 'play-button-in' || event.originalEvent.animationName == 'pause-button-in'){
97
+ return false;
98
+ }else{
99
+
100
+ if((this.type === 'rs-unfold01' && event.originalEvent.animationName !== 'rs-unfold1_3Back')||(this.type === 'rs-unfold02' && event.originalEvent.animationName !== 'rs-unfold2_3Back')){
101
+ return false;
102
+ }
103
+
104
+ this.rsCover.removeClass('rs-hide');
105
+ this.rsTransition.removeClass('rs-show');
106
+ this.animated = false;
107
+ this.rsEffect.prop('disabled', false);
108
+
109
+ return false;
110
+ }
111
+
112
+ }, this)
113
+ });
114
+ }
115
+
116
+ //update "transition" animation option
117
+ $(this.rsEffect).on('change.RailSlider', $.proxy(function(event) {
118
+ if(this.animated){
119
+ $(event.currentTarget).val(this.type);
120
+ }else{
121
+ this.type = $(event.currentTarget).val();
122
+ this.rsTransition.removeClass().addClass('rs-transition').addClass(this.type);
123
+ this.effectSelector = this.effectsSelector();
124
+ }
125
+ }, this));
126
+
127
+ //handling bullets events
128
+ for(var index = 0; index < this.bullets.length; index++){
129
+
130
+ this.bullets.eq(index).on('click', $.proxy(function(event){
131
+
132
+ var currentElementPosition = $(event.target).attr('data-position');
133
+
134
+ //checking if browser supports 3D animations and an animation is not already running
135
+ if(!(this.hasPerspective && this.animated) && this.currentImg + 1 != currentElementPosition){
136
+
137
+ this.animated = true;
138
+ this.changeBullet(currentElementPosition - 1);
139
+ this.showNext(currentElementPosition);
140
+ }
141
+
142
+ //calling "event.stopPropagation()" and "event.preventDefault()"
143
+ return false;
144
+
145
+ }, this));
146
+ }
147
+
148
+ this.changeBullet(this.currentImg);
149
+
150
+ //handling commands ("play" and "pause") events
151
+ this.rsCover.on('click','.rs-animation-command',$.proxy(function(event) {
152
+ this.toggleAnimation();
153
+ if($(event.target).hasClass('rs-pause')){
154
+ $(event.target).removeClass('rs-pause');
155
+ $(event.target).addClass('rs-play');
156
+ }else{
157
+ $(event.target).removeClass('rs-play');
158
+ $(event.target).addClass('rs-pause');
159
+ }
160
+ }, this));
161
+
162
+ //starting auto sliding if needed
163
+ if(this.isAutoSlidingEnabled){
164
+ this.toggleAnimation();
165
+ }
166
+
167
+ //initializing selector depending on selected effect type
168
+ this.effectSelector = this.effectsSelector();
169
+ },
170
+
171
+ changeBullet:function(index){
172
+ this.bullets.removeClass('shown').eq(index).addClass('shown');
173
+ },
174
+
175
+ showNext:function(direction){
176
+
177
+ if (this.hasPerspective) {
178
+
179
+ this.rsTransition.addClass('rs-show');
180
+ this.rsCover.addClass('rs-hide');
181
+ }
182
+
183
+ this.updateImages(direction);
184
+ },
185
+
186
+ updateImages:function(direction){
187
+
188
+ // "direction" can be "prev", "next" or number pointing exact image
189
+ var $last_img = this.rsImages.eq(this.currentImg);
190
+
191
+ switch(direction){
192
+ case 'next':{
193
+ if(this.currentImg === this.imagesCount - 1){
194
+ this.currentImg = 0;
195
+ }else{
196
+ this.currentImg++;
197
+ }
198
+ break;
199
+ }
200
+ case 'prev':{
201
+ if(this.currentImg === 0){
202
+ this.currentImg = this.imagesCount - 1;
203
+ }else{
204
+ this.currentImg--;
205
+ }
206
+ break;
207
+ }
208
+ default:{
209
+
210
+ if(direction > 0 && direction <= this.imagesCount) {
211
+ this.currentImg = --direction ;
212
+ }
213
+
214
+ break;
215
+ }
216
+ }
217
+
218
+ var $currentImg = this.rsImages.eq(this.currentImg);
219
+
220
+ this.rsTransition.find(this.effectSelector + '.rs-front').empty().append('<img src="' + $last_img.attr('src') + '">');
221
+ this.rsTransition.find(this.effectSelector + '.rs-back').empty().append('<img src="' + $currentImg.attr('src') + '">');
222
+
223
+ this.rsCover.find('img').attr('src', $currentImg.attr('src'));
224
+
225
+ this.changeBullet(this.currentImg);
226
+ },
227
+
228
+ toggleNavigation:function(){
229
+ $('#' + this.containerID + ' .rs-controls').toggle();
230
+ },
231
+
232
+ toggleAnimation:function(){
233
+ if(this.isAutoSlidingOn){
234
+ clearInterval(this.autoSlidingID);
235
+ this.isAutoSlidingOn = false;
236
+ }else{
237
+ this.autoSlidingID = setInterval($.proxy(function(event){this.simulatedClick(this)}, this),this.timeInterval);
238
+ this.isAutoSlidingOn = true;
239
+ }
240
+ },
241
+
242
+ simulatedClick:function(){
243
+ if(this.direction == 'next'){
244
+ this.navNext.trigger('click');
245
+ } else {
246
+ this.navPrev.trigger('click');
247
+ }
248
+ },
249
+
250
+ effectsSelector:function(){
251
+ var currentGroup = this.rsEffect.find('option:selected').closest('optgroup').prop('label');
252
+
253
+ switch(currentGroup){
254
+ case 'rs-flips': currentGroup = '.rs-flip ';break;
255
+ case 'rs-rotations': currentGroup = '.rs-flip ';break;
256
+ case 'rs-multi-flips': currentGroup = '.rs-multi-flip ';break;
257
+ case 'rs-cubes': currentGroup = '.rs-cube';break;
258
+ case 'rs-unfolds': currentGroup = '.rs-unfold';break;
259
+ case 'rs-others': currentGroup = '';break;
260
+ }
261
+
262
+ return currentGroup;
263
+ }
264
+ };
265
+ }
266
+ });
267
+
268
+ }else{
269
+ setTimeout(IsjQueryLoaded(),100);
270
+ }
271
+ })();
272
+
273
+
274
+ /* Example call
275
+
276
+ //Checking if jQuery function is loaded
277
+ (function IsjQueryLoaded(){
278
+ if(window.jQuery){
279
+ //Waiting for all elements to be rendered
280
+ $(document).ready(function(){
281
+ //Creating new instance of "RailSlider"
282
+ test = new RailSlider({'containerID':'test'});
283
+ test.init();
284
+ });
285
+ }else{
286
+ setTimeout(IsjQueryLoaded(),100);
287
+ }
288
+ })();
289
+
290
+ */