-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmf_mobile-switch.js
More file actions
144 lines (125 loc) · 4.29 KB
/
Copy pathmf_mobile-switch.js
File metadata and controls
144 lines (125 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/************************************************************
Failsafe function-container
*************************************************************/
;(function($, window, document, undefined) {
/************************************************************
Strict Mode
@see http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more
*************************************************************/
'use strict';
/************************************************************
@description create, read, erase -Cookies
@see http://www.quirksmode.org/js/cookies.html
*************************************************************/
var Cookie = {
createCookie: function(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
},
readCookie: function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
}
return null;
},
eraseCookie: function(name) {
this.createCookie(name,'',-1);
}
};
/************************************************************
@description
data-website-type=""
data-breakpoint=""
data-msg=""
*************************************************************/
var SwitchToMobile = {
cacheElements: function() {
this.$switch_link = $('.switch-link');
},
init: function() {
// cache Elements
this.cacheElements();
// setup Defaults
this.breakpoint = this.$switch_link.data('breakpoint');
this.msg = this.$switch_link.data('msg');
this.url = this.$switch_link.attr('href');
this.viewportWidth = $(window).width();
this.website_type = this.$switch_link.data('website-type');
this.GET = this.getGET();
// set choice (user changes from mobile to desktop)
if (this.GET.mf_mobile_switch) {
this.setChoice(this.GET.mf_mobile_switch);
}
// get last remembered status
this.getChoice();
// bind Events
this.bindEvents();
// check for website type
this.checkWebsiteType();
},
bindEvents: function() {
this.$switch_link.on('click', function(event) {
event.preventDefault();
var web_type;
if (SwitchToMobile.website_type === 'desktop') {
web_type = 'mobile';
} else {
web_type = 'desktop';
}
// todo: save current sites choice
SwitchToMobile.setChoice(web_type);
// go to mobile website
window.location = SwitchToMobile.url + '?mf_mobile_switch=' + web_type;
});
},
getGET: function() {
if(window.location.search) {
// http://stackoverflow.com/questions/439463/how-to-get-get-and-post-variables-with-jquery
var qs = document.location.search.split("+").join(" "),
params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
} else {
return false;
}
},
getChoice: function() {
var lastchoice = Cookie.readCookie('mf_mobile-switch');
if(lastchoice) {
this.choice = lastchoice;
} else {
this.choice = undefined;
}
},
setChoice: function(web_type) {
Cookie.createCookie('mf_mobile-switch', web_type, 999);
},
checkWebsiteType: function() {
if((this.viewportWidth <= this.breakpoint) && (this.choice === undefined) && (this.website_type !== 'mobile')) {
var choice = confirm(this.msg);
if(choice) {
this.setChoice('mobile');
window.location = this.url;
} else {
this.setChoice('desktop');
}
} else if ((this.choice === 'mobile') && (this.website_type === 'desktop')) {
window.location = this.url;
}
}
};
SwitchToMobile.init();
})(jQuery, window, document);