Skip to content

Commit c1de052

Browse files
Merge pull request #204 from TakayukiHoshi1984/bugfix_missing_uri
不具合修正: POST用のUIが表示されない
2 parents 0ee61c9 + de93c72 commit c1de052

4 files changed

Lines changed: 76 additions & 115 deletions

File tree

dConnectJavascriptApp/js/profile/canvas.js

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,12 @@ function showCanvasDrawImage(serviceId) {
5555
serviceId);
5656
reloadHeader(btnStr);
5757
reloadFooter(btnStr);
58+
5859
/* ※省略パラメータがあり1種類のformでは対応できないため2つformを用意しています。 */
5960

6061
let str = '';
6162

62-
str += '<form action="' + uri + '" method="POST"' +
63-
' enctype="multipart/form-data" id="fileForm"><br>';
64-
str += '<input type="hidden" name="serviceId" value="' + serviceId + '"/>';
65-
str += '<input type="hidden" name="accessToken" value="' +
66-
sdk.getAccessToken() + '"/>';
67-
63+
str += '<form id="fileForm"><br>';
6864
str += makeInputText('path', 'path', 'path');
6965
str += makeInputText('uri', 'uri', 'uri');
7066
str += makeInputText('MIME-Type', 'mimeType', 'mimeType');
@@ -114,38 +110,22 @@ function doCanvasDrawImage(serviceId, fileFormId) {
114110
showLoading();
115111

116112
let myForm = document.getElementById(fileFormId);
117-
let myFormData = new FormData(myForm);
118-
let myXhr = new XMLHttpRequest();
119-
try {
120-
if (!myFormData.get('mode')) {
121-
myFormData.delete('mode');
122-
}
123-
} catch (e) {
124-
// Safariなどでは、FormData#append以外はサポートしていないため、エラーが出る
125-
}
126-
myXhr.open(myForm.method, myForm.action, true);
127-
myXhr.onreadystatechange = function() {
128-
if (myXhr.readyState === 4) {
129-
if (myXhr.status === 200 || myXhr.status == 0) {
130-
if (DEBUG) {
131-
console.log('Response:' + myXhr.responseText)
132-
}
133-
let str = '';
134-
let obj = JSON.parse(myXhr.responseText);
135-
if (obj.result == 0) {
136-
//alert('success:canvas/drawimage');
137-
$('#path').val('');
138-
$('#data').val('');
139-
} else {
140-
showError('POST canvas/drawimage', obj.errorCode, obj.errorMessage);
141-
}
142-
} else {
143-
alert('error:' + myXhr.status);
144-
}
145-
closeLoading();
146-
}
147-
};
148-
myXhr.send(myFormData);
113+
let inputs = myForm.elements;
114+
let params = { serviceId };
115+
parseInputElements(inputs, params);
116+
117+
sdk.post({
118+
profile: 'canvas',
119+
attribute: 'drawImage',
120+
params
121+
}).then(json => {
122+
closeLoading();
123+
$('#path').val('');
124+
$('#data').val('');
125+
}).catch(e => {
126+
closeLoading();
127+
showError('POST canvas/drawimage', e.errorCode, e.errorMessage);
128+
});
149129
}
150130

