これは MTAppjQuery Advent Calendar 2018の 7日目の記事です。 今回は、Movable Type 7 の `コンテンツデータ` または `記事` を複製できる独自のメソッドを作ってみました。
これは MTAppjQuery Advent Calendar 2018 の 7日目の記事です。
今回は、Movable Type 7 の コンテンツデータ
または 記事
を複製できる独自のメソッドを作ってみました。
実際に動作させてみると下図のような感じになります。
今回のコードは下記のようになります。 細かい説明は割愛させていただきますが、MTAppjQuery v2 から同梱されているエムロジック社の DataAPIProxy プラグインのおかげで、このような認証が必要なエンドポイントでも、Movable Type で権限を持っていれば、管理画面の認証を利用して Data API の認証も通すことができます。
なお、ところどころ alert
や prompt
系の処理には昨日の Movable Type の管理画面に SweetAlert2 を導入してみよう で導入した SweetAlert2 を利用しています。
(function ($) {
mtapp.apiDuplicateContent = function () {
mtappVars.DataAPI.getToken(function (token) {
if (token.error) {
mtapp.modalMsg({
title: 'Error',
content: token.error.message
});
return;
}
if (mtappVars.type === 'content_data') {
mtappVars.DataAPI.getContentData(mtappVars.blog_id, mtappVars.content_type_id, mtappVars.content_data.id, function (data) {
if (data.error) {
mtapp.modalMsg({
title: 'Error',
content: data.error.message
});
}
delete data.id;
swal({
type: 'info',
title: data.label + 'を複製します。'
}).then(function () {
data.status = 'Draft';
mtapp.loadingImage('show');
const newContentData = Object.toJSON(data);
mtappVars.DataAPI.createContentData(mtappVars.blog_id, mtappVars.content_type_id, newContentData, function (response) {
mtapp.loadingImage('hide');
if (response.id) {
swal({
type: 'success',
title: response.label + 'を作成しました。'
}).then(function () {
location.href = mtappVars.adminScript + '?__mode=list&_type=content_data&type=content_data_' + mtappVars.content_type_id + '&blog_id=' + mtappVars.blog_id;
});
return;
}
swal({
type: 'error',
title: data.error.message
});
return;
});
});
});
}
else if (mtappVars.type === 'entry') {
mtappVars.DataAPI.getEntry(mtappVars.blog_id, mtappVars.entry.id, function (entry) {
if (entry.error) {
mtapp.modalMsg({
title: 'Error',
content: entry.error.message
});
}
delete entry.id;
entry.title = 'COPY_' + entry.title;
swal({
title: 'タイトルを入力してください',
input: 'text',
inputValue: entry.title
}).then(function (res) {
if (res.value) {
entry.title = res.value;
}
entry.status = 'Draft';
mtapp.loadingImage('show');
const newEntry = Object.toJSON(entry);
mtappVars.DataAPI.createEntry(mtappVars.blog_id, newEntry, function (response) {
mtapp.loadingImage('hide');
if (response.id) {
swal({
type: 'success',
title: response.title + 'を作成しました。'
}).then(function () {
location.href = mtappVars.adminScript + '?__mode=list&_type=entry&blog_id=' + mtappVars.blog_id;
});
return;
}
swal({
type: 'error',
title: entry.error.message
});
return;
});
});
});
}
});
};
})(jQuery);
このように MTAppjQuery では、プラグインが提供しているメソッドの他に、ご自身で独自のメソッドを定義して自由自在に Movable Type の管理画面をカスタマイズすることができます。より使いやすい Movable Type を目指しましょう。
以上です。