pagination_ajax 1.0.5

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.
@@ -0,0 +1,13 @@
1
+ JST = {
2
+ 'templates/test': function(obj){
3
+ var __p=[];
4
+ print=function(){
5
+ __p.push.apply(__p,arguments);
6
+ };
7
+ with(obj||{}){
8
+ __p.push('<div class=\'comment\'>\n\t', comment ,' - ', author ,'\n</div>\n');
9
+ }
10
+
11
+ return __p.join('');
12
+ }
13
+ }
@@ -0,0 +1,27 @@
1
+ (function ($) {
2
+ $.fn.handleLinkClick = function(){
3
+
4
+ $(this).on('click', function(e){
5
+ e.preventDefault();
6
+ var $el = $(this);
7
+ var $parent = $el.closest('.pagination_container');
8
+ var $p_canv = $el.parent().siblings('.pagination_canvas');
9
+ var data_attributes = $parent.data();
10
+ var server_response = ajaxPagination.sendPaginationRequest(data_attributes);
11
+ var response;
12
+ server_response.done(function(data){
13
+ response = {status: 'success', data: data};
14
+ ajaxPagination.buildDomElements($p_canv, data_attributes.template, response);
15
+ ajaxPagination.drawLinkIcons($el, $parent, response);
16
+ }).fail(function(jqXHR, textStatus, errorThrown){
17
+ response = {status: 'fail', data: []};
18
+ ajaxPagination.buildDomElements($p_canv, data_attributes.template, response);
19
+ });
20
+ });
21
+
22
+ };
23
+ })(jQuery);
24
+
25
+
26
+
27
+
@@ -0,0 +1,143 @@
1
+ describe('Pagination Link Generation', function(){
2
+
3
+ beforeEach(function(){
4
+ loadFixtures('pagination_canvas.html');
5
+ });
6
+
7
+ describe('drawing the list of links', function(){
8
+
9
+ it('should create a link for every array item passed into drawLinkIcons()', function(){
10
+ loadFixtures('complete_pagination_dom.html');
11
+ spyOn(ajaxPagination,'buildLinkList').and.returnValue([1,2,3,4,5,'...',10,'next']);
12
+
13
+ var $el = $('.selected_page');
14
+ var $parent = $('.pagination_container');
15
+ var response = {data: [{},{}]};
16
+
17
+ ajaxPagination.drawLinkIcons($el, $parent, response);
18
+ var html = $('.link_canvas').html();
19
+ expect(html).toEqual('<a href="#" class="pagination_link" data-element_number="0">1</a><a href="#" class="pagination_link" data-element_number="1">2</a><a href="#" class="pagination_link" data-element_number="2">3</a><a href="#" class="pagination_link" data-element_number="3">4</a><a href="#" class="pagination_link selected_page" data-element_number="4">5</a><span>...</span><a href="#" class="pagination_link" data-element_number="9">10</a><a href="#" class="pagination_link" data-element_number="next">next</a>')
20
+ });
21
+
22
+ });
23
+
24
+ describe('displays a truncated listing of links if there are MORE THAN 7 pages', function(){
25
+
26
+ it('should display the proper list of links given 10 pages of links and current_page being the first page', function(){
27
+ var $parent = $('.pagination_container');
28
+ $parent.data('current_page',0);
29
+ $parent.data('per_page',2);
30
+ $parent.data('total',20);
31
+ var data = [{},{}];
32
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual([1,2,3,4,5,'...',10,'next']);
33
+ });
34
+
35
+ it('should display the proper list of links given 10 pages of links and current_page == 1', function(){
36
+ var $parent = $('.pagination_container');
37
+ $parent.data('current_page',1);
38
+ $parent.data('per_page',2);
39
+ $parent.data('total',20);
40
+ var data = [{},{}];
41
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,2,3,4,5,'...',10,'next']);
42
+ });
43
+
44
+ it('should display the proper list of links given 10 pages of links and current_page == 5', function(){
45
+ var $parent = $('.pagination_container');
46
+ $parent.data('current_page',5);
47
+ $parent.data('per_page',2);
48
+ $parent.data('total',20);
49
+ var data = [{},{}];
50
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,'...',4,5,6,7,8,'...',10,'next']);
51
+ });
52
+
53
+ it('should display the proper list of links given 10 pages of links and current_page == 8', function(){
54
+ var $parent = $('.pagination_container');
55
+ $parent.data('current_page',8);
56
+ $parent.data('per_page',2);
57
+ $parent.data('total',20);
58
+ var data = [{},{}];
59
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,'...',6,7,8,9,10,'next']);
60
+ });
61
+
62
+ it('should display the proper list of links given 10 pages of links and current_page being the last page', function(){
63
+ var $parent = $('.pagination_container');
64
+ $parent.data('current_page',9);
65
+ $parent.data('per_page',2);
66
+ $parent.data('total',20);
67
+ var data = [{},{}];
68
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,'...',6,7,8,9,10]);
69
+ });
70
+
71
+ });
72
+
73
+ describe('displays a listing of links if there are LESS THAN 7 pages', function(){
74
+
75
+ it('should display all pages without the "prev" link given current_page being the first page', function(){
76
+ loadFixtures('pagination_canvas.html');
77
+ var $parent = $('.pagination_container');
78
+ $parent.data('current_page',0);
79
+ $parent.data('per_page',2);
80
+ $parent.data('total',10);
81
+ var data = [{},{}];
82
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual([1,2,3,4,5,'next']);
83
+ });
84
+
85
+ it('should display all pages without the "next" link given current_page being other than the first or last page', function(){
86
+ loadFixtures('pagination_canvas.html');
87
+ var $parent = $('.pagination_container');
88
+ $parent.data('current_page',4);
89
+ $parent.data('per_page',2);
90
+ $parent.data('total',10);
91
+ var data = [{},{}];
92
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,2,3,4,5]);
93
+ });
94
+
95
+ it('should display all pages and the "next" and "prev" links given current_page is not the first or last page', function(){
96
+ loadFixtures('pagination_canvas.html');
97
+ var $parent = $('.pagination_container');
98
+ $parent.data('current_page',2);
99
+ $parent.data('per_page',2);
100
+ $parent.data('total',10);
101
+ var data = [{},{}];
102
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev',1,2,3,4,5,'next']);
103
+ });
104
+
105
+ });
106
+
107
+ describe('displays a combination of the "prev" and "next" links given incomplete information from the user', function(){
108
+
109
+ it('should only display the "next" link if no per_page is given and the current_page == 0, ', function(){
110
+ var $parent = $('.pagination_container');
111
+ $parent.data('current_page',0);
112
+ $parent.data('total',10);
113
+ var data = [{},{}];
114
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['next']);
115
+ });
116
+
117
+ it('should only display the "prev" link if no per_page is given and the current_page != 0 and data.length == 0, ', function(){
118
+ var $parent = $('.pagination_container');
119
+ $parent.data('current_page',4);
120
+ $parent.data('total',10);
121
+ var data = [];
122
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev']);
123
+ });
124
+
125
+ it('should display the "prev" link if no per_page is given and the current_page != 0 and data.length == 0, ', function(){
126
+ var $parent = $('.pagination_container');
127
+ $parent.data('current_page',4);
128
+ $parent.data('total',10);
129
+ var data = [];
130
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev']);
131
+ });
132
+
133
+ it('should display the "prev" and "next" links if no per_page is given and the current_page != 0 and data.length > 0, ', function(){
134
+ var $parent = $('.pagination_container');
135
+ $parent.data('current_page',4);
136
+ $parent.data('total',10);
137
+ var data = [{},{}];
138
+ expect(ajaxPagination.buildLinkList($parent, data)).toEqual(['prev','next']);
139
+ });
140
+
141
+ });
142
+
143
+ });
@@ -0,0 +1,108 @@
1
+ describe('Pagination Link Interaction', function(){
2
+
3
+ beforeEach(function(){
4
+ loadFixtures('complete_pagination_dom.html');
5
+ });
6
+
7
+ describe('sending a request to the server', function(){
8
+
9
+ it('should call ajaxPagination.sendPaginationRequest() with the data_attributes from the dom when the user clicks on .pagination_link', function(){
10
+ var d = $.Deferred();
11
+ d.resolve();
12
+
13
+ spyOn(ajaxPagination,'sendPaginationRequest').and.returnValue(d.promise());
14
+ spyOn(ajaxPagination,'buildDomElements');
15
+
16
+ var $link = $('.selected_page');
17
+ $link.handleLinkClick();
18
+ $link.trigger('click');
19
+
20
+ var data_attributes = $('.pagination_container').data();
21
+ expect(ajaxPagination.sendPaginationRequest).toHaveBeenCalledWith(data_attributes);
22
+ });
23
+
24
+ });
25
+
26
+ describe('updating the dom given a successful ajax request', function(){
27
+
28
+ it('should increment current page by one if the user clicks the "next" link and add the "selected_page" class to the new link', function(){
29
+ var d = $.Deferred();
30
+ d.resolve([{comment: 'value', author: 'Louie'}]);
31
+ spyOn(ajaxPagination,'sendPaginationRequest').and.returnValue(d.promise());
32
+
33
+ var $container = $('.pagination_container');
34
+
35
+ expect($container.data('current_page')).toEqual(4);
36
+
37
+ var $next = $('.pagination_link[data-element_number="next"]');
38
+ $next.handleLinkClick();
39
+ $next.trigger('click');
40
+
41
+ expect($container.data('current_page')).toEqual(5);
42
+ expect($('.selected_page[data-element_number="5"]').length).toBeGreaterThan(0);
43
+ });
44
+
45
+ it('should decrement current page by one if the user clicks the "prev" link and add the "selected_page" class to the new link', function(){
46
+ var d = $.Deferred();
47
+ d.resolve([{comment: 'value', author: 'Louie'}]);
48
+ spyOn(ajaxPagination,'sendPaginationRequest').and.returnValue(d.promise());
49
+
50
+ var $container = $('.pagination_container');
51
+
52
+ expect($container.data('current_page')).toEqual(4);
53
+
54
+ var $prev = $('.pagination_link[data-element_number="prev"]');
55
+ $prev.handleLinkClick();
56
+ $prev.trigger('click');
57
+
58
+ expect($container.data('current_page')).toEqual(3);
59
+ expect($('.selected_page[data-element_number="3"]').length).toEqual(1);
60
+ });
61
+
62
+ it('should set the current page to the icon that was clicked and add the "selected_page" class to the clicked link', function(){
63
+ var d = $.Deferred();
64
+ d.resolve([{comment: 'value', author: 'Louie'}]);
65
+ spyOn(ajaxPagination,'sendPaginationRequest').and.returnValue(d.promise());
66
+
67
+ var $container = $('.pagination_container');
68
+
69
+ var $link = $('.pagination_link[data-element_number="5"]');
70
+ $link.handleLinkClick();
71
+ $link.trigger('click');
72
+
73
+ expect($container.data('current_page')).toEqual(5);
74
+ expect($('.selected_page[data-element_number="5"]').length).toEqual(1);
75
+ });
76
+
77
+ });
78
+
79
+ describe('failed ajax request', function(){
80
+
81
+ it('should not update the data attributes or link list given a failed ajax request', function(){
82
+ var d = $.Deferred();
83
+ d.reject();
84
+ spyOn(ajaxPagination,'sendPaginationRequest').and.returnValue(d.promise());
85
+
86
+ var $container = $('.pagination_container');
87
+
88
+ var $links_before_click = $('.pagination_link');
89
+ console.log($links_before_click);
90
+ var $current_page_before_click = $container.data('current_page');
91
+
92
+ var $link = $('.pagination_link[data-element_number="6"]');
93
+ $link.handleLinkClick();
94
+ $link.trigger('click');
95
+
96
+ var $links_after_click = $('.pagination_link');
97
+ var $current_page_after_click = $container.data('current_page');
98
+
99
+ for(x=0;x<=$links_after_click.length;x++){
100
+ var b = $links_before_click[x];
101
+ var a = $links_after_click[x];
102
+ expect(a).toEqual(b);
103
+ }
104
+ });
105
+
106
+ });
107
+
108
+ });
@@ -0,0 +1,129 @@
1
+ # src_files
2
+ #
3
+ # Return an array of filepaths relative to src_dir to include before jasmine specs.
4
+ # Default: []
5
+ #
6
+ # EXAMPLE:
7
+ #
8
+ # src_files:
9
+ # - lib/source1.js
10
+ # - lib/source2.js
11
+ # - dist/**/*.js
12
+ #
13
+ src_files:
14
+ - spec/javascripts/helpers/jquery.js
15
+ - spec/javascripts/helpers/jasmine_jquery.js
16
+ - spec/javascripts/helpers/jst_obj.js
17
+ - spec/javascripts/helpers/plugin.js
18
+ - lib/assets/pagination_ajax.js
19
+
20
+
21
+ # stylesheets
22
+ #
23
+ # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
24
+ # Default: []
25
+ #
26
+ # EXAMPLE:
27
+ #
28
+ # stylesheets:
29
+ # - css/style.css
30
+ # - stylesheets/*.css
31
+ #
32
+ stylesheets:
33
+ - stylesheets/**/*.css
34
+
35
+ # helpers
36
+ #
37
+ # Return an array of filepaths relative to spec_dir to include before jasmine specs.
38
+ # Default: ["helpers/**/*.js"]
39
+ #
40
+ # EXAMPLE:
41
+ #
42
+ # helpers:
43
+ # - helpers/**/*.js
44
+ #
45
+ helpers:
46
+ - 'helpers/**/*.js'
47
+
48
+ # spec_files
49
+ #
50
+ # Return an array of filepaths relative to spec_dir to include.
51
+ # Default: ["**/*[sS]pec.js"]
52
+ #
53
+ # EXAMPLE:
54
+ #
55
+ # spec_files:
56
+ # - **/*[sS]pec.js
57
+ #
58
+ spec_files:
59
+ - '**/*[sS]pec.js'
60
+
61
+ # src_dir
62
+ #
63
+ # Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
64
+ # Default: project root
65
+ #
66
+ # EXAMPLE:
67
+ #
68
+ # src_dir: public
69
+ #
70
+ src_dir:
71
+
72
+ # spec_dir
73
+ #
74
+ # Spec directory path. Your spec_files must be returned relative to this path.
75
+ # Default: spec/javascripts
76
+ #
77
+ # EXAMPLE:
78
+ #
79
+ # spec_dir: spec/javascripts
80
+ #
81
+ spec_dir:
82
+
83
+ # spec_helper
84
+ #
85
+ # Ruby file that Jasmine server will require before starting.
86
+ # Returned relative to your root path
87
+ # Default spec/javascripts/support/jasmine_helper.rb
88
+ #
89
+ # EXAMPLE:
90
+ #
91
+ # spec_helper: spec/javascripts/support/jasmine_helper.rb
92
+ #
93
+ spec_helper: spec/javascripts/support/jasmine_helper.rb
94
+
95
+ # boot_dir
96
+ #
97
+ # Boot directory path. Your boot_files must be returned relative to this path.
98
+ # Default: Built in boot file
99
+ #
100
+ # EXAMPLE:
101
+ #
102
+ # boot_dir: spec/javascripts/support/boot
103
+ #
104
+ boot_dir:
105
+
106
+ # boot_files
107
+ #
108
+ # Return an array of filepaths relative to boot_dir to include in order to boot Jasmine
109
+ # Default: Built in boot file
110
+ #
111
+ # EXAMPLE
112
+ #
113
+ # boot_files:
114
+ # - '**/*.js'
115
+ #
116
+ boot_files:
117
+
118
+ # rack_options
119
+ #
120
+ # Extra options to be passed to the rack server
121
+ # by default, Port and AccessLog are passed.
122
+ #
123
+ # This is an advanced options, and left empty by default
124
+ #
125
+ # EXAMPLE
126
+ #
127
+ # rack_options:
128
+ # server: 'thin'
129
+
@@ -0,0 +1,15 @@
1
+ #Use this file to set/override Jasmine configuration options
2
+ #You can remove it if you don't need it.
3
+ #This file is loaded *after* jasmine.yml is interpreted.
4
+ #
5
+ #Example: using a different boot file.
6
+ #Jasmine.configure do |config|
7
+ # config.boot_dir = '/absolute/path/to/boot_dir'
8
+ # config.boot_files = lambda { ['/absolute/path/to/boot_dir/file.js'] }
9
+ #end
10
+ #
11
+ #Example: prevent PhantomJS auto install, uses PhantomJS already on your path.
12
+ #Jasmine.configure do |config|
13
+ # config.prevent_phantomjs_auto_install = true
14
+ #end
15
+ #
@@ -0,0 +1,16 @@
1
+ describe('Rendering content to the page', function(){
2
+
3
+ it('should render the template to the view', function(){
4
+ loadFixtures('complete_pagination_dom.html');
5
+
6
+ var $canvas = $('.pagination_canvas');
7
+ var template = $('.pagination_container').data('template');
8
+ var response = {status: 'success',
9
+ data: [{"author":"Louie Mancini","comment":"here is a comment"},
10
+ {"author":"Robert Duval","comment":"The Godfather was the best movie ever"}]
11
+ }
12
+ ajaxPagination.buildDomElements($canvas, template, response);
13
+ expect($('.comment').length).toEqual(2);
14
+ });
15
+
16
+ });