forked from fc2blog/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax_add.twig
More file actions
168 lines (157 loc) · 7.21 KB
/
Copy pathajax_add.twig
File metadata and controls
168 lines (157 loc) · 7.21 KB
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
{# include from admin/entries/create #}
<table>
<tbody>
<tr>
<th>{{ _('Categories') }}</th>
<td>
<ul id="sys-category-list">
{{ renderCategoriesTree(categories, entry_categories) }}
</ul>
</td>
</tr>
<tr>
<th>{{ _('Category add') }}</th>
<td>
<dl class="form_item">
<dt><label>{{ _('Parent category') }}</label></dt>
<dd>
<select id="sys-category-add-parent">
<option value="0">{{ _('Not selected') }}</option>
{% for category in categories %}
<option value="{{ category.id }}" {% if category.id == 1 %}disabled="disabled"{% endif %}>{{ spaceIndent(category.level) }}{{ category.name }}</option>
{% endfor %}
</select>
</dd>
<dt><label>{{ _('Category name') }}</label></dt>
<dd>
<input type="text" id="sys-category-add-name" placeholder="{{ _('Category name') }}"/>
<input id="sys-category-add" type="button" value="{{ _('Add') }}"/>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
<script>
// カテゴリーのチェックボックスクリック時 強調表示と親階層のチェック処理
function categoryChange(target) {
var target = $(target);
target.prop('checked') ? target.parent().addClass('active') : target.parent().removeClass('active');
// 以下の処理は親階層のチェック処理を行う
var parent = target.parent('li');
// 親全てチェック
if (target.prop('checked')) {
while ((parent = parent.parent('ul').parent('li').prev('li')).length) {
parent.find('input[type=checkbox]').prop('checked', true).parent().addClass('active');
}
return;
}
// 同一階層にチェック済みがない場合 1つ上の親のチェックを外す
while (!parent.siblings().children('input[type=checkbox]:checked').length) {
parent = parent.parent('ul').parent('li').prev('li');
if (!parent.length) {
return;
}
parent.find('input[type=checkbox]').prop('checked', false).parent().removeClass('active');
}
}
$(function () {
// セレクトボックスにカテゴリを追加
var addSelect = function (category) {
if (category.parent_id == 0) {
// 末尾に追加
$('#sys-category-add-parent').append($('<option>').html(category.name).val(category.id));
return;
}
// 親の子供の末尾に追加(目印になるものが無いので空白の個数便りに探しだしている)
var option = $('#sys-category-add-parent > option[value=' + category.parent_id + ']');
var spaceCount = option.html().split(' ').length;
var space = '';
for (var i = 0; i < spaceCount; i++) {
space += ' ';
}
do {
if (option.next('option').length == 0) {
$('#sys-category-add-parent').append($('<option>').html(space + category.name).val(category.id));
break;
}
option = option.next('option');
if (option.html().split(space).length - 1 == 0) {
option.prev().after($('<option>').html(space + category.name).val(category.id));
break;
}
} while (option.length);
};
// カテゴリ一覧にカテゴリを追加
var addList = function (category) {
var html = '<li class="active">';
html += ' <input id="sys-entry-categories-id-' + category.id + '" type="checkbox" name="entry_categories[category_id][]" value="' + category.id + '" checked="checked" onclick="categoryChange(this);" />';
html += ' <label for="sys-entry-categories-id-' + category.id + '">' + category.name + '</label>';
html += '</li>';
if (category.parent_id == 0) {
$('#sys-category-list').append($(html).hide());
$('#sys-entry-categories-id-' + category.id).parent().slideDown('fast');
} else {
var li = $('#sys-entry-categories-id-' + category.parent_id).parent();
if (!li.next().children('ul').length) {
// 子無しの場合は子を入れるulを追加する
li.after('<li><ul></ul></li>');
}
// 末尾に追加する
li.next().children('ul').append($(html).hide());
$('#sys-entry-categories-id-' + category.id).parent().slideDown('fast');
}
};
// Enterキーでカテゴリ追加
$('#sys-category-add-name').keypress(function (e) {
if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
$('#sys-category-add').click();
}
});
// カテゴリ追加処理
$('#sys-category-add').click(function () {
var parent_id = $('#sys-category-add-parent').val();
var name = $('#sys-category-add-name').val();
if (name == '') {
alert('{{ _('Please enter the category name') }}');
return;
}
if ($('#sys-category-add').prop('disabled')) {
return;
}
$('#sys-category-add').attr('disabled', 'disabled');
$('#sys-category-add').val('通信中');
$.ajax({
url: '{{ url(req, 'Categories', 'ajax_add') }}',
type: 'POST',
data: {
category: {
parent_id: parent_id,
name: name
},
sig: "{{ sig }}"
},
dataType: 'json',
success: function (json) {
if (json.status != 1) {
alert(json.error.name);
} else {
addSelect(json.category); // セレクトボックスにカテゴリを追加
addList(json.category); // 一覧にカテゴリを追加
}
$('#sys-category-add-name').val('');
$('#sys-category-add').removeAttr('disabled');
$('#sys-category-add').val('{{ _('Add') }}');
},
error: function (data, status, xhr){
alert("カテゴリ追加時にエラーが発生しました、時間をおいてから再度試行してください。\n" +
"An error occurred when adding the category, please wait a while and try again.");
$('#sys-category-add-name').val('');
$('#sys-category-add').removeAttr('disabled');
$('#sys-category-add').val('{{ _('Add') }}');
}
});
return false;
});
});
</script>