151131
/**

dConnectJavascriptApp/js/profile/file.js

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,8 @@ function showFileSend(serviceId) {
562562
reloadHeader(btnStr);
563563
reloadFooter(btnStr);
564564

565-
let builder = new sdk.URIBuilder();
566-
builder.setProfile('file');
567-
let uri = builder.build();
568-
569-
if (DEBUG) {
570-
console.log('Uri: ' + uri);
571-
}
572-
573565
let str = '';
574-
str += '<form action="' + uri + '" method="POST"' +
575-
' enctype="multipart/form-data" id="fileForm"><br>';
576-
str += '<input type="hidden" name="serviceId" value="' + serviceId + '"/>';
577-
str += '<input type="hidden" name="accessToken" value="' + sdk.getAccessToken() + '"/>';
566+
str += '<form id="fileForm"><br>';
578567
str += makeSelectBoolean('forceOverwrite');
579568
str += makeInputText('path', 'path', 'path');
580569
str += makeInputText('uri', 'uri', 'uri');
@@ -606,37 +595,20 @@ function doFileSend(serviceId) {
606595
showLoading();
607596

608597
let myForm = document.getElementById('fileForm');
609-
let myFormData = new FormData(myForm);
610-
611-
try {
612-
if (myFormData.get('uri') === '') {
613-
myFormData.delete('uri');
614-
}
615-
} catch (e) {
616-
}
598+
let inputs = myForm.elements;
599+
let params = { serviceId };
600+
parseInputElements(inputs, params);
617601

618-
let myXhr = new XMLHttpRequest();
619-
myXhr.open(myForm.method, myForm.action, true);
620-
myXhr.onreadystatechange = function() {
621-
if (myXhr.readyState === 4) {
622-
if (myXhr.status === 200 || myXhr.status == 0) {
623-
if (DEBUG) {
624-
console.log('Response:' + myXhr.responseText)
625-
}
626-
let str = '';
627-
let obj = JSON.parse(myXhr.responseText);
628-
if (obj.result == 0) {
629-
alert('success:file/send');
630-
} else {
631-
showError('PUT file/send', obj.errorCode, obj.errorMessage);
632-
}
633-
} else {
634-
alert('error:' + myXhr.status);
635-
}
636-
closeLoading();
637-
}
638-
};
639-
myXhr.send(myFormData);
602+
sdk.post({
603+
profile: 'file',
604+
params
605+
}).then(json => {
606+
closeLoading();
607+
alert('success:file/send');
608+
}).catch(e => {
609+
closeLoading();
610+
showError('PUT file/send', e.errorCode, e.errorMessage);
611+
});
640612
}
641613

642614
/**

dConnectJavascriptApp/js/profile/notification.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ function showNotification(serviceId) {
3030
setTitle('Notification Profile(Notify)');
3131

3232
let str = '';
33-
str += '<form action="' + uri + '" method="POST" id="notificationForm"' +
34-
' name="notificationForm" enctype="multipart/form-data"' +
35-
' onsubmit="return false;">';
33+
str += '<form id="notificationForm" name="notificationForm">';
3634

3735
if (myDeviceName.indexOf('Pebble') == -1 &&
3836
myDeviceName.indexOf('SmartWatch') == -1 &&
@@ -254,39 +252,32 @@ function doUnregisterNotificationError(serviceId) {
254252
*/
255253
function doNotificationNotify(serviceId) {
256254
let myForm = document.getElementById('notificationForm');
257-
let myFormData = new FormData(myForm);
258-
let myXhr = new XMLHttpRequest();
259-
myXhr.open(myForm.method, myForm.action, true);
260-
myXhr.onreadystatechange = function() {
261-
if (myXhr.readyState === 4) {
262-
if (myXhr.status === 200 || myXhr.status == 0) {
263-
if (DEBUG) {
264-
console.log('Response:' + myXhr.responseText)
265-
}
266-
let obj = JSON.parse(myXhr.responseText);
267-
if (obj.result == 0) {
268-
let str = '';
269-
if (myDeviceName.indexOf('Pebble') != -1) {
270-
} else if (myDeviceName.indexOf('SmartWatch') != -1) {
271-
} else {
272-
str += '<center>';
273-
str += '<input type="button" onclick="notificationDel(\'' +
274-
serviceId + '\',\'' + obj.notificationId +
275-
'\');" value="Delete" type="button" >';
276-
str += '</center>';
277-
reloadMenu(str)
278-
}
279-
} else {
280-
showError('POST notification/notify',
281-
obj.errorCode, obj.errorMessage);
282-
}
283-
} else {
284-
alert('error:' + myXhr.status);
285-
}
286-
closeLoading();
255+
let inputs = myForm.elements;
256+
let params = { serviceId };
257+
parseInputElements(inputs, params);
258+
259+
sdk.post({
260+
profile: 'notification',
261+
attribute: 'notify',
262+
params
263+
}).then(json => {
264+
closeLoading();
265+
266+
let str = '';
267+
if (myDeviceName.indexOf('Pebble') != -1) {
268+
} else if (myDeviceName.indexOf('SmartWatch') != -1) {
269+
} else {
270+
str += '<center>';
271+
str += '<input type="button" onclick="notificationDel(\'' +
272+
serviceId + '\',\'' + json.notificationId +
273+
'\');" value="Delete" type="button" >';
274+
str += '</center>';
275+
reloadMenu(str)
287276
}
288-
};
289-
myXhr.send(myFormData);
277+
}).catch(e => {
278+
closeLoading();
279+
showError('POST notification/notify', e.errorCode, e.errorMessage);
280+
});
290281
}
291282

292283
/**

dConnectJavascriptApp/js/sdk/util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,22 @@ function toLocaleDateTime(date) {
257257
let timeStr = date.toLocaleTimeString();
258258
let msStr = (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5);
259259
return `${dateStr} ${timeStr}.${msStr}`;
260+
}
261+
262+
function parseInputElements(inputs, params) {
263+
for (let i = 0; i < inputs.length; i++) {
264+
let input = inputs[i];
265+
if (input.type === 'button') {
266+
continue;
267+
} else if (input.type === 'file') {
268+
if (input.files.length > 0) {
269+
params[input.name] = input.files[0];
270+
}
271+
} else {
272+
let value = input.value;
273+
if (value !== '') {
274+
params[input.name] = value;
275+
}
276+
}
277+
}
260278
}

0 commit comments

Comments
 (0)