Last active
June 24, 2016 01:26
-
-
Save tinybeans/6f0b600402eb370e5a835186c59a84b4 to your computer and use it in GitHub Desktop.
$.MTAppTemplateSelectorWidget();
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
// --------------------------------------------------------------------- | |
// $.MTAppTemplateSelectorWidget(); | |
// --------------------------------------------------------------------- | |
// Latest update: 2016/06/24 | |
// | |
// テンプレートの編集画面に、テンプレート一覧のドロップダウンリストウィジェットを追加し、 | |
// 選択したテンプレートの編集画面にジャンプします。 | |
// | |
// --------------------------------------------------------------------- | |
$.MTAppTemplateSelectorWidget = function(options, words){ | |
var op = $.extend({}, $.MTAppTemplateSelectorWidget.defaults, options); | |
var language = mtappVars.language === 'ja' ? 'ja' : 'en'; | |
var words = words || {}; | |
var l10n = $.extend({}, $.MTAppTemplateSelectorWidget.l10n[language], words); | |
if (mtappVars.template_filename !== 'edit_template') { | |
return; | |
} | |
var api; | |
if (!op.api) { | |
api = op.api; | |
} else if (typeof mtappVars.DataAPI !== 'undefined') { | |
api = mtappVars.DataAPI; | |
} else { | |
return $.errorMessage('MTAppTemplateSelectorWidget', 'DataAPI Object is not found.', 'alert', false); | |
} | |
api.getToken(function(response){ | |
if (response.error) { | |
alert(response.error.message); | |
return false; | |
} | |
var params = { | |
fields: 'id,name,type', | |
limit: 100000 | |
}; | |
api.listTemplates(mtappVars.blog_id, params, function(templates){ | |
var currentTemplateName = $('#title').val(); | |
var items = templates.items; | |
$.objectSort(items, 'type', 'ascend'); | |
var list = {}; | |
for (var i = 0, l = items.length; i < l; i++) { | |
var type = items[i]['type']; | |
var id = items[i]['id']; | |
var name = items[i]['name']; | |
var selected = name === currentTemplateName ? ' selected="selected"' : ''; | |
if (typeof list[type] === 'undefined') { | |
list[type] = ''; | |
} | |
list[type] += '<option value="' + id + '"' + selected + '>' + name + '</option>'; | |
} | |
var html = ''; | |
for (var key in list) { | |
if (list.hasOwnProperty(key)) { | |
html += '<optgroup label="' + key + '">' + list[key] + '</optgroup>'; | |
} | |
} | |
html = '<select id="mtapptemplateselector" style="width:100%;">' + html + '</select>' + | |
'<p style="margin:5px 0;"><input type="checkbox" id="mtapptemplateselector-newwindow"> ' + l10n.newWindow + '</p>' + | |
'<a id="mtapptemplateselector-newwindow-open" href="#" target="_blank" style="display:none;">' + l10n.open + '</a>'; | |
var widget = $.MTAppMakeWidget({ | |
label: l10n.widgetName, | |
content: html, | |
action: '', | |
footer: '' | |
}); | |
$('#template-status-widget').before(widget); | |
var $openWindow = $('#mtapptemplateselector-newwindow-open'); | |
$('#mtapptemplateselector-newwindow').on('click.MTAppTemplateSelectorWidget', function(){ | |
if ($(this).is(':checked')) { | |
$openWindow.show(); | |
} else { | |
$openWindow.hide(); | |
} | |
}); | |
$('#mtapptemplateselector').on('change.MTAppTemplateSelectorWidget', function(){ | |
var id = $(this).val(); | |
var link = CMSScriptURI + '?__mode=view&_type=template&blog_id=' + mtappVars.blog_id + '&id=' + id; | |
if ($('#mtapptemplateselector-newwindow').is(':checked')) { | |
$openWindow.attr('href', link); | |
} else { | |
location.href = link; | |
} | |
}); | |
}); | |
}); | |
}; | |
$.MTAppTemplateSelectorWidget.l10n = { | |
en: { | |
widgetName: 'Template Selector', | |
newWindow: 'In a new window', | |
open: 'open' | |
}, | |
ja: { | |
widgetName: 'テンプレートセレクター', | |
newWindow: '別ウィンドウ', | |
open: '開く' | |
} | |
}; | |
$.MTAppTemplateSelectorWidget.defaults = { | |
// Data API object | |
api: null | |
}; | |
// end - $.MTAppTemplateSelectorWidget(); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment