Split files.
Showing
7 changed files
with
184 additions
and
1 deletions
... | @@ -11,7 +11,7 @@ module.exports = function(grunt) { | ... | @@ -11,7 +11,7 @@ module.exports = function(grunt) { |
11 | browserOptions: { | 11 | browserOptions: { |
12 | }, | 12 | }, |
13 | model: [ | 13 | model: [ |
14 | 'src/scripts/solr-search/model/**/*.js', | 14 | 'src/scripts/solr/model/**/*.js', |
15 | ], | 15 | ], |
16 | }; | 16 | }; |
17 | /* | 17 | /* | ... | ... |
This diff is collapsed.
Click to expand it.
src/scripts/solr/model/Ordering.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | var Backbone = require('backbone'); | ||
4 | |||
5 | var Ordering = Backbone.Model.extend({ | ||
6 | defaults: { | ||
7 | value: null, | ||
8 | items: new Backbone.Collection(), | ||
9 | }, | ||
10 | initialize: function(data, options) { | ||
11 | if (this.get('value') === null) { | ||
12 | var firstItem = this.get('items').at(0); | ||
13 | if (firstItem) { | ||
14 | this.set('value', firstItem.get('value')); | ||
15 | } | ||
16 | } | ||
17 | }, | ||
18 | parse: function(data) { | ||
19 | var result = _.clone(data); | ||
20 | if (result.items && !(result.items instanceof Backbone.Collection)) { | ||
21 | result.items = new Backbone.Collection(result.items, {parse: true}); | ||
22 | } | ||
23 | return result; | ||
24 | }, | ||
25 | }); | ||
26 | return Ordering; | ||
27 | }); |
src/scripts/solr/model/Pagination.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | var Backbone = require('backbone'); | ||
4 | |||
5 | function stepFalse(e) { | ||
6 | e.preventDefault(); | ||
7 | return false; | ||
8 | } | ||
9 | var Pagination = Backbone.Model.extend({ | ||
10 | defaults: { | ||
11 | currentPage: 1, | ||
12 | hasNext: false, | ||
13 | hasNextJumpPage: false, | ||
14 | hasPrevious: false, | ||
15 | hasPreviousJumpPage: false, | ||
16 | nextJumpPage: undefined, | ||
17 | nextPage: undefined, | ||
18 | pages: [], | ||
19 | pageJump: 5, | ||
20 | pageSize: 10, | ||
21 | previousJumpPage: undefined, | ||
22 | previousPage: undefined, | ||
23 | totalCount: 0, | ||
24 | totalPages: 0, | ||
25 | }, | ||
26 | initialize: function(data, options) { | ||
27 | var buildPages = function buildPages() { | ||
28 | var currentPage = parseInt(this.get('currentPage')); | ||
29 | var pageSize = parseInt(this.get('pageSize')); | ||
30 | var totalCount = parseInt(this.get('totalCount')); | ||
31 | if (!currentPage || !pageSize || !totalCount) { | ||
32 | return; | ||
33 | } | ||
34 | var pages = []; | ||
35 | var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize); | ||
36 | function addPage(self, i) { | ||
37 | pages.push({ | ||
38 | current: i === currentPage, | ||
39 | jump: function() { | ||
40 | self.set('currentPage', i); | ||
41 | return true; | ||
42 | }, | ||
43 | number: i, | ||
44 | }); | ||
45 | } | ||
46 | var startAt = currentPage - 4, endAt = currentPage + 5; | ||
47 | if (startAt < 1) { | ||
48 | endAt += (1 - startAt); | ||
49 | } | ||
50 | if (endAt > totalPages) { | ||
51 | startAt -= endAt - totalPages - 1; | ||
52 | endAt = totalPages + 1; | ||
53 | } | ||
54 | if (startAt < 1) { | ||
55 | startAt = 1; | ||
56 | } | ||
57 | if (endAt - startAt < 9) { | ||
58 | /* global console:false */ | ||
59 | console.log('foo'); | ||
60 | } | ||
61 | |||
62 | for (var i = startAt; i < endAt; i++) { | ||
63 | if (i > 0 && i <= totalPages) { | ||
64 | addPage(this, i); | ||
65 | } | ||
66 | } | ||
67 | var hasPrevious = currentPage > 1; | ||
68 | var hasNext = currentPage < totalPages; | ||
69 | var pageJump = this.get('pageJump'); | ||
70 | var nextJumpPage, previousJumpPage, hasNextJump, hasPreviousJump; | ||
71 | if (pageJump) { | ||
72 | nextJumpPage = currentPage + pageJump; | ||
73 | previousJumpPage = currentPage - pageJump; | ||
74 | hasNextJump = nextJumpPage < totalPages; | ||
75 | hasPreviousJump = previousJumpPage > 0; | ||
76 | } else { | ||
77 | hasNextJump = false; | ||
78 | hasPreviousJump = false; | ||
79 | } | ||
80 | this.set({ | ||
81 | hasNext: hasNext, | ||
82 | hasNextJump: hasNextJump, | ||
83 | hasPrevious: hasPrevious, | ||
84 | hasPreviousJump: hasPreviousJump, | ||
85 | nextJumpPage: hasNextJump ? nextJumpPage : undefined, | ||
86 | nextPage: hasNext ? currentPage + 1 : undefined, | ||
87 | pages: pages, | ||
88 | previousJumpPage: hasNextJump ? previousJumpPage : undefined, | ||
89 | previousPage: hasPrevious ? currentPage - 1 : undefined, | ||
90 | totalPages: totalPages, | ||
91 | }); | ||
92 | }; | ||
93 | this.on('change:pageSize change:currentPage change:totalCount', buildPages, this); | ||
94 | var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) { | ||
95 | this.on(eventName, function() { | ||
96 | var hasNext = this.get(hasNextName); | ||
97 | var hasPrevious = this.get(hasPreviousName); | ||
98 | var next, previous; | ||
99 | if (hasNext) { | ||
100 | next = _.bind(function(e) { | ||
101 | e.preventDefault(); | ||
102 | this.set('currentPage', this.get('currentPage') + pageCount); | ||
103 | return false; | ||
104 | }, this); | ||
105 | } else { | ||
106 | next = stepFalse; | ||
107 | } | ||
108 | if (hasPrevious) { | ||
109 | previous = _.bind(function(e) { | ||
110 | e.preventDefault(); | ||
111 | this.set('currentPage', this.get('currentPage') - pageCount); | ||
112 | return false; | ||
113 | }, this); | ||
114 | } else { | ||
115 | previous = stepFalse; | ||
116 | } | ||
117 | this.set(nextName, next); | ||
118 | this.set(previousName, previous); | ||
119 | }, this); | ||
120 | this[nextName] = _.bind(function() { | ||
121 | return this.get(nextName)(); | ||
122 | }, this); | ||
123 | this[previousName] = _.bind(function() { | ||
124 | return this.get(previousName)(); | ||
125 | }, this); | ||
126 | }, this); | ||
127 | this.on('change:pageJump', function() { | ||
128 | var pageJump = this.get('pageJump'); | ||
129 | installActions('change:hasNextJump change:hasPreviousJump', 'nextJump', 'hasNextJump', 'previousJump', 'hasPreviousJump', pageJump); | ||
130 | }, this); | ||
131 | installActions('change:hasNext change:hasPrevious', 'next', 'hasNext', 'previous', 'hasPrevious', 1); | ||
132 | buildPages.apply(this); | ||
133 | this.trigger('change:pageJump'); | ||
134 | return Pagination.__super__.initialize.apply(this, arguments); | ||
135 | } | ||
136 | }); | ||
137 | return Pagination; | ||
138 | }); |
src/scripts/solr/model/QueryTextField.js
0 → 100644
src/scripts/solr/model/Solr.js
0 → 100644
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment