forked from Esri/Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauthsample.html
173 lines (148 loc) · 4.88 KB
/
oauthsample.html
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
165
166
167
168
169
170
171
172
173
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
<title>ArcGIS Online Items</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.27compact/dijit/themes/claro/claro.css">
<style>
html, body {
font-family: Lucida Sans, Lucida Grande, Arial !important;
font-size: 14px;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
.esri-item-gallery .esri-item-container {
float: left;
text-align: center;
padding: 10px;
width: 204px;
display: inline-block;
}
.esri-item-gallery .esri-image {
width: 200px;
height: 133px;
border: 2px solid gray;
border-radius: 5px;
}
.esri-item-gallery .esri-null-image {
line-height: 133px;
text-align: center;
color: #999999;
}
.esri-item-gallery .esri-title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.esri-item-gallery .esri-null-title {
color: #999999;
}
.action {
color: blue;
cursor: pointer;
text-decoration: underline;
}
</style>
<script src="https://js.arcgis.com/3.27compact/"></script>
<script>
require([
"esri/arcgis/Portal", "esri/arcgis/OAuthInfo", "esri/IdentityManager",
"dojo/dom-style", "dojo/dom-attr", "dojo/dom", "dojo/on", "dojo/_base/array",
"dojo/domReady!"
], function (arcgisPortal, OAuthInfo, esriId,
domStyle, domAttr, dom, on, arrayUtils){
var info = new OAuthInfo({
appId: "aH05lzpSnmNF1uXe",
// Uncomment the next line and update if using your own portal
// portalUrl: "https://<host>:<port>/arcgis",
// Uncomment the next line to prevent the user's signed in state from being shared
// with other apps on the same domain with the same authNamespace value.
//authNamespace: "portal_oauth_inline",
popup: false
});
esriId.registerOAuthInfos([info]);
esriId.checkSignInStatus(info.portalUrl + "/sharing").then(
function (){
displayItems();
}
).otherwise(
function (){
// Anonymous view
domStyle.set("anonymousPanel", "display", "block");
domStyle.set("personalizedPanel", "display", "none");
}
);
on(dom.byId("sign-in"), "click", function (){
console.log("click", arguments);
// user will be redirected to OAuth Sign In page
esriId.getCredential(info.portalUrl + "/sharing");
});
on(dom.byId("sign-out"), "click", function (){
esriId.destroyCredentials();
window.location.reload();
});
function displayItems(){
new arcgisPortal.Portal(info.portalUrl).signIn().then(
function (portalUser){
console.log("Signed in to the portal: ", portalUser);
domAttr.set("userId", "innerHTML", portalUser.fullName);
domStyle.set("anonymousPanel", "display", "none");
domStyle.set("personalizedPanel", "display", "block");
queryPortal(portalUser);
}
).otherwise(
function (error){
console.log("Error occurred while signing in: ", error);
}
);
}
function queryPortal(portalUser){
var portal = portalUser.portal;
//See list of valid item types here: http://www.arcgis.com/apidocs/rest/index.html?itemtypes.html
//See search reference here: http://www.arcgis.com/apidocs/rest/index.html?searchreference.html
var queryParams = {
q: "owner:" + portalUser.username,
sortField: "numViews",
sortOrder: "desc",
num: 20
};
portal.queryItems(queryParams).then(createGallery);
}
function createGallery(items){
var htmlFragment = "";
arrayUtils.forEach(items.results, function (item){
htmlFragment += (
"<div class=\"esri-item-container\">" +
(
item.thumbnailUrl ?
"<div class=\"esri-image\" style=\"background-image:url(" + item.thumbnailUrl + ");\"></div>" :
"<div class=\"esri-image esri-null-image\">Thumbnail not available</div>"
) +
(
item.title ?
"<div class=\"esri-title\">" + (item.title || "") + "</div>" :
"<div class=\"esri-title esri-null-title\">Title not available</div>"
) +
"</div>"
);
});
dom.byId("itemGallery").innerHTML = htmlFragment;
}
});
</script>
</head>
<body class="claro">
<div id="anonymousPanel" style="display: none; padding: 5px; text-align: center;">
<span id="sign-in" class="action">Sign In</span> and view your ArcGIS Online items.
</div>
<div id="personalizedPanel" style="display: none; padding: 5px; text-align: center;">
Welcome <span id="userId" style="font-weight: bold;"></span>
-
<span id="sign-out" class="action">Sign Out</span>
</div>
<div id="itemGallery" class="esri-item-gallery" style="width: 100%;"></div>
</body>
</html>