Skip to content

Fix issue 386 #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/concepts/matestack/ui/core/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ const componentDef = {
},
computed: Vuex.mapState({
asyncTemplate: state => state.pageTemplate,
currentPathName: state => state.currentPathName,
currentSearch: state => state.currentSearch,
currentOrigin: state => state.currentOrigin,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused that we'd need this. Yes, I read your comment it seems weird to me that there should be no way to access that state throughout the history/event API etc. 🤔

And I mean I also tried it out (not with the back button though obviously 😅 ) and there I had it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmkay, seems more involved. Odd, I guess I have to read up on the history APIs etc. https://stackoverflow.com/questions/8980255/how-do-i-retrieve-if-the-popstate-event-comes-from-back-or-forward-actions-with

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a lot, but this was the only way to fix it.

}),
mounted: function(){
const self = this;
window.onpopstate = (event) => {
if (isNavigatingToAnotherPage(document.location, event)) {
this.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
};
if (isNavigatingToAnotherPage({
origin: self.currentOrigin,
pathName: self.currentPathName,
search: self.currentSearch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing, having all these "flat" in the store seemed weird to me before. This makes me feel it's even weirder. How about storing them in an object in the store? Like: currentLocation: {all_da_values} then that object could be passed on as a whole. It also makes it clearer that all these values belong together and are not of too much use on their own.

}, document.location)){
self.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
}
}
},
components: {
Expand Down
5 changes: 2 additions & 3 deletions app/concepts/matestack/ui/core/app/location.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const isNavigatingToAnotherPage = function(currentLocation, popstateEvent) {
const targetLocation = popstateEvent.target.location;
const isNavigatingToAnotherPage = function(currentLocation, targetLocation) {

// omits hash by design
return currentLocation.pathname !== targetLocation.pathname ||
return currentLocation.pathName !== targetLocation.pathname ||
currentLocation.origin !== targetLocation.origin ||
currentLocation.search !== targetLocation.search
}
Expand Down
12 changes: 8 additions & 4 deletions app/concepts/matestack/ui/core/app/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ Vue.use(Vuex)
const store = new Vuex.Store({
state: {
pageTemplate: null,
currentPath: document.location.pathname
currentPathName: document.location.pathname,
currentSearch: document.location.search,
currentOrigin: document.location.origin
},
mutations: {
setPageTemplate (state, serverResponse){
state.pageTemplate = serverResponse
},
setCurrentPath (state, path){
state.currentPath = path
setCurrentLocation (state, current){
state.currentPathName = current.path
state.currentSearch = current.search
state.currentOrigin = current.origin
}
},
actions: {
Expand Down Expand Up @@ -46,7 +50,7 @@ const store = new Vuex.Store({
setTimeout(function () {
resolve(response["data"])
commit('setPageTemplate', response["data"])
commit('setCurrentPath', url)
commit('setCurrentLocation', { path: url, search: document.location.search, origin: document.location.origin })
matestackEventHub.$emit("page_loaded", url);
if (typeof matestackUiCoreTransitionSuccess !== 'undefined') {
matestackUiCoreTransitionSuccess(url);
Expand Down
46 changes: 45 additions & 1 deletion spec/usage/components/transition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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

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

Expand Down Expand Up @@ -78,6 +78,10 @@ def page2
end
Rails.application.reload_routes!

end

it "Example 1 - Perform transition from one page to another without page reload if related to app" do

visit "/my_example_app/page1"

expect(page).to have_content("My Example App Layout")
Expand Down Expand Up @@ -113,6 +117,46 @@ def page2
expect(first_content_on_page_1).not_to eq(refreshed_content_on_page_1)
end

it "Example 2 - Perform transition from one page to another without page reload when using page history buttons" do

visit "/my_example_app/page1"

expect(page).to have_content("My Example App Layout")
expect(page).to have_button("Page 1")
expect(page).to have_button("Page 2")

expect(page).to have_content("This is Page 1")
expect(page).not_to have_content("This is Page 2")

element = page.find("#my-div-on-page-1")
first_content_on_page_1 = element.text

page.evaluate_script('document.body.classList.add("not-reloaded")')
expect(page).to have_selector("body.not-reloaded")

click_button("Page 2")

expect(page).to have_content("My Example App Layout")
expect(page).not_to have_content("This is Page 1")
expect(page).to have_content("This is Page 2")
expect(page).to have_selector("body.not-reloaded")

page.go_back

expect(page).to have_content("My Example App Layout")
expect(page).to have_content("This is Page 1")
expect(page).not_to have_content("This is Page 2")
expect(page).to have_selector("body.not-reloaded")
expect(page).to have_no_content(first_content_on_page_1)

page.go_forward

expect(page).to have_content("My Example App Layout")
expect(page).not_to have_content("This is Page 1")
expect(page).to have_content("This is Page 2")
expect(page).to have_selector("body.not-reloaded")
end

# supposed to work, but doesn't. Suspect Vue is the culprint here
# it "Example 2 - Perform transition from one page to another by providing route as string (not recommended)" do
#
Expand Down
16 changes: 10 additions & 6 deletions vendor/assets/javascripts/dist/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
"entrypoints": {
"matestack-ui-core": {
"css": [
"/dist/matestack-ui-core.min.css"
"/dist/matestack-ui-core.css"
],
"js": [
"/dist/matestack-ui-core.min.js"
"/dist/matestack-ui-core.js"
],
"css.map": [
"/dist/matestack-ui-core.css.map"
],
"js.map": [
"/dist/matestack-ui-core.min.js.map"
"/dist/matestack-ui-core.js.map"
]
}
},
"matestack-ui-core.css": "/dist/matestack-ui-core.min.css",
"matestack-ui-core.js": "/dist/matestack-ui-core.min.js",
"matestack-ui-core.js.map": "/dist/matestack-ui-core.min.js.map"
"matestack-ui-core.css": "/dist/matestack-ui-core.css",
"matestack-ui-core.css.map": "/dist/matestack-ui-core.css.map",
"matestack-ui-core.js": "/dist/matestack-ui-core.js",
"matestack-ui-core.js.map": "/dist/matestack-ui-core.js.map"
}
31 changes: 21 additions & 10 deletions vendor/assets/javascripts/dist/matestack-ui-core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/assets/javascripts/dist/matestack-ui-core.js.map

Large diffs are not rendered by default.