Skip to content

Commit 24ba6fb

Browse files
authored
Merge pull request #98 from basemate/implementing_issue_59
Implementing issue 59 - Collection Component
2 parents ea297e3 + 2e6dc44 commit 24ba6fb

File tree

35 files changed

+2962
-367
lines changed

35 files changed

+2962
-367
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import Vue from 'vue/dist/vue.esm'
2+
3+
import matestackEventHub from 'js/event-hub'
4+
import queryParamsHelper from 'js/helpers/query-params-helper'
5+
6+
import componentMixin from 'component/component'
7+
import asyncMixin from 'async/async'
8+
9+
const componentDef = {
10+
mixins: [componentMixin, asyncMixin],
11+
data: function(){
12+
return {
13+
currentLimit: null,
14+
currentOffset: null,
15+
currentFilteredCount: null,
16+
currentBaseCount: null
17+
}
18+
},
19+
methods: {
20+
next: function(){
21+
if (this.currentTo() < this.currentCount()){
22+
this.currentOffset += this.currentLimit
23+
var url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-offset", this.currentOffset)
24+
window.history.pushState({matestackApp: true, url: url}, null, url);
25+
matestackEventHub.$emit(this.componentConfig["id"] + "-update")
26+
}
27+
},
28+
previous: function(){
29+
if ((this.currentOffset - this.currentLimit)*-1 != this.currentLimit){
30+
if((this.currentOffset - this.currentLimit) < 0){
31+
this.currentOffset = 0
32+
} else {
33+
this.currentOffset -= this.currentLimit
34+
}
35+
var url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-offset", this.currentOffset)
36+
window.history.pushState({matestackApp: true, url: url}, null, url);
37+
matestackEventHub.$emit(this.componentConfig["id"] + "-update")
38+
}
39+
},
40+
currentTo: function(){
41+
var to = parseInt(this.currentOffset) + parseInt(this.currentLimit)
42+
if (to > parseInt(this.currentCount())){
43+
return this.currentCount();
44+
} else {
45+
return to;
46+
}
47+
},
48+
currentCount: function(){
49+
if (this.currentFilteredCount != null || this.currentFilteredCount != undefined){
50+
return this.currentFilteredCount;
51+
} else {
52+
return this.currentBaseCount;
53+
}
54+
},
55+
goToPage: function(page){
56+
this.currentOffset = parseInt(this.currentLimit) * (parseInt(page)-1)
57+
var url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-offset", this.currentOffset)
58+
window.history.pushState({matestackApp: true, url: url}, null, url);
59+
matestackEventHub.$emit(this.componentConfig["id"] + "-update")
60+
}
61+
},
62+
mounted: function(){
63+
if(queryParamsHelper.getQueryParam(this.componentConfig["id"] + "-offset") != null){
64+
this.currentOffset = parseInt(queryParamsHelper.getQueryParam(this.componentConfig["id"] + "-offset"))
65+
} else {
66+
if(this.componentConfig["init_offset"] != undefined){
67+
this.currentOffset = this.componentConfig["init_offset"]
68+
} else {
69+
this.currentOffset = 0
70+
}
71+
}
72+
73+
if(queryParamsHelper.getQueryParam(this.componentConfig["id"] + "-limit") != null){
74+
this.currentOffset = parseInt(queryParamsHelper.getQueryParam(this.componentConfig["id"] + "-limit"))
75+
} else {
76+
if(this.componentConfig["init_limit"] != undefined){
77+
this.currentLimit = this.componentConfig["init_limit"]
78+
} else {
79+
this.currentLimit = 10
80+
}
81+
}
82+
83+
if(this.componentConfig["filtered_count"] != undefined){
84+
this.currentFilteredCount = this.componentConfig["filtered_count"]
85+
if(this.currentOffset >= this.currentFilteredCount){
86+
this.previous()
87+
}
88+
}
89+
if(this.componentConfig["base_count"] != undefined){
90+
this.currentBaseCount = this.componentConfig["base_count"]
91+
if(this.currentOffset >= this.currentBaseCount){
92+
this.previous()
93+
}
94+
}
95+
}
96+
}
97+
98+
let component = Vue.component('matestack-ui-core-collection-content', componentDef)
99+
100+
export default componentDef
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Matestack::Ui::Core::Collection::Content
2+
class Content < Matestack::Ui::Core::Component::Dynamic
3+
4+
def setup
5+
@rerender = true
6+
@component_config = @component_config.except(:data, :paginated_data)
7+
end
8+
9+
def response
10+
components {
11+
div do
12+
yield_components
13+
end
14+
}
15+
end
16+
17+
end
18+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%a{@tag_attributes, "@click": "next()"}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Collection::Content::Next
2+
class Next < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%a{@tag_attributes, "@click": "goToPage(#{@options[:page]})"}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Collection::Content::Page::Link
2+
class Link < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%a{@tag_attributes, "@click": "previous()"}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Collection::Content::Previous
2+
class Previous < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Vue from 'vue/dist/vue.esm'
2+
3+
import matestackEventHub from 'js/event-hub'
4+
import queryParamsHelper from 'js/helpers/query-params-helper'
5+
6+
import componentMixin from 'component/component'
7+
8+
const componentDef = {
9+
mixins: [componentMixin],
10+
data: function(){
11+
return {
12+
filter: {}
13+
}
14+
},
15+
methods: {
16+
submitFilter: function(){
17+
var url;
18+
var filter = this.filter
19+
for (var key in this.filter) {
20+
url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-filter-" + key, this.filter[key], url)
21+
}
22+
url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-offset", 0, url)
23+
window.history.pushState({matestackApp: true, url: url}, null, url);
24+
matestackEventHub.$emit(this.componentConfig["id"] + "-update")
25+
},
26+
resetFilter: function(){
27+
var url;
28+
for (var key in this.filter) {
29+
url = queryParamsHelper.updateQueryParams(this.componentConfig["id"] + "-filter-" + key, null, url)
30+
this.filter[key] = null;
31+
this.$forceUpdate();
32+
}
33+
window.history.pushState({matestackApp: true, url: url}, null, url);
34+
matestackEventHub.$emit(this.componentConfig["id"] + "-update")
35+
}
36+
},
37+
created: function(){
38+
var self = this;
39+
var queryParamsObject = queryParamsHelper.queryParamsToObject()
40+
Object.keys(queryParamsObject).forEach(function(key){
41+
if (key.startsWith(self.componentConfig["id"] + "-filter-")){
42+
self.filter[key.replace(self.componentConfig["id"] + "-filter-", "")] = queryParamsObject[key]
43+
}
44+
})
45+
}
46+
}
47+
48+
let component = Vue.component('matestack-ui-core-collection-filter', componentDef)
49+
50+
export default componentDef
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Matestack::Ui::Core::Collection::Filter
2+
class Filter < Matestack::Ui::Core::Component::Dynamic
3+
4+
def setup
5+
@component_config = @component_config.except(:data, :paginated_data)
6+
end
7+
8+
def response
9+
components {
10+
div do
11+
yield_components
12+
end
13+
}
14+
end
15+
16+
end
17+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- if [:text, :number, :email, :date, :password].include?(options[:type])
2+
%input{"v-model#{'.number' if options[:type] == :number}": input_key,
3+
type: options[:type],
4+
class: options[:class],
5+
id: component_id,
6+
"@keyup.enter": "submitFilter()",
7+
ref: "filter.#{attr_key}",
8+
placeholder: options[:placeholder]}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Matestack::Ui::Core::Collection::Filter::Input
2+
class Input < Matestack::Ui::Core::Component::Static
3+
4+
def input_key
5+
'filter["' + options[:key].to_s + '"]'
6+
end
7+
8+
def attr_key
9+
return options[:key].to_s
10+
end
11+
12+
end
13+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%a{@tag_attributes, "@click": "resetFilter()"}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Collection::Filter::Reset
2+
class Reset < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%a{@tag_attributes, "@click": "submitFilter()"}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Collection::Filter::Submit
2+
class Submit < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
module Matestack::Ui::Core::Collection
2+
3+
CollectionConfig = Struct.new(:id, :init_offset, :init_limit, :filtered_count, :base_count, :data, :context) do
4+
5+
def paginated_data
6+
resulting_data = data
7+
resulting_data = resulting_data.offset(get_collection_offset) unless get_collection_offset == 0
8+
resulting_data = resulting_data.limit(get_collection_limit) unless get_collection_limit == 0
9+
10+
return resulting_data
11+
end
12+
13+
def get_collection_offset
14+
(context[:params]["#{id}-offset".to_sym] ||= init_offset).to_i
15+
end
16+
17+
def get_collection_limit
18+
(context[:params]["#{id}-limit".to_sym] ||= init_limit).to_i
19+
end
20+
21+
def pages
22+
offset = get_collection_offset
23+
limit = get_collection_limit
24+
if filtered_count.present?
25+
count = filtered_count
26+
else
27+
count = base_count
28+
end
29+
page_count = count/limit
30+
page_count += 1 if count%limit > 0
31+
return (1..page_count).to_a
32+
end
33+
34+
def from
35+
return get_collection_offset + 1 if to > 0
36+
return 0 if to == 0
37+
end
38+
39+
def to
40+
current_to = get_collection_offset + get_collection_limit
41+
if filtered_count.present?
42+
if current_to > filtered_count
43+
return filtered_count
44+
else
45+
return current_to
46+
end
47+
else
48+
if current_to > base_count
49+
return base_count
50+
else
51+
return current_to
52+
end
53+
end
54+
end
55+
56+
def config
57+
self.to_h.except(:context)
58+
end
59+
60+
end
61+
62+
63+
module Helper
64+
65+
def get_collection_filter collection_id, key=nil
66+
filter_hash = {}
67+
context[:params].each do |param_key, param_value|
68+
if param_key.start_with?("#{collection_id}-filter-")
69+
param_key.gsub("#{collection_id}-filter-", "")
70+
filter_hash[param_key.gsub("#{collection_id}-filter-", "").to_sym] = param_value
71+
end
72+
end
73+
if key.nil?
74+
return filter_hash
75+
else
76+
return filter_hash[key]
77+
end
78+
end
79+
80+
def get_collection_order collection_id, key=nil
81+
order_hash = {}
82+
context[:params].each do |param_key, param_value|
83+
if param_key.start_with?("#{collection_id}-order-")
84+
param_key.gsub("#{collection_id}-order-", "")
85+
order_hash[param_key.gsub("#{collection_id}-order-", "").to_sym] = param_value
86+
end
87+
end
88+
if key.nil?
89+
return order_hash
90+
else
91+
return order_hash[key]
92+
end
93+
end
94+
95+
def set_collection id: nil, init_offset: 0, init_limit: nil, base_count: nil, filtered_count: nil, data: nil
96+
@collections = {} if @collections.nil?
97+
98+
collection_config = CollectionConfig.new(
99+
id,
100+
init_offset,
101+
init_limit,
102+
filtered_count,
103+
base_count,
104+
data,
105+
context
106+
)
107+
108+
@collections[id.to_sym] = collection_config
109+
110+
return collection_config
111+
end
112+
113+
end
114+
end

0 commit comments

Comments
 (0)