-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·165 lines (128 loc) · 5.94 KB
/
main.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var format = {
//take the content of each descriptor div, and put it into a data-role=page format. simple reformatting.
pages: function() {
var content = $("[data-page]");
var pages = content.length;
var pages_to_append = "";
for (var i = 0; i < pages; i++) {
//make page
pages_to_append += "<div data-role='page' id='" + content.eq(i).attr("data-page") + "'>";
//put in header and back button
pages_to_append += "<div data-role='header' data-theme='b'>" + '<a data-rel="back" data-iconpos="notext" data-icon="back">Back</a>';
//title
pages_to_append += "<h1>Beginner's Guide to Dental Implant Prosthodontics</h1>";
//navigator
pages_to_append += '<select name="nav" id="nav" data-icon="menu" class="ui-btn-right" data-mini="true" data-iconpos="notext" data-theme="b"><option value=""></option><option value="index.php">Procedures</option><option value="appendix.php">Appendix</option></select></div>';
//content
pages_to_append += "<div data-role='content' id='content" + content.eq(i).attr("data-page") + "'>";
pages_to_append += content.eq(i).html();
pages_to_append += "</div></div>";
}
return pages_to_append;
},
//each list item must be linked to a data-role page as per jquery mobile specs
linkPages: function() {
// for non accordion, get the anchor, and the find the closest page to it. append that pages number to its href
$("h3").find(".goToPage").each(function() {
var $this = $(this);
var pair = $this.closest("h3").next(".descriptor").attr("data-page");
$this.attr("href","#" + pair);
//console.log(pair.substring(pair.length -1 , pair.length));
//$($this.text()).appendTo($("#contentpage" + pair.substring(pair.length -1 , pair.length)));
});
//same as above, except with accordion
$("li").find(".goToPage").each(function() {
var $this = $(this);
var pair = $this.closest("li").next(".descriptor").attr("data-page");
$this.attr("href","#" + pair);
$($this.text()).prependTo($("#" + pair).find("[data-role=content]"));
// for the last list item, the descriptor is appended after, so the lookup is slightly different
if ($this.closest("li").hasClass("last-li")){
pair = $this.closest("[data-role=collapsible]").next(".descriptor").attr("data-page");
$this.attr("href","#" + pair);
$($this.text()).prependTo($("#" + pair).find("[data-role=content]"));
}
});
//finally, remove the descriptor elements.
$(".relative.descriptor").each(function() {
$(this).remove();
});
},
//in order for images to display in the cms, their path began with ../. this removes that. either way, the path had to be modified.
};
//first line lets things work on first page load, no need for refresh.
$("#main").off('pagecreate').live('pagecreate', function (event) {
//stops media from being reloaded constantly, greatly improves performance
$.mobile.page.prototype.options.domCache = true;
//make the separate page views for content
$(format.pages()).appendTo($("body"));
//link each list item to the next content item
format.linkPages();
//the list of conditions on the procedures page goes to that condition's specific page.
$("[name=condition]").bind("click",function() {
$(this).closest("form").submit();
return false;
});
//the top right select menu with procedures and appendix.
$("#nav").live("change",function() {
var page = $(this).val();
window.location = page;
return false;
});
//goes to ajax call, submits step2.php, the condition name (edentulism in the first case), and the id of the subsection of edentulism
$("[name=case]").bind("click",function() {
var fieldset = $(this).closest("fieldset");
makeRequest(fieldset.attr("data-submission"),fieldset.attr("data-condition"), $(this).attr("id"));
return false;
});
//the button formatting given by the designer did not allow input type = submit for some reason. Workaround
$("#continue").die("tap").live("tap",function() {
$(this).closest("form").submit();
return false;
});
//Thanks to MDN for all the browser specifics
function makeRequest(url, condition,id) {
//make sure ajax works in all browsers...mdn
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
//change the page state
httpRequest.onreadystatechange = alertContents;
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//send the data, which is interpreted by php. no matter what, the form is sent, even if incomplete. In this case,
//php decides what to do with the info. If incomplete, it does nothing.
httpRequest.send("&case=" + encodeURIComponent(id) + "&condition=" + encodeURIComponent(condition));
}
//insert response text into ajaxcontent box, and show the continue button, cause now we're ready to GO!
function alertContents() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
//show the ajax box with the html from step 3 in it. Trigger create is used to apply the jqmobile styles to the returned html
$(".ajaxContent").show().html(httpRequest.responseText).fadeIn("fast").trigger("create") ;
$("#continue").show();
} else {
alert('There was a problem with the request.');
}
}
}
catch( e ) {
alert('Caught Exception: ' + e.description);
}
}
});