This repository was archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathaf.actionsheet.js
More file actions
136 lines (127 loc) · 4.53 KB
/
af.actionsheet.js
File metadata and controls
136 lines (127 loc) · 4.53 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
/**
* af.actionsheet - an actionsheet for html5 mobile apps
* Copyright 2014 - Intel
*/
/* EXAMPLE
You can pass in an HTML string that will get rendered
$(document.body).actionsheet('<a >Back</a><a onclick="alert(\'hi\');" >Show Alert 3</a><a onclick="alert(\'goodbye\');">Show Alert 4</a>');
You can also use an array of objects to show each item. There are three properties
text - the text to display
cssClasses - extra css classes
handler - click handler function
$(document.body).actionsheet(
[{
text: 'back',
cssClasses: 'red',
handler: function () {
$.ui.goBack();
}
}, {
text: 'show alert 5',
cssClasses: 'blue',
handler: function () {
alert("hi");
}
}, {
text: 'show alert 6',
cssClasses: '',
handler: function () {
alert("goodbye");
}
}]
);
*/
(function($) {
"use strict";
$.fn.actionsheet = function(opts) {
var tmp;
for (var i = 0; i < this.length; i++) {
tmp = new actionsheet(this[i], opts);
}
return this.length === 1 ? tmp : this;
};
var actionsheet = function(elID, opts) {
if (typeof elID === "string" || elID instanceof String) {
this.el = document.getElementById(elID);
} else {
this.el = elID;
}
if (!this.el) {
window.alert("Could not find element for actionsheet " + elID);
return;
}
if (this instanceof actionsheet) {
if (typeof(opts) === "object") {
for (var j in opts) {
this[j] = opts[j];
}
}
} else {
return new actionsheet(elID, opts);
}
var markStart = "<div id='af_actionsheet'><div style='width:100%'>";
var markEnd = "</div></div>";
var markup;
var noop=function(){};
if (typeof opts === "string") {
markup = $(markStart + opts + "<a href='javascript:;' class='cancel'>Cancel</a>" + markEnd);
} else if (typeof opts === "object") {
markup = $(markStart + markEnd);
var container = $(markup.children().get(0));
opts.push({
text: "Cancel",
cssClasses: "cancel"
});
for (var i = 0; i < opts.length; i++) {
var item = $("<a href='javascript:;'>" + (opts[i].text || "TEXT NOT ENTERED") + "</a>");
item[0].onclick = (opts[i].handler || noop);
if (opts[i].cssClasses && opts[i].cssClasses.length > 0)
item.addClass(opts[i].cssClasses);
container.append(item);
}
}
$(elID).find("#af_actionsheet").remove();
$(elID).find("#af_action_mask").remove();
$(elID).append(markup);
markup.vendorCss("Transition", "all 0ms");
this.el.style.overflow = "hidden";
markup.on("click", "a", this.sheetClickHandler.bind(this));
this.activeSheet = markup;
markup.cssTranslate("0," + ((markup.height())) + "px");
$(elID).append("<div id='af_action_mask' style='position:absolute;top:0px;left:0px;right:0px;bottom:0px;z-index:9998;background:rgba(0,0,0,.4)'/>");
setTimeout(function() {
markup.vendorCss("Transition", "all 300ms");
markup.cssTranslate("0,0");
}, 10);
$("#af_action_mask").bind("touchstart touchmove touchend click",function(e){
e.preventDefault();
e.stopPropagation();
});
};
actionsheet.prototype = {
activeSheet: null,
sheetClickHandler:function(){
this.hideSheet();
return false;
},
hideSheet: function() {
this.activeSheet.off("click", "a", this.sheetClickHandler);
$(this.el).find("#af_action_mask").unbind("click").remove();
this.activeSheet.vendorCss("Transition", "all 0ms");
var markup = this.activeSheet;
var theEl = this.el;
setTimeout(function() {
markup.vendorCss("Transition", "all 300ms");
markup.cssTranslate("0,"+(markup.height()+60)+"px");
setTimeout(function() {
markup.remove();
markup = null;
theEl.style.overflow = "none";
}, 350);
}, 10);
}
};
$.afui.actionsheet=function(opts){
return $(document.body).actionsheet(opts);
};
})(jQuery);