Skip to content

Instantly share code, notes, and snippets.

@tohokuaiki
Last active August 22, 2019 03:18
Show Gist options
  • Save tohokuaiki/71f1c384c19ddda6f56250dbaa6c888c to your computer and use it in GitHub Desktop.
Save tohokuaiki/71f1c384c19ddda6f56250dbaa6c888c to your computer and use it in GitHub Desktop.
AJS.$((function($){
return function(){
// 添付ファイルのコメント欄を偽装する comment = "url|+|http://www.example.com<:>title|+|about this url"
var attachCommentDelimiter = '<:>';
var propertyDelimiter = '|+|';
var extraAttachComments = {
title: '使用論文名',
comment: 'コメント',
url: '論文URL'
};
var extraCommentClass = 'junoe-extracomment';
/* stringからプロパティObjectを作成 */
var getPropertiesFromComment = function(comment){
var properties = {};
var comments = comment.split(attachCommentDelimiter);
for (var prop in extraAttachComments){
properties[prop] = "";
for (var i=0,j=comments.length; i<j; i++){
if (comments[i].indexOf(prop + propertyDelimiter) === 0){
properties[prop] = comments[i].substring(prop.length + propertyDelimiter.length);
break;
}
}
}
return properties;
};
// プロパティ入力欄の分割
$('#content.view-attachment .attachment-field input#newComment').each(function(i, input){
var field = $(input).closest('div.attachment-field');
var fieldgroup = $(input).closest('div.field-group');
fieldgroup.hide();
var createInput = function(label, name, value){
var line = $('<div class="field-group"><label>'+label+'</label><input type="text" class="text '+extraCommentClass+'" rel="'+name+'"/></div>');
line.find('input').val(value);
return line;
};
var comments = getPropertiesFromComment($(input).val());
for (var prop in extraAttachComments){
field.append(createInput(extraAttachComments[prop], prop, comments[prop]));
}
});
// 保存時に結合
$('form[name="editattachment"]').on('submit', function(e){
var form = $(e.currentTarget);
var comment = [];
form.find('input.'+ extraCommentClass).each(function(i, input){
comment.push($(input).attr('rel') + propertyDelimiter + $(input).val());
});
$('input#newComment').val(comment.join(attachCommentDelimiter));
});
// 分割した添付ファイルのコメントをうまく見せる
$('#view-attachments table.tableview td.comment,table.attachments td.filename-column span.attachment-comment').each(function(i, dom){
var comments = getPropertiesFromComment(jQuery.trim($(dom).text()));
var html = "";
for (var prop in extraAttachComments){
var comment = typeof(comments[prop]) === 'undefined' ? '' : comments[prop];
if (comment && comment.length > 0) {
html += extraAttachComments[prop] + " : " + comment + "<br>";
}
}
$(dom).html(html);
});
};
})(AJS.$));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment