/* -*- coding: utf-8 -*-
*
* Copyright 2013 whiteleaf. All rights reserved.
*/
var t; // for debugging
$(document).ready(function() {
"use strict";
var touchable_device = "ontouchstart" in window;
var click_event_name = (touchable_device ? "touchstart" : "click");
// MEMO: table 全てに対して操作
// table.$("tr.selected").removeClass("selected");
// MEMO: .selected が付いている行を全て取得
// table.rows('.selected').data().length
// 端末ごとの表示項目初期設定
var cell_visible = (touchable_device ?
// スマフォ系端末
{
id: false, last_update: false, title: true, author: false, sitename: false,
toc_url: false, novel_type: false, tags: false, status: false, menu: true,
download: false,
}
// PC
: {
id: true, last_update: true, title: true, author: true, sitename: true,
toc_url: true, novel_type: true, tags: true, status: true, menu: true,
download: true,
});
$.fn.dataTable.Api.register("fireChangeSelect()", function() {
$(this.table().node()).trigger("changeselect");
});
$.fn.dataTable.Api.register("eachRow()", function(callback) {
this.table().rows().eq(0).each(function(idx) {
var row = table.row(idx);
var data = row.data();
callback(row, data);
});
});
var now = new Date;
var today = Narou.formatDate(now, "YYYYMMDD");
var NEW_ARRIVALS_LIMIT = 6 * 60 * 60; // 6時間(秒)
function isToday(date) {
var dateobj = (typeof date === "number" ? new Date(date * 1000) : date);
return Narou.formatDate(dateobj, "YYYYMMDD") === today;
}
var table = t = $("#novel-list").DataTable({
ajax: "/api/list",
dom: (touchable_device ? 'lprtpi' : 'Rlprtpi'),
stateSave: true,
stateDuration: 0, // tableの状態保存を永続化
paginationType: "full_numbers",
lengthMenu: [[20, 50, 100, -1], [20, 50, 100, "全"]],
order: [[ 1, "desc" ]], // 最初は新着順でソートしておく
columns: [
// ID
{ title: "ID",
data: "id", className: "column-id text-center", visible: cell_visible.id },
// 最終更新日
{ title: "更新日",
data: "last_update", className: "text-center",
orderSequence: [ "desc", "asc" ], visible: cell_visible.last_update,
render: function(data, type, row) {
if (type === "display" || type === "filter") {
var date = new Date(data * 1000);
var display = Narou.formatDate(date, "YYYY/MM/DD
hh:mm");
if (type === "display") {
var klass = null;
if (row.new_arrivals_date) {
if ((row.new_arrivals_date + NEW_ARRIVALS_LIMIT) * 1000 >= now) {
klass = "new-arrivals";
}
}
if (!klass && isToday(date)) {
klass = "today-update";
}
if (klass) {
display = "" + display + "";
}
}
return display;
}
return data;
}
},
// タイトル
{ title: "タイトル",
data: "title", visible: cell_visible.title },
// 作者名
{ title: "作者名",
data: "author", visible: cell_visible.author },
// 掲載サイト
{ title: "掲載",
data: "sitename", visible: cell_visible.sitename },
// 掲載URL
{
title: "リンク",
data: "toc_url", className: "text-center", orderable: false,
searchable: false, visible: cell_visible.toc_url },
// 小説種別
{ title: "種別",
data: "novel_type", className: "text-center", width: "25px",
orderSequence: [ "desc", "asc" ], visible: cell_visible.novel_type },
// タグ
{ title: "タグ",
data: "tags", orderSequence: [ "desc", "asc" ], width: "70px",
visible: cell_visible.tags },
// 状態
{ title: "状態",
data: "status", visible: cell_visible.status },
// 書籍データDLリンク
{
title: "DL",
data: "download", className: "text-center", orderable: false,
searchable: false, visible: cell_visible.download },
{
title: "個別",
data: "menu", className: "text-center",
defaultContent: '',
visible: cell_visible.menu,
createdCell: function(td, cellData, rowData, rowIndex, colIndex) {
$(td).children("button")
.data("targetId", rowData.id)
.on("click", function(e) {
e.stopPropagation();
var $this = $(this);
var $tr = $(e.target).closest("tr");
var target_id = $this.data("targetId");
var $cover = $("