Skip to content

Commit 6586691

Browse files
authored
Merge pull request #395 from matestack/fix_issue_386
Fix issue 386
2 parents 852c29a + 2c3b6f1 commit 6586691

File tree

7 files changed

+98
-28
lines changed

7 files changed

+98
-28
lines changed

app/concepts/matestack/ui/core/app/app.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ const componentDef = {
1010
},
1111
computed: Vuex.mapState({
1212
asyncTemplate: state => state.pageTemplate,
13+
currentPathName: state => state.currentPathName,
14+
currentSearch: state => state.currentSearch,
15+
currentOrigin: state => state.currentOrigin,
1316
}),
1417
mounted: function(){
18+
const self = this;
1519
window.onpopstate = (event) => {
16-
if (isNavigatingToAnotherPage(document.location, event)) {
17-
this.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
18-
};
20+
if (isNavigatingToAnotherPage({
21+
origin: self.currentOrigin,
22+
pathName: self.currentPathName,
23+
search: self.currentSearch
24+
}, document.location)){
25+
self.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
26+
}
1927
}
2028
},
2129
components: {

app/concepts/matestack/ui/core/app/location.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const isNavigatingToAnotherPage = function(currentLocation, popstateEvent) {
2-
const targetLocation = popstateEvent.target.location;
1+
const isNavigatingToAnotherPage = function(currentLocation, targetLocation) {
32

43
// omits hash by design
5-
return currentLocation.pathname !== targetLocation.pathname ||
4+
return currentLocation.pathName !== targetLocation.pathname ||
65
currentLocation.origin !== targetLocation.origin ||
76
currentLocation.search !== targetLocation.search
87
}

app/concepts/matestack/ui/core/app/store.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ Vue.use(Vuex)
88
const store = new Vuex.Store({
99
state: {
1010
pageTemplate: null,
11-
currentPath: document.location.pathname
11+
currentPathName: document.location.pathname,
12+
currentSearch: document.location.search,
13+
currentOrigin: document.location.origin
1214
},
1315
mutations: {
1416
setPageTemplate (state, serverResponse){
1517
state.pageTemplate = serverResponse
1618
},
17-
setCurrentPath (state, path){
18-
state.currentPath = path
19+
setCurrentLocation (state, current){
20+
state.currentPathName = current.path
21+
state.currentSearch = current.search
22+
state.currentOrigin = current.origin
1923
}
2024
},
2125
actions: {
@@ -46,7 +50,7 @@ const store = new Vuex.Store({
4650
setTimeout(function () {
4751
resolve(response["data"])
4852
commit('setPageTemplate', response["data"])
49-
commit('setCurrentPath', url)
53+
commit('setCurrentLocation', { path: url, search: document.location.search, origin: document.location.origin })
5054
matestackEventHub.$emit("page_loaded", url);
5155
if (typeof matestackUiCoreTransitionSuccess !== 'undefined') {
5256
matestackUiCoreTransitionSuccess(url);

spec/usage/components/transition_spec.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
describe "Transition Component", type: :feature, js: true do
55

6-
it "Example 1 - Perform transition from one page to another without page reload if related to app" do
6+
before :all do
77

88
class Apps::ExampleApp < Matestack::Ui::App
99

@@ -78,6 +78,10 @@ def page2
7878
end
7979
Rails.application.reload_routes!
8080

81+
end
82+
83+
it "Example 1 - Perform transition from one page to another without page reload if related to app" do
84+
8185
visit "/my_example_app/page1"
8286

8387
expect(page).to have_content("My Example App Layout")
@@ -113,6 +117,46 @@ def page2
113117
expect(first_content_on_page_1).not_to eq(refreshed_content_on_page_1)
114118
end
115119

120+
it "Example 2 - Perform transition from one page to another without page reload when using page history buttons" do
121+
122+
visit "/my_example_app/page1"
123+
124+
expect(page).to have_content("My Example App Layout")
125+
expect(page).to have_button("Page 1")
126+
expect(page).to have_button("Page 2")
127+
128+
expect(page).to have_content("This is Page 1")
129+
expect(page).not_to have_content("This is Page 2")
130+
131+
element = page.find("#my-div-on-page-1")
132+
first_content_on_page_1 = element.text
133+
134+
page.evaluate_script('document.body.classList.add("not-reloaded")')
135+
expect(page).to have_selector("body.not-reloaded")
136+
137+
click_button("Page 2")
138+
139+
expect(page).to have_content("My Example App Layout")
140+
expect(page).not_to have_content("This is Page 1")
141+
expect(page).to have_content("This is Page 2")
142+
expect(page).to have_selector("body.not-reloaded")
143+
144+
page.go_back
145+
146+
expect(page).to have_content("My Example App Layout")
147+
expect(page).to have_content("This is Page 1")
148+
expect(page).not_to have_content("This is Page 2")
149+
expect(page).to have_selector("body.not-reloaded")
150+
expect(page).to have_no_content(first_content_on_page_1)
151+
152+
page.go_forward
153+
154+
expect(page).to have_content("My Example App Layout")
155+
expect(page).not_to have_content("This is Page 1")
156+
expect(page).to have_content("This is Page 2")
157+
expect(page).to have_selector("body.not-reloaded")
158+
end
159+
116160
# supposed to work, but doesn't. Suspect Vue is the culprint here
117161
# it "Example 2 - Perform transition from one page to another by providing route as string (not recommended)" do
118162
#

vendor/assets/javascripts/dist/manifest.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"entrypoints": {
33
"matestack-ui-core": {
44
"css": [
5-
"/dist/matestack-ui-core.min.css"
5+
"/dist/matestack-ui-core.css"
66
],
77
"js": [
8-
"/dist/matestack-ui-core.min.js"
8+
"/dist/matestack-ui-core.js"
9+
],
10+
"css.map": [
11+
"/dist/matestack-ui-core.css.map"
912
],
1013
"js.map": [
11-
"/dist/matestack-ui-core.min.js.map"
14+
"/dist/matestack-ui-core.js.map"
1215
]
1316
}
1417
},
15-
"matestack-ui-core.css": "/dist/matestack-ui-core.min.css",
16-
"matestack-ui-core.js": "/dist/matestack-ui-core.min.js",
17-
"matestack-ui-core.js.map": "/dist/matestack-ui-core.min.js.map"
18+
"matestack-ui-core.css": "/dist/matestack-ui-core.css",
19+
"matestack-ui-core.css.map": "/dist/matestack-ui-core.css.map",
20+
"matestack-ui-core.js": "/dist/matestack-ui-core.js",
21+
"matestack-ui-core.js.map": "/dist/matestack-ui-core.js.map"
1822
}

vendor/assets/javascripts/dist/matestack-ui-core.js

Lines changed: 21 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/dist/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)