narou 1.7.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of narou might be problematic. Click here for more details.
- checksums.yaml +5 -13
- data/.gitignore +1 -0
- data/ChangeLog.md +35 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +100 -0
- data/README.md +28 -39
- data/lib/color.rb +0 -2
- data/lib/command.rb +1 -0
- data/lib/command/convert.rb +33 -4
- data/lib/command/diff.rb +5 -4
- data/lib/command/download.rb +9 -1
- data/lib/command/flag.rb +2 -2
- data/lib/command/list.rb +1 -1
- data/lib/command/mail.rb +3 -3
- data/lib/command/remove.rb +2 -1
- data/lib/command/send.rb +7 -6
- data/lib/command/setting.rb +39 -95
- data/lib/command/tag.rb +25 -13
- data/lib/command/update.rb +6 -1
- data/lib/command/version.rb +5 -1
- data/lib/command/web.rb +111 -0
- data/lib/commandbase.rb +5 -2
- data/lib/commandline.rb +16 -0
- data/lib/converterbase.rb +20 -14
- data/lib/device.rb +5 -4
- data/lib/downloader.rb +68 -39
- data/lib/eventable.rb +72 -0
- data/lib/helper.rb +105 -37
- data/lib/ini.rb +2 -1
- data/lib/input.rb +68 -0
- data/lib/inventory.rb +4 -0
- data/lib/kindlestrip.rb +2 -2
- data/lib/logger.rb +41 -19
- data/lib/narou.rb +10 -0
- data/lib/narou/api.rb +1 -1
- data/lib/novelconverter.rb +8 -21
- data/lib/novelsetting.rb +79 -4
- data/lib/version.rb +1 -1
- data/lib/web/all.rb +12 -0
- data/lib/web/appserver.rb +612 -0
- data/lib/web/helper4web.rb +15 -0
- data/lib/web/progressbar4web.rb +32 -0
- data/lib/web/public/favicon.ico +0 -0
- data/lib/web/public/resources/bootbox.min.js +6 -0
- data/lib/web/public/resources/common.ui.js +143 -0
- data/lib/web/public/resources/dataTables.colVis.js +1113 -0
- data/lib/web/public/resources/help/rect_select.png +0 -0
- data/lib/web/public/resources/help/ssmain.png +0 -0
- data/lib/web/public/resources/help/tag.png +0 -0
- data/lib/web/public/resources/jquery.moveto.js +44 -0
- data/lib/web/public/resources/jquery.outerclick.js +60 -0
- data/lib/web/public/resources/jquery.slidenavbar.js +89 -0
- data/lib/web/public/resources/narou.library.js +815 -0
- data/lib/web/public/resources/narou.ui.js +993 -0
- data/lib/web/public/resources/perfect-scrollbar.min.css +5 -0
- data/lib/web/public/resources/perfect-scrollbar.min.js +4 -0
- data/lib/web/public/resources/shortcut.js +223 -0
- data/lib/web/public/resources/sort_asc.png +0 -0
- data/lib/web/public/resources/sort_desc.png +0 -0
- data/lib/web/public/resources/toggle-switch.css +322 -0
- data/lib/web/public/robots.txt +3 -0
- data/lib/web/public/test/jquery.outerclick.html +72 -0
- data/lib/web/pushserver.rb +110 -0
- data/lib/web/settingmessages.rb +14 -0
- data/lib/web/streaminginput.rb +103 -0
- data/lib/web/streaminglogger.rb +52 -0
- data/lib/web/views/about.haml +11 -0
- data/lib/web/views/help.haml +105 -0
- data/lib/web/views/index.haml +245 -0
- data/lib/web/views/js/widget.erb +74 -0
- data/lib/web/views/layout.haml +49 -0
- data/lib/web/views/novels/setting.haml +177 -0
- data/lib/web/views/settings.haml +115 -0
- data/lib/web/views/style.scss +737 -0
- data/lib/web/views/widget.haml +39 -0
- data/lib/web/web-socket-ruby/.gitignore +1 -0
- data/lib/web/web-socket-ruby/README.txt +75 -0
- data/lib/web/web-socket-ruby/lib/web_socket.rb +601 -0
- data/lib/web/web-socket-ruby/samples/chat_server.rb +58 -0
- data/lib/web/web-socket-ruby/samples/echo_server.rb +33 -0
- data/lib/web/web-socket-ruby/samples/stdio_client.rb +25 -0
- data/lib/web/worker.rb +87 -0
- data/narou.gemspec +36 -3
- data/narou.rb +8 -6
- data/preset/ncode.syosetu.com/n8725k/converter.rb +2 -1
- data/spec/data/convert_test/replace/correct_test_replace.txt +1 -1
- data/spec/data/convert_test/replace/test_replace.txt +1 -1
- data/spec/data/convert_test/ruby/correct_test_ruby.txt +18 -1
- data/spec/data/convert_test/ruby/test_ruby.txt +18 -0
- data/spec/downloader_spec.rb +37 -0
- data/spec/eventable_spec.rb +172 -0
- data/spec/exit_code_spec.rb +67 -0
- data/spec/helper_spec.rb +72 -0
- data/spec/input_spec.rb +76 -0
- data/spec/logger_spec.rb +53 -0
- data/spec/novelsetting_spec.rb +35 -0
- data/spec/worker_spec.rb +56 -0
- data/template/ibunko_novel.txt.erb +2 -2
- data/template/novel.txt.erb +2 -2
- metadata +213 -29
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright 2013 whiteleaf. All rights reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
require_relative "../progressbar"
|
7
|
+
|
8
|
+
#
|
9
|
+
# コンソール用のプログレスバーはWEB UIでは使えないため置き換える
|
10
|
+
#
|
11
|
+
class ProgressBar
|
12
|
+
def self.push_server=(server)
|
13
|
+
@@push_server = server
|
14
|
+
end
|
15
|
+
|
16
|
+
alias :original_initialize :initialize
|
17
|
+
|
18
|
+
def initialize(*args)
|
19
|
+
original_initialize(*args)
|
20
|
+
@@push_server.send_all("progressbar.init" => true)
|
21
|
+
end
|
22
|
+
|
23
|
+
def output(num)
|
24
|
+
percent = calc_ratio(num) * 100
|
25
|
+
@@push_server.send_all("progressbar.step" => percent)
|
26
|
+
end
|
27
|
+
|
28
|
+
def clear
|
29
|
+
@@push_server.send_all("progressbar.clear" => true)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
Binary file
|
@@ -0,0 +1,6 @@
|
|
1
|
+
/**
|
2
|
+
* bootbox.js v4.3.0
|
3
|
+
*
|
4
|
+
* http://bootboxjs.com/license.txt
|
5
|
+
*/
|
6
|
+
!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof exports?module.exports=b(require("jquery")):a.bootbox=b(a.jQuery)}(this,function a(b,c){"use strict";function d(a){var b=q[o.locale];return b?b[a]:q.en[a]}function e(a,c,d){a.stopPropagation(),a.preventDefault();var e=b.isFunction(d)&&d(a)===!1;e||c.modal("hide")}function f(a){var b,c=0;for(b in a)c++;return c}function g(a,c){var d=0;b.each(a,function(a,b){c(a,b,d++)})}function h(a){var c,d;if("object"!=typeof a)throw new Error("Please supply an object of options");if(!a.message)throw new Error("Please specify a message");return a=b.extend({},o,a),a.buttons||(a.buttons={}),c=a.buttons,d=f(c),g(c,function(a,e,f){if(b.isFunction(e)&&(e=c[a]={callback:e}),"object"!==b.type(e))throw new Error("button with key "+a+" must be an object");e.label||(e.label=a),e.className||(e.className=2>=d&&f===d-1?"btn-primary":"btn-default")}),a}function i(a,b){var c=a.length,d={};if(1>c||c>2)throw new Error("Invalid argument length");return 2===c||"string"==typeof a[0]?(d[b[0]]=a[0],d[b[1]]=a[1]):d=a[0],d}function j(a,c,d){return b.extend(!0,{},a,i(c,d))}function k(a,b,c,d){var e={className:"bootbox-"+a,buttons:l.apply(null,b)};return m(j(e,d,c),b)}function l(){for(var a={},b=0,c=arguments.length;c>b;b++){var e=arguments[b],f=e.toLowerCase(),g=e.toUpperCase();a[f]={label:d(g)}}return a}function m(a,b){var d={};return g(b,function(a,b){d[b]=!0}),g(a.buttons,function(a){if(d[a]===c)throw new Error("button key "+a+" is not allowed (options are "+b.join("\n")+")")}),a}var n={dialog:"<div class='bootbox modal' tabindex='-1' role='dialog'><div class='modal-dialog'><div class='modal-content'><div class='modal-body'><div class='bootbox-body'></div></div></div></div></div>",header:"<div class='modal-header'><h4 class='modal-title'></h4></div>",footer:"<div class='modal-footer'></div>",closeButton:"<button type='button' class='bootbox-close-button close' data-dismiss='modal' aria-hidden='true'>×</button>",form:"<form class='bootbox-form'></form>",inputs:{text:"<input class='bootbox-input bootbox-input-text form-control' autocomplete=off type=text />",textarea:"<textarea class='bootbox-input bootbox-input-textarea form-control'></textarea>",email:"<input class='bootbox-input bootbox-input-email form-control' autocomplete='off' type='email' />",select:"<select class='bootbox-input bootbox-input-select form-control'></select>",checkbox:"<div class='checkbox'><label><input class='bootbox-input bootbox-input-checkbox' type='checkbox' /></label></div>",date:"<input class='bootbox-input bootbox-input-date form-control' autocomplete=off type='date' />",time:"<input class='bootbox-input bootbox-input-time form-control' autocomplete=off type='time' />",number:"<input class='bootbox-input bootbox-input-number form-control' autocomplete=off type='number' />",password:"<input class='bootbox-input bootbox-input-password form-control' autocomplete='off' type='password' />"}},o={locale:"en",backdrop:!0,animate:!0,className:null,closeButton:!0,show:!0,container:"body"},p={};p.alert=function(){var a;if(a=k("alert",["ok"],["message","callback"],arguments),a.callback&&!b.isFunction(a.callback))throw new Error("alert requires callback property to be a function when provided");return a.buttons.ok.callback=a.onEscape=function(){return b.isFunction(a.callback)?a.callback():!0},p.dialog(a)},p.confirm=function(){var a;if(a=k("confirm",["cancel","confirm"],["message","callback"],arguments),a.buttons.cancel.callback=a.onEscape=function(){return a.callback(!1)},a.buttons.confirm.callback=function(){return a.callback(!0)},!b.isFunction(a.callback))throw new Error("confirm requires a callback");return p.dialog(a)},p.prompt=function(){var a,d,e,f,h,i,k;if(f=b(n.form),d={className:"bootbox-prompt",buttons:l("cancel","confirm"),value:"",inputType:"text"},a=m(j(d,arguments,["title","callback"]),["cancel","confirm"]),i=a.show===c?!0:a.show,a.message=f,a.buttons.cancel.callback=a.onEscape=function(){return a.callback(null)},a.buttons.confirm.callback=function(){var c;switch(a.inputType){case"text":case"textarea":case"email":case"select":case"date":case"time":case"number":case"password":c=h.val();break;case"checkbox":var d=h.find("input:checked");c=[],g(d,function(a,d){c.push(b(d).val())})}return a.callback(c)},a.show=!1,!a.title)throw new Error("prompt requires a title");if(!b.isFunction(a.callback))throw new Error("prompt requires a callback");if(!n.inputs[a.inputType])throw new Error("invalid prompt type");switch(h=b(n.inputs[a.inputType]),a.inputType){case"text":case"textarea":case"email":case"date":case"time":case"number":case"password":h.val(a.value);break;case"select":var o={};if(k=a.inputOptions||[],!k.length)throw new Error("prompt with select requires options");g(k,function(a,d){var e=h;if(d.value===c||d.text===c)throw new Error("given options in wrong format");d.group&&(o[d.group]||(o[d.group]=b("<optgroup/>").attr("label",d.group)),e=o[d.group]),e.append("<option value='"+d.value+"'>"+d.text+"</option>")}),g(o,function(a,b){h.append(b)}),h.val(a.value);break;case"checkbox":var q=b.isArray(a.value)?a.value:[a.value];if(k=a.inputOptions||[],!k.length)throw new Error("prompt with checkbox requires options");if(!k[0].value||!k[0].text)throw new Error("given options in wrong format");h=b("<div/>"),g(k,function(c,d){var e=b(n.inputs[a.inputType]);e.find("input").attr("value",d.value),e.find("label").append(d.text),g(q,function(a,b){b===d.value&&e.find("input").prop("checked",!0)}),h.append(e)})}return a.placeholder&&h.attr("placeholder",a.placeholder),a.pattern&&h.attr("pattern",a.pattern),f.append(h),f.on("submit",function(a){a.preventDefault(),a.stopPropagation(),e.find(".btn-primary").click()}),e=p.dialog(a),e.off("shown.bs.modal"),e.on("shown.bs.modal",function(){h.focus()}),i===!0&&e.modal("show"),e},p.dialog=function(a){a=h(a);var c=b(n.dialog),d=c.find(".modal-dialog"),f=c.find(".modal-body"),i=a.buttons,j="",k={onEscape:a.onEscape};if(g(i,function(a,b){j+="<button data-bb-handler='"+a+"' type='button' class='btn "+b.className+"'>"+b.label+"</button>",k[a]=b.callback}),f.find(".bootbox-body").html(a.message),a.animate===!0&&c.addClass("fade"),a.className&&c.addClass(a.className),"large"===a.size&&d.addClass("modal-lg"),"small"===a.size&&d.addClass("modal-sm"),a.title&&f.before(n.header),a.closeButton){var l=b(n.closeButton);a.title?c.find(".modal-header").prepend(l):l.css("margin-top","-10px").prependTo(f)}return a.title&&c.find(".modal-title").html(a.title),j.length&&(f.after(n.footer),c.find(".modal-footer").html(j)),c.on("hidden.bs.modal",function(a){a.target===this&&c.remove()}),c.on("shown.bs.modal",function(){c.find(".btn-primary:first").focus()}),c.on("escape.close.bb",function(a){k.onEscape&&e(a,c,k.onEscape)}),c.on("click",".modal-footer button",function(a){var d=b(this).data("bb-handler");e(a,c,k[d])}),c.on("click",".bootbox-close-button",function(a){e(a,c,k.onEscape)}),c.on("keyup",function(a){27===a.which&&c.trigger("escape.close.bb")}),b(a.container).append(c),c.modal({backdrop:a.backdrop,keyboard:!1,show:!1}),a.show&&c.modal("show"),c},p.setDefaults=function(){var a={};2===arguments.length?a[arguments[0]]=arguments[1]:a=arguments[0],b.extend(o,a)},p.hideAll=function(){return b(".bootbox").modal("hide"),p};var q={br:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Sim"},cs:{OK:"OK",CANCEL:"Zrušit",CONFIRM:"Potvrdit"},da:{OK:"OK",CANCEL:"Annuller",CONFIRM:"Accepter"},de:{OK:"OK",CANCEL:"Abbrechen",CONFIRM:"Akzeptieren"},el:{OK:"Εντάξει",CANCEL:"Ακύρωση",CONFIRM:"Επιβεβαίωση"},en:{OK:"OK",CANCEL:"Cancel",CONFIRM:"OK"},es:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Aceptar"},et:{OK:"OK",CANCEL:"Katkesta",CONFIRM:"OK"},fi:{OK:"OK",CANCEL:"Peruuta",CONFIRM:"OK"},fr:{OK:"OK",CANCEL:"Annuler",CONFIRM:"D'accord"},he:{OK:"אישור",CANCEL:"ביטול",CONFIRM:"אישור"},id:{OK:"OK",CANCEL:"Batal",CONFIRM:"OK"},it:{OK:"OK",CANCEL:"Annulla",CONFIRM:"Conferma"},ja:{OK:"OK",CANCEL:"キャンセル",CONFIRM:"OK"},lt:{OK:"Gerai",CANCEL:"Atšaukti",CONFIRM:"Patvirtinti"},lv:{OK:"Labi",CANCEL:"Atcelt",CONFIRM:"Apstiprināt"},nl:{OK:"OK",CANCEL:"Annuleren",CONFIRM:"Accepteren"},no:{OK:"OK",CANCEL:"Avbryt",CONFIRM:"OK"},pl:{OK:"OK",CANCEL:"Anuluj",CONFIRM:"Potwierdź"},pt:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Confirmar"},ru:{OK:"OK",CANCEL:"Отмена",CONFIRM:"Применить"},sv:{OK:"OK",CANCEL:"Avbryt",CONFIRM:"OK"},tr:{OK:"Tamam",CANCEL:"İptal",CONFIRM:"Onayla"},zh_CN:{OK:"OK",CANCEL:"取消",CONFIRM:"确认"},zh_TW:{OK:"OK",CANCEL:"取消",CONFIRM:"確認"}};return p.init=function(c){return a(c||b)},p});
|
@@ -0,0 +1,143 @@
|
|
1
|
+
/* -*- coding: utf-8 -*-
|
2
|
+
*
|
3
|
+
* Copyright 2013 whiteleaf. All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
/*
|
7
|
+
* 全ページ共通のUI関係のコード
|
8
|
+
*/
|
9
|
+
$(document).ready(function() {
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
/*
|
13
|
+
* bootboxjs 初期設定
|
14
|
+
*/
|
15
|
+
bootbox.setDefaults({
|
16
|
+
locale: "ja",
|
17
|
+
backdrop: "static",
|
18
|
+
});
|
19
|
+
|
20
|
+
/*
|
21
|
+
* ページ内リンクをスクロールで移動するための初期化
|
22
|
+
* (jquery.moveto.js)
|
23
|
+
*/
|
24
|
+
$.moveTo();
|
25
|
+
|
26
|
+
/*
|
27
|
+
* 自然に消える alert
|
28
|
+
*/
|
29
|
+
$("#fadeout-alert").delay(2000).animate({ opacity: "hide" }, 1500);
|
30
|
+
|
31
|
+
/*
|
32
|
+
* キューに積まれた数を表示
|
33
|
+
*/
|
34
|
+
var notification = Narou.Notification.instance();
|
35
|
+
var $queue = $("#queue");
|
36
|
+
function highlightQueuBoxIcon(size) {
|
37
|
+
if (size > 0) {
|
38
|
+
$("#queue-text").addClass("active");
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
$("#queue-text").removeClass("active");
|
42
|
+
}
|
43
|
+
}
|
44
|
+
notification.on("notification.queue", function(data) {
|
45
|
+
var before_size = $queue.text();
|
46
|
+
$queue.text(data);
|
47
|
+
highlightQueuBoxIcon(data);
|
48
|
+
// キューのスタック数の横に +1 が浮かび上がって消えるアニメーション
|
49
|
+
var diff = data - before_size;
|
50
|
+
if (diff <= 0) return;
|
51
|
+
var $plus = $("<span>+" + diff + "</span>");
|
52
|
+
$plus.css({
|
53
|
+
position: "absolute",
|
54
|
+
left: $queue.offset().left + $queue.outerWidth() + 1 - $(document).scrollLeft(),
|
55
|
+
top: $queue.offset().top - 3 - $(document).scrollTop(),
|
56
|
+
color: "#5cb85c",
|
57
|
+
"font-weight": "bold",
|
58
|
+
});
|
59
|
+
$(".navbar").append($plus);
|
60
|
+
$plus.delay(100).animate({
|
61
|
+
top: - $plus.outerHeight(),
|
62
|
+
opacity: 0,
|
63
|
+
}, 3000, function() {
|
64
|
+
$(this).remove();
|
65
|
+
});
|
66
|
+
});
|
67
|
+
$.get("/api/get_queue_size", function(json) {
|
68
|
+
$queue.text(json.size);
|
69
|
+
highlightQueuBoxIcon(json.size);
|
70
|
+
});
|
71
|
+
|
72
|
+
/*************************************************************************
|
73
|
+
* Webサーバの方から入力を求められた時にモーダルを表示して返事を返す
|
74
|
+
*************************************************************************/
|
75
|
+
(function() {
|
76
|
+
var notification = Narou.Notification.instance();
|
77
|
+
var boxes = {};
|
78
|
+
|
79
|
+
// モーダル生存確認への応答
|
80
|
+
var pong = function(id) {
|
81
|
+
var hash = {};
|
82
|
+
hash["pong.modal." + id] = true
|
83
|
+
notification.send(hash);
|
84
|
+
};
|
85
|
+
|
86
|
+
// ユーザの選択をサーバに通知する
|
87
|
+
var answer = function(id, result) {
|
88
|
+
var hash = {};
|
89
|
+
hash["answer.modal." + id] = { result: result };
|
90
|
+
notification.send(hash);
|
91
|
+
};
|
92
|
+
|
93
|
+
notification.on("ping.modal", function(json) {
|
94
|
+
if (boxes[json.id]) pong(json.id);
|
95
|
+
});
|
96
|
+
|
97
|
+
// キャンセル、OK を確認する confirm モーダル表示
|
98
|
+
notification.on("modal.confirm", function(json) {
|
99
|
+
var id = json.id;
|
100
|
+
boxes[id] = bootbox.confirm(json.message, function(result) {
|
101
|
+
answer(id, result);
|
102
|
+
});
|
103
|
+
});
|
104
|
+
|
105
|
+
// 選択肢を表示する choose モーダル表示
|
106
|
+
notification.on("modal.choose", function(json) {
|
107
|
+
var id = json.id;
|
108
|
+
var message = "<div>" + json.message.replace(/\n/g, "<br>") + "</div>";
|
109
|
+
$.each(json.choices, function(key, val) {
|
110
|
+
var label_id = "choice-" + key;
|
111
|
+
message += "<div class=radio><label for='" + label_id + "'>" +
|
112
|
+
"<input type=radio name=choices id='" + label_id + "' value='" + key + "'>" + val +
|
113
|
+
"</label></div>";
|
114
|
+
});
|
115
|
+
boxes[id] = bootbox.dialog({
|
116
|
+
title: json.title,
|
117
|
+
message: message,
|
118
|
+
closeButton: false,
|
119
|
+
buttons: {
|
120
|
+
main: {
|
121
|
+
label: "決定",
|
122
|
+
className: "btn-primary",
|
123
|
+
callback: function() {
|
124
|
+
var result = $("input[name='choices']:checked").val();
|
125
|
+
answer(id, result);
|
126
|
+
}
|
127
|
+
}
|
128
|
+
}
|
129
|
+
});
|
130
|
+
});
|
131
|
+
|
132
|
+
notification.on("hide.modal", function(json) {
|
133
|
+
var box = boxes[json.id];
|
134
|
+
if (box) {
|
135
|
+
box.modal("hide");
|
136
|
+
delete boxes[json.id];
|
137
|
+
}
|
138
|
+
});
|
139
|
+
|
140
|
+
return;
|
141
|
+
})();
|
142
|
+
});
|
143
|
+
|
@@ -0,0 +1,1113 @@
|
|
1
|
+
/*! ColVis 1.1.2-dev
|
2
|
+
* ©2010-2014 SpryMedia Ltd - datatables.net/license
|
3
|
+
*/
|
4
|
+
|
5
|
+
/**
|
6
|
+
* @summary ColVis
|
7
|
+
* @description Controls for column visibility in DataTables
|
8
|
+
* @version 1.1.2-dev
|
9
|
+
* @file dataTables.colReorder.js
|
10
|
+
* @author SpryMedia Ltd (www.sprymedia.co.uk)
|
11
|
+
* @contact www.sprymedia.co.uk/contact
|
12
|
+
* @copyright Copyright 2010-2014 SpryMedia Ltd.
|
13
|
+
*
|
14
|
+
* This source file is free software, available under the following license:
|
15
|
+
* MIT license - http://datatables.net/license/mit
|
16
|
+
*
|
17
|
+
* This source file is distributed in the hope that it will be useful, but
|
18
|
+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
19
|
+
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
|
20
|
+
*
|
21
|
+
* For details please refer to: http://www.datatables.net
|
22
|
+
*/
|
23
|
+
|
24
|
+
(function(window, document, undefined) {
|
25
|
+
|
26
|
+
|
27
|
+
var factory = function( $, DataTable ) {
|
28
|
+
"use strict";
|
29
|
+
|
30
|
+
/**
|
31
|
+
* ColVis provides column visibility control for DataTables
|
32
|
+
*
|
33
|
+
* @class ColVis
|
34
|
+
* @constructor
|
35
|
+
* @param {object} DataTables settings object. With DataTables 1.10 this can
|
36
|
+
* also be and API instance, table node, jQuery collection or jQuery selector.
|
37
|
+
* @param {object} ColVis configuration options
|
38
|
+
*/
|
39
|
+
var ColVis = function( oDTSettings, oInit )
|
40
|
+
{
|
41
|
+
/* Santiy check that we are a new instance */
|
42
|
+
if ( !this.CLASS || this.CLASS != "ColVis" )
|
43
|
+
{
|
44
|
+
alert( "Warning: ColVis must be initialised with the keyword 'new'" );
|
45
|
+
}
|
46
|
+
|
47
|
+
if ( typeof oInit == 'undefined' )
|
48
|
+
{
|
49
|
+
oInit = {};
|
50
|
+
}
|
51
|
+
|
52
|
+
var camelToHungarian = $.fn.dataTable.camelToHungarian;
|
53
|
+
if ( camelToHungarian ) {
|
54
|
+
camelToHungarian( ColVis.defaults, ColVis.defaults, true );
|
55
|
+
camelToHungarian( ColVis.defaults, oInit );
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
60
|
+
* Public class variables
|
61
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
62
|
+
|
63
|
+
/**
|
64
|
+
* @namespace Settings object which contains customisable information for
|
65
|
+
* ColVis instance. Augmented by ColVis.defaults
|
66
|
+
*/
|
67
|
+
this.s = {
|
68
|
+
/**
|
69
|
+
* DataTables settings object
|
70
|
+
* @property dt
|
71
|
+
* @type Object
|
72
|
+
* @default null
|
73
|
+
*/
|
74
|
+
"dt": null,
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Customisation object
|
78
|
+
* @property oInit
|
79
|
+
* @type Object
|
80
|
+
* @default passed in
|
81
|
+
*/
|
82
|
+
"oInit": oInit,
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Flag to say if the collection is hidden
|
86
|
+
* @property hidden
|
87
|
+
* @type boolean
|
88
|
+
* @default true
|
89
|
+
*/
|
90
|
+
"hidden": true,
|
91
|
+
|
92
|
+
/**
|
93
|
+
* Store the original visibility settings so they could be restored
|
94
|
+
* @property abOriginal
|
95
|
+
* @type Array
|
96
|
+
* @default []
|
97
|
+
*/
|
98
|
+
"abOriginal": []
|
99
|
+
};
|
100
|
+
|
101
|
+
|
102
|
+
/**
|
103
|
+
* @namespace Common and useful DOM elements for the class instance
|
104
|
+
*/
|
105
|
+
this.dom = {
|
106
|
+
/**
|
107
|
+
* Wrapper for the button - given back to DataTables as the node to insert
|
108
|
+
* @property wrapper
|
109
|
+
* @type Node
|
110
|
+
* @default null
|
111
|
+
*/
|
112
|
+
"wrapper": null,
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Activation button
|
116
|
+
* @property button
|
117
|
+
* @type Node
|
118
|
+
* @default null
|
119
|
+
*/
|
120
|
+
"button": null,
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Collection list node
|
124
|
+
* @property collection
|
125
|
+
* @type Node
|
126
|
+
* @default null
|
127
|
+
*/
|
128
|
+
"collection": null,
|
129
|
+
|
130
|
+
/**
|
131
|
+
* Background node used for shading the display and event capturing
|
132
|
+
* @property background
|
133
|
+
* @type Node
|
134
|
+
* @default null
|
135
|
+
*/
|
136
|
+
"background": null,
|
137
|
+
|
138
|
+
/**
|
139
|
+
* Element to position over the activation button to catch mouse events when using mouseover
|
140
|
+
* @property catcher
|
141
|
+
* @type Node
|
142
|
+
* @default null
|
143
|
+
*/
|
144
|
+
"catcher": null,
|
145
|
+
|
146
|
+
/**
|
147
|
+
* List of button elements
|
148
|
+
* @property buttons
|
149
|
+
* @type Array
|
150
|
+
* @default []
|
151
|
+
*/
|
152
|
+
"buttons": [],
|
153
|
+
|
154
|
+
/**
|
155
|
+
* List of group button elements
|
156
|
+
* @property groupButtons
|
157
|
+
* @type Array
|
158
|
+
* @default []
|
159
|
+
*/
|
160
|
+
"groupButtons": [],
|
161
|
+
|
162
|
+
/**
|
163
|
+
* Restore button
|
164
|
+
* @property restore
|
165
|
+
* @type Node
|
166
|
+
* @default null
|
167
|
+
*/
|
168
|
+
"restore": null
|
169
|
+
};
|
170
|
+
|
171
|
+
/* Store global reference */
|
172
|
+
ColVis.aInstances.push( this );
|
173
|
+
|
174
|
+
/* Constructor logic */
|
175
|
+
this.s.dt = $.fn.dataTable.Api ?
|
176
|
+
new $.fn.dataTable.Api( oDTSettings ).settings()[0] :
|
177
|
+
oDTSettings;
|
178
|
+
|
179
|
+
this._fnConstruct( oInit );
|
180
|
+
return this;
|
181
|
+
};
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
ColVis.prototype = {
|
186
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
187
|
+
* Public methods
|
188
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
189
|
+
|
190
|
+
/**
|
191
|
+
* Get the ColVis instance's control button so it can be injected into the
|
192
|
+
* DOM
|
193
|
+
* @method button
|
194
|
+
* @returns {node} ColVis button
|
195
|
+
*/
|
196
|
+
button: function ()
|
197
|
+
{
|
198
|
+
return this.dom.wrapper;
|
199
|
+
},
|
200
|
+
|
201
|
+
/**
|
202
|
+
* Alias of `rebuild` for backwards compatibility
|
203
|
+
* @method fnRebuild
|
204
|
+
*/
|
205
|
+
"fnRebuild": function ()
|
206
|
+
{
|
207
|
+
this.rebuild();
|
208
|
+
},
|
209
|
+
|
210
|
+
/**
|
211
|
+
* Rebuild the list of buttons for this instance (i.e. if there is a column
|
212
|
+
* header update)
|
213
|
+
* @method fnRebuild
|
214
|
+
*/
|
215
|
+
rebuild: function ()
|
216
|
+
{
|
217
|
+
/* Remove the old buttons */
|
218
|
+
for ( var i=this.dom.buttons.length-1 ; i>=0 ; i-- ) {
|
219
|
+
this.dom.collection.removeChild( this.dom.buttons[i] );
|
220
|
+
}
|
221
|
+
this.dom.buttons.splice( 0, this.dom.buttons.length );
|
222
|
+
|
223
|
+
if ( this.dom.restore ) {
|
224
|
+
this.dom.restore.parentNode( this.dom.restore );
|
225
|
+
}
|
226
|
+
|
227
|
+
/* Re-add them (this is not the optimal way of doing this, it is fast and effective) */
|
228
|
+
this._fnAddGroups();
|
229
|
+
this._fnAddButtons();
|
230
|
+
|
231
|
+
/* Update the checkboxes */
|
232
|
+
this._fnDrawCallback();
|
233
|
+
},
|
234
|
+
|
235
|
+
|
236
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
237
|
+
* Private methods (they are of course public in JS, but recommended as private)
|
238
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Constructor logic
|
242
|
+
* @method _fnConstruct
|
243
|
+
* @returns void
|
244
|
+
* @private
|
245
|
+
*/
|
246
|
+
"_fnConstruct": function ( init )
|
247
|
+
{
|
248
|
+
this._fnApplyCustomisation( init );
|
249
|
+
|
250
|
+
var that = this;
|
251
|
+
var i, iLen;
|
252
|
+
this.dom.wrapper = document.createElement('div');
|
253
|
+
this.dom.wrapper.className = "ColVis";
|
254
|
+
|
255
|
+
this.dom.button = $( '<button />', {
|
256
|
+
'class': !this.s.dt.bJUI ?
|
257
|
+
"ColVis_Button ColVis_MasterButton" :
|
258
|
+
"ColVis_Button ColVis_MasterButton ui-button ui-state-default"
|
259
|
+
} )
|
260
|
+
.append( '<span>'+this.s.buttonText+'</span>' )
|
261
|
+
.bind( this.s.activate=="mouseover" ? "mouseover" : "click", function (e) {
|
262
|
+
e.preventDefault();
|
263
|
+
that._fnCollectionShow();
|
264
|
+
} )
|
265
|
+
.appendTo( this.dom.wrapper )[0];
|
266
|
+
|
267
|
+
this.dom.catcher = this._fnDomCatcher();
|
268
|
+
this.dom.collection = this._fnDomCollection();
|
269
|
+
this.dom.background = this._fnDomBackground();
|
270
|
+
|
271
|
+
this._fnAddGroups();
|
272
|
+
this._fnAddButtons();
|
273
|
+
|
274
|
+
/* Store the original visibility information */
|
275
|
+
for ( i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
|
276
|
+
{
|
277
|
+
this.s.abOriginal.push( this.s.dt.aoColumns[i].bVisible );
|
278
|
+
}
|
279
|
+
|
280
|
+
/* Update on each draw */
|
281
|
+
this.s.dt.aoDrawCallback.push( {
|
282
|
+
"fn": function () {
|
283
|
+
that._fnDrawCallback.call( that );
|
284
|
+
},
|
285
|
+
"sName": "ColVis"
|
286
|
+
} );
|
287
|
+
|
288
|
+
/* If columns are reordered, then we need to update our exclude list and
|
289
|
+
* rebuild the displayed list
|
290
|
+
*/
|
291
|
+
$(this.s.dt.oInstance).bind( 'column-reorder', function ( e, oSettings, oReorder ) {
|
292
|
+
for ( i=0, iLen=that.s.aiExclude.length ; i<iLen ; i++ ) {
|
293
|
+
var reorderd_index = oReorder.aiInvertMapping[ that.s.aiExclude[i] ];
|
294
|
+
that.s.aiExclude[i] = reorderd_index ? reorderd_index : that.s.aiExclude[i];
|
295
|
+
}
|
296
|
+
|
297
|
+
var mStore = that.s.abOriginal.splice( oReorder.iFrom, 1 )[0];
|
298
|
+
that.s.abOriginal.splice( oReorder.iTo, 0, mStore );
|
299
|
+
|
300
|
+
that.fnRebuild();
|
301
|
+
} );
|
302
|
+
|
303
|
+
// Set the initial state
|
304
|
+
this._fnDrawCallback();
|
305
|
+
},
|
306
|
+
|
307
|
+
|
308
|
+
/**
|
309
|
+
* Apply any customisation to the settings from the DataTables initialisation
|
310
|
+
* @method _fnApplyCustomisation
|
311
|
+
* @returns void
|
312
|
+
* @private
|
313
|
+
*/
|
314
|
+
"_fnApplyCustomisation": function ( init )
|
315
|
+
{
|
316
|
+
$.extend( true, this.s, ColVis.defaults, init );
|
317
|
+
|
318
|
+
// Slightly messy overlap for the camelCase notation
|
319
|
+
if ( ! this.s.showAll && this.s.bShowAll ) {
|
320
|
+
this.s.showAll = this.s.sShowAll;
|
321
|
+
}
|
322
|
+
|
323
|
+
if ( ! this.s.restore && this.s.bRestore ) {
|
324
|
+
this.s.restore = this.s.sRestore;
|
325
|
+
}
|
326
|
+
|
327
|
+
// CamelCase to Hungarian for the column groups
|
328
|
+
var groups = this.s.groups;
|
329
|
+
var hungarianGroups = this.s.aoGroups;
|
330
|
+
if ( groups ) {
|
331
|
+
for ( var i=0, ien=groups.length ; i<ien ; i++ ) {
|
332
|
+
if ( groups[i].title ) {
|
333
|
+
hungarianGroups[i].sTitle = groups[i].title;
|
334
|
+
}
|
335
|
+
if ( groups[i].columns ) {
|
336
|
+
hungarianGroups[i].aiColumns = groups[i].columns;
|
337
|
+
}
|
338
|
+
}
|
339
|
+
}
|
340
|
+
},
|
341
|
+
|
342
|
+
|
343
|
+
/**
|
344
|
+
* On each table draw, check the visibility checkboxes as needed. This allows any process to
|
345
|
+
* update the table's column visibility and ColVis will still be accurate.
|
346
|
+
* @method _fnDrawCallback
|
347
|
+
* @returns void
|
348
|
+
* @private
|
349
|
+
*/
|
350
|
+
"_fnDrawCallback": function ()
|
351
|
+
{
|
352
|
+
var columns = this.s.dt.aoColumns;
|
353
|
+
var buttons = this.dom.buttons;
|
354
|
+
var groups = this.s.aoGroups;
|
355
|
+
var button;
|
356
|
+
|
357
|
+
for ( var i=0, ien=buttons.length ; i<ien ; i++ ) {
|
358
|
+
button = buttons[i];
|
359
|
+
|
360
|
+
if ( button.__columnIdx !== undefined ) {
|
361
|
+
$('input', button).prop( 'checked', columns[ button.__columnIdx ].bVisible );
|
362
|
+
}
|
363
|
+
}
|
364
|
+
|
365
|
+
var allVisible = function ( columnIndeces ) {
|
366
|
+
for ( var k=0, kLen=columnIndeces.length ; k<kLen ; k++ )
|
367
|
+
{
|
368
|
+
if ( columns[columnIndeces[k]].bVisible === false ) { return false; }
|
369
|
+
}
|
370
|
+
return true;
|
371
|
+
};
|
372
|
+
var allHidden = function ( columnIndeces ) {
|
373
|
+
for ( var m=0 , mLen=columnIndeces.length ; m<mLen ; m++ )
|
374
|
+
{
|
375
|
+
if ( columns[columnIndeces[m]].bVisible === true ) { return false; }
|
376
|
+
}
|
377
|
+
return true;
|
378
|
+
};
|
379
|
+
|
380
|
+
for ( var j=0, jLen=groups.length ; j<jLen ; j++ )
|
381
|
+
{
|
382
|
+
if ( allVisible(groups[j].aiColumns) )
|
383
|
+
{
|
384
|
+
$('input', this.dom.groupButtons[j]).prop('checked', true);
|
385
|
+
$('input', this.dom.groupButtons[j]).prop('indeterminate', false);
|
386
|
+
}
|
387
|
+
else if ( allHidden(groups[j].aiColumns) )
|
388
|
+
{
|
389
|
+
$('input', this.dom.groupButtons[j]).prop('checked', false);
|
390
|
+
$('input', this.dom.groupButtons[j]).prop('indeterminate', false);
|
391
|
+
}
|
392
|
+
else
|
393
|
+
{
|
394
|
+
$('input', this.dom.groupButtons[j]).prop('indeterminate', true);
|
395
|
+
}
|
396
|
+
}
|
397
|
+
},
|
398
|
+
|
399
|
+
|
400
|
+
/**
|
401
|
+
* Loop through the groups (provided in the settings) and create a button for each.
|
402
|
+
* @method _fnAddgroups
|
403
|
+
* @returns void
|
404
|
+
* @private
|
405
|
+
*/
|
406
|
+
"_fnAddGroups": function ()
|
407
|
+
{
|
408
|
+
var nButton;
|
409
|
+
|
410
|
+
if ( typeof this.s.aoGroups != 'undefined' )
|
411
|
+
{
|
412
|
+
for ( var i=0, iLen=this.s.aoGroups.length ; i<iLen ; i++ )
|
413
|
+
{
|
414
|
+
nButton = this._fnDomGroupButton( i );
|
415
|
+
this.dom.groupButtons.push( nButton );
|
416
|
+
this.dom.buttons.push( nButton );
|
417
|
+
this.dom.collection.appendChild( nButton );
|
418
|
+
}
|
419
|
+
}
|
420
|
+
},
|
421
|
+
|
422
|
+
|
423
|
+
/**
|
424
|
+
* Loop through the columns in the table and as a new button for each one.
|
425
|
+
* @method _fnAddButtons
|
426
|
+
* @returns void
|
427
|
+
* @private
|
428
|
+
*/
|
429
|
+
"_fnAddButtons": function ()
|
430
|
+
{
|
431
|
+
var
|
432
|
+
nButton,
|
433
|
+
columns = this.s.dt.aoColumns;
|
434
|
+
|
435
|
+
if ( $.inArray( 'all', this.s.aiExclude ) === -1 ) {
|
436
|
+
for ( var i=0, iLen=columns.length ; i<iLen ; i++ )
|
437
|
+
{
|
438
|
+
if ( $.inArray( i, this.s.aiExclude ) === -1 &&
|
439
|
+
$.inArray( columns[i].data, this.s.aiExclude ) === -1 )
|
440
|
+
{
|
441
|
+
nButton = this._fnDomColumnButton( i );
|
442
|
+
nButton.__columnIdx = i;
|
443
|
+
this.dom.buttons.push( nButton );
|
444
|
+
}
|
445
|
+
}
|
446
|
+
}
|
447
|
+
|
448
|
+
if ( this.s.order === 'alpha' ) {
|
449
|
+
this.dom.buttons.sort( function ( a, b ) {
|
450
|
+
var titleA = columns[ a.__columnIdx ].sTitle;
|
451
|
+
var titleB = columns[ b.__columnIdx ].sTitle;
|
452
|
+
|
453
|
+
return titleA === titleB ?
|
454
|
+
0 :
|
455
|
+
titleA < titleB ?
|
456
|
+
-1 :
|
457
|
+
1;
|
458
|
+
} );
|
459
|
+
}
|
460
|
+
|
461
|
+
if ( this.s.restore )
|
462
|
+
{
|
463
|
+
nButton = this._fnDomRestoreButton();
|
464
|
+
nButton.className += " ColVis_Restore";
|
465
|
+
this.dom.buttons.push( nButton );
|
466
|
+
}
|
467
|
+
|
468
|
+
if ( this.s.showAll )
|
469
|
+
{
|
470
|
+
nButton = this._fnDomShowXButton( this.s.showAll, true );
|
471
|
+
nButton.className += " ColVis_ShowAll";
|
472
|
+
this.dom.buttons.push( nButton );
|
473
|
+
}
|
474
|
+
|
475
|
+
if ( this.s.showNone )
|
476
|
+
{
|
477
|
+
nButton = this._fnDomShowXButton( this.s.showNone, false );
|
478
|
+
nButton.className += " ColVis_ShowNone";
|
479
|
+
this.dom.buttons.push( nButton );
|
480
|
+
}
|
481
|
+
|
482
|
+
$(this.dom.collection).append( this.dom.buttons );
|
483
|
+
},
|
484
|
+
|
485
|
+
|
486
|
+
/**
|
487
|
+
* Create a button which allows a "restore" action
|
488
|
+
* @method _fnDomRestoreButton
|
489
|
+
* @returns {Node} Created button
|
490
|
+
* @private
|
491
|
+
*/
|
492
|
+
"_fnDomRestoreButton": function ()
|
493
|
+
{
|
494
|
+
var
|
495
|
+
that = this,
|
496
|
+
dt = this.s.dt;
|
497
|
+
|
498
|
+
return $(
|
499
|
+
'<li class="ColVis_Special '+(dt.bJUI ? 'ui-button ui-state-default' : '')+'">'+
|
500
|
+
this.s.restore+
|
501
|
+
'</li>'
|
502
|
+
)
|
503
|
+
.click( function (e) {
|
504
|
+
for ( var i=0, iLen=that.s.abOriginal.length ; i<iLen ; i++ )
|
505
|
+
{
|
506
|
+
that.s.dt.oInstance.fnSetColumnVis( i, that.s.abOriginal[i], false );
|
507
|
+
}
|
508
|
+
that._fnAdjustOpenRows();
|
509
|
+
that.s.dt.oInstance.fnAdjustColumnSizing( false );
|
510
|
+
that.s.dt.oInstance.fnDraw( false );
|
511
|
+
} )[0];
|
512
|
+
},
|
513
|
+
|
514
|
+
|
515
|
+
/**
|
516
|
+
* Create a button which allows show all and show node actions
|
517
|
+
* @method _fnDomShowXButton
|
518
|
+
* @returns {Node} Created button
|
519
|
+
* @private
|
520
|
+
*/
|
521
|
+
"_fnDomShowXButton": function ( str, action )
|
522
|
+
{
|
523
|
+
var
|
524
|
+
that = this,
|
525
|
+
dt = this.s.dt,
|
526
|
+
columns = dt.aoColumns;
|
527
|
+
|
528
|
+
return $(
|
529
|
+
'<li class="ColVis_Special '+(dt.bJUI ? 'ui-button ui-state-default' : '')+'">'+
|
530
|
+
str+
|
531
|
+
'</li>'
|
532
|
+
)
|
533
|
+
.click( function (e) {
|
534
|
+
for ( var i=0, iLen=that.s.abOriginal.length ; i<iLen ; i++ )
|
535
|
+
{
|
536
|
+
if (that.s.aiExclude.indexOf(i) === -1 &&
|
537
|
+
$.inArray(columns[i].data, that.s.aiExclude) === -1)
|
538
|
+
{
|
539
|
+
that.s.dt.oInstance.fnSetColumnVis( i, action, false );
|
540
|
+
}
|
541
|
+
}
|
542
|
+
that._fnAdjustOpenRows();
|
543
|
+
that.s.dt.oInstance.fnAdjustColumnSizing( false );
|
544
|
+
that.s.dt.oInstance.fnDraw( false );
|
545
|
+
} )[0];
|
546
|
+
},
|
547
|
+
|
548
|
+
|
549
|
+
/**
|
550
|
+
* Create the DOM for a show / hide group button
|
551
|
+
* @method _fnDomGroupButton
|
552
|
+
* @param {int} i Group in question, order based on that provided in settings
|
553
|
+
* @returns {Node} Created button
|
554
|
+
* @private
|
555
|
+
*/
|
556
|
+
"_fnDomGroupButton": function ( i )
|
557
|
+
{
|
558
|
+
var
|
559
|
+
that = this,
|
560
|
+
dt = this.s.dt,
|
561
|
+
oGroup = this.s.aoGroups[i];
|
562
|
+
|
563
|
+
return $(
|
564
|
+
'<li class="ColVis_Special '+(dt.bJUI ? 'ui-button ui-state-default' : '')+'">'+
|
565
|
+
'<label>'+
|
566
|
+
'<input type="checkbox" />'+
|
567
|
+
'<span>'+oGroup.sTitle+'</span>'+
|
568
|
+
'</label>'+
|
569
|
+
'</li>'
|
570
|
+
)
|
571
|
+
.click( function (e) {
|
572
|
+
var showHide = !$('input', this).is(":checked");
|
573
|
+
if ( e.target.nodeName.toLowerCase() !== "li" )
|
574
|
+
{
|
575
|
+
showHide = ! showHide;
|
576
|
+
}
|
577
|
+
|
578
|
+
for ( var j=0 ; j < oGroup.aiColumns.length ; j++ )
|
579
|
+
{
|
580
|
+
that.s.dt.oInstance.fnSetColumnVis( oGroup.aiColumns[j], showHide );
|
581
|
+
}
|
582
|
+
} )[0];
|
583
|
+
},
|
584
|
+
|
585
|
+
|
586
|
+
/**
|
587
|
+
* Create the DOM for a show / hide button
|
588
|
+
* @method _fnDomColumnButton
|
589
|
+
* @param {int} i Column in question
|
590
|
+
* @returns {Node} Created button
|
591
|
+
* @private
|
592
|
+
*/
|
593
|
+
"_fnDomColumnButton": function ( i )
|
594
|
+
{
|
595
|
+
var
|
596
|
+
that = this,
|
597
|
+
column = this.s.dt.aoColumns[i],
|
598
|
+
dt = this.s.dt;
|
599
|
+
|
600
|
+
var title = this.s.fnLabel===null ?
|
601
|
+
column.sTitle :
|
602
|
+
this.s.fnLabel( i, column.sTitle, column.nTh );
|
603
|
+
|
604
|
+
return $(
|
605
|
+
'<li '+(dt.bJUI ? 'class="ui-button ui-state-default"' : '')+'>'+
|
606
|
+
'<label>'+
|
607
|
+
'<input type="checkbox" />'+
|
608
|
+
'<span>'+title+'</span>'+
|
609
|
+
'</label>'+
|
610
|
+
'</li>'
|
611
|
+
)
|
612
|
+
.click( function (e) {
|
613
|
+
var showHide = !$('input', this).is(":checked");
|
614
|
+
if ( e.target.nodeName.toLowerCase() !== "li" )
|
615
|
+
{
|
616
|
+
showHide = ! showHide;
|
617
|
+
}
|
618
|
+
|
619
|
+
/* Need to consider the case where the initialiser created more than one table - change the
|
620
|
+
* API index that DataTables is using
|
621
|
+
*/
|
622
|
+
var oldIndex = $.fn.dataTableExt.iApiIndex;
|
623
|
+
$.fn.dataTableExt.iApiIndex = that._fnDataTablesApiIndex.call(that);
|
624
|
+
|
625
|
+
// Optimisation for server-side processing when scrolling - don't do a full redraw
|
626
|
+
if ( dt.oFeatures.bServerSide )
|
627
|
+
{
|
628
|
+
that.s.dt.oInstance.fnSetColumnVis( i, showHide, false );
|
629
|
+
that.s.dt.oInstance.fnAdjustColumnSizing( false );
|
630
|
+
if (dt.oScroll.sX !== "" || dt.oScroll.sY !== "" )
|
631
|
+
{
|
632
|
+
that.s.dt.oInstance.oApi._fnScrollDraw( that.s.dt );
|
633
|
+
}
|
634
|
+
that._fnDrawCallback();
|
635
|
+
}
|
636
|
+
else
|
637
|
+
{
|
638
|
+
that.s.dt.oInstance.fnSetColumnVis( i, showHide );
|
639
|
+
}
|
640
|
+
|
641
|
+
$.fn.dataTableExt.iApiIndex = oldIndex; /* Restore */
|
642
|
+
|
643
|
+
if ( e.target.nodeName.toLowerCase() === 'input' && that.s.fnStateChange !== null )
|
644
|
+
{
|
645
|
+
that.s.fnStateChange.call( that, i, showHide );
|
646
|
+
}
|
647
|
+
} )[0];
|
648
|
+
},
|
649
|
+
|
650
|
+
|
651
|
+
/**
|
652
|
+
* Get the position in the DataTables instance array of the table for this
|
653
|
+
* instance of ColVis
|
654
|
+
* @method _fnDataTablesApiIndex
|
655
|
+
* @returns {int} Index
|
656
|
+
* @private
|
657
|
+
*/
|
658
|
+
"_fnDataTablesApiIndex": function ()
|
659
|
+
{
|
660
|
+
for ( var i=0, iLen=this.s.dt.oInstance.length ; i<iLen ; i++ )
|
661
|
+
{
|
662
|
+
if ( this.s.dt.oInstance[i] == this.s.dt.nTable )
|
663
|
+
{
|
664
|
+
return i;
|
665
|
+
}
|
666
|
+
}
|
667
|
+
return 0;
|
668
|
+
},
|
669
|
+
|
670
|
+
|
671
|
+
/**
|
672
|
+
* Create the element used to contain list the columns (it is shown and
|
673
|
+
* hidden as needed)
|
674
|
+
* @method _fnDomCollection
|
675
|
+
* @returns {Node} div container for the collection
|
676
|
+
* @private
|
677
|
+
*/
|
678
|
+
"_fnDomCollection": function ()
|
679
|
+
{
|
680
|
+
return $('<ul />', {
|
681
|
+
'class': !this.s.dt.bJUI ?
|
682
|
+
"ColVis_collection" :
|
683
|
+
"ColVis_collection ui-buttonset ui-buttonset-multi"
|
684
|
+
} )
|
685
|
+
.css( {
|
686
|
+
'display': 'none',
|
687
|
+
'opacity': 0,
|
688
|
+
'position': ! this.s.bCssPosition ?
|
689
|
+
'absolute' :
|
690
|
+
''
|
691
|
+
} )[0];
|
692
|
+
},
|
693
|
+
|
694
|
+
|
695
|
+
/**
|
696
|
+
* An element to be placed on top of the activate button to catch events
|
697
|
+
* @method _fnDomCatcher
|
698
|
+
* @returns {Node} div container for the collection
|
699
|
+
* @private
|
700
|
+
*/
|
701
|
+
"_fnDomCatcher": function ()
|
702
|
+
{
|
703
|
+
var
|
704
|
+
that = this,
|
705
|
+
nCatcher = document.createElement('div');
|
706
|
+
nCatcher.className = "ColVis_catcher";
|
707
|
+
|
708
|
+
$(nCatcher).click( function () {
|
709
|
+
that._fnCollectionHide.call( that, null, null );
|
710
|
+
} );
|
711
|
+
|
712
|
+
return nCatcher;
|
713
|
+
},
|
714
|
+
|
715
|
+
|
716
|
+
/**
|
717
|
+
* Create the element used to shade the background, and capture hide events (it is shown and
|
718
|
+
* hidden as needed)
|
719
|
+
* @method _fnDomBackground
|
720
|
+
* @returns {Node} div container for the background
|
721
|
+
* @private
|
722
|
+
*/
|
723
|
+
"_fnDomBackground": function ()
|
724
|
+
{
|
725
|
+
var that = this;
|
726
|
+
|
727
|
+
var background = $('<div></div>')
|
728
|
+
.addClass( 'ColVis_collectionBackground' )
|
729
|
+
.css( 'opacity', 0 )
|
730
|
+
.click( function () {
|
731
|
+
that._fnCollectionHide.call( that, null, null );
|
732
|
+
} );
|
733
|
+
|
734
|
+
/* When considering a mouse over action for the activation, we also consider a mouse out
|
735
|
+
* which is the same as a mouse over the background - without all the messing around of
|
736
|
+
* bubbling events. Use the catcher element to avoid messing around with bubbling
|
737
|
+
*/
|
738
|
+
if ( this.s.activate == "mouseover" )
|
739
|
+
{
|
740
|
+
background.mouseover( function () {
|
741
|
+
that.s.overcollection = false;
|
742
|
+
that._fnCollectionHide.call( that, null, null );
|
743
|
+
} );
|
744
|
+
}
|
745
|
+
|
746
|
+
return background[0];
|
747
|
+
},
|
748
|
+
|
749
|
+
|
750
|
+
/**
|
751
|
+
* Show the show / hide list and the background
|
752
|
+
* @method _fnCollectionShow
|
753
|
+
* @returns void
|
754
|
+
* @private
|
755
|
+
*/
|
756
|
+
"_fnCollectionShow": function ()
|
757
|
+
{
|
758
|
+
var that = this, i, iLen, iLeft;
|
759
|
+
var oPos = $(this.dom.button).offset();
|
760
|
+
var nHidden = this.dom.collection;
|
761
|
+
var nBackground = this.dom.background;
|
762
|
+
var iDivX = parseInt(oPos.left, 10);
|
763
|
+
var iDivY = parseInt(oPos.top + $(this.dom.button).outerHeight(), 10);
|
764
|
+
|
765
|
+
if ( ! this.s.bCssPosition )
|
766
|
+
{
|
767
|
+
nHidden.style.top = iDivY+"px";
|
768
|
+
nHidden.style.left = iDivX+"px";
|
769
|
+
}
|
770
|
+
|
771
|
+
$(nHidden).css( {
|
772
|
+
'display': 'block',
|
773
|
+
'opacity': 0
|
774
|
+
} );
|
775
|
+
|
776
|
+
nBackground.style.bottom ='0px';
|
777
|
+
nBackground.style.right = '0px';
|
778
|
+
|
779
|
+
var oStyle = this.dom.catcher.style;
|
780
|
+
oStyle.height = $(this.dom.button).outerHeight()+"px";
|
781
|
+
oStyle.width = $(this.dom.button).outerWidth()+"px";
|
782
|
+
oStyle.top = oPos.top+"px";
|
783
|
+
oStyle.left = iDivX+"px";
|
784
|
+
|
785
|
+
document.body.appendChild( nBackground );
|
786
|
+
document.body.appendChild( nHidden );
|
787
|
+
document.body.appendChild( this.dom.catcher );
|
788
|
+
|
789
|
+
/* This results in a very small delay for the end user but it allows the animation to be
|
790
|
+
* much smoother. If you don't want the animation, then the setTimeout can be removed
|
791
|
+
*/
|
792
|
+
$(nHidden).animate({"opacity": 1}, that.s.iOverlayFade);
|
793
|
+
$(nBackground).animate({"opacity": 0.45}, that.s.iOverlayFade, 'linear', function () {
|
794
|
+
/* In IE6 if you set the checked attribute of a hidden checkbox, then this is not visually
|
795
|
+
* reflected. As such, we need to do it here, once it is visible. Unbelievable.
|
796
|
+
*/
|
797
|
+
if ( $.browser && $.browser.msie && $.browser.version == "6.0" )
|
798
|
+
{
|
799
|
+
that._fnDrawCallback();
|
800
|
+
}
|
801
|
+
});
|
802
|
+
|
803
|
+
/* Visual corrections to try and keep the collection visible */
|
804
|
+
if ( !this.s.bCssPosition )
|
805
|
+
{
|
806
|
+
iLeft = ( this.s.sAlign=="left" ) ?
|
807
|
+
iDivX :
|
808
|
+
iDivX - $(nHidden).outerWidth() + $(this.dom.button).outerWidth();
|
809
|
+
|
810
|
+
nHidden.style.left = iLeft+"px";
|
811
|
+
|
812
|
+
var iDivWidth = $(nHidden).outerWidth();
|
813
|
+
var iDivHeight = $(nHidden).outerHeight();
|
814
|
+
var iDocWidth = $(document).width();
|
815
|
+
|
816
|
+
if ( iLeft + iDivWidth > iDocWidth )
|
817
|
+
{
|
818
|
+
nHidden.style.left = (iDocWidth-iDivWidth)+"px";
|
819
|
+
}
|
820
|
+
}
|
821
|
+
|
822
|
+
this.s.hidden = false;
|
823
|
+
},
|
824
|
+
|
825
|
+
|
826
|
+
/**
|
827
|
+
* Hide the show / hide list and the background
|
828
|
+
* @method _fnCollectionHide
|
829
|
+
* @returns void
|
830
|
+
* @private
|
831
|
+
*/
|
832
|
+
"_fnCollectionHide": function ( )
|
833
|
+
{
|
834
|
+
var that = this;
|
835
|
+
|
836
|
+
if ( !this.s.hidden && this.dom.collection !== null )
|
837
|
+
{
|
838
|
+
this.s.hidden = true;
|
839
|
+
|
840
|
+
$(this.dom.collection).animate({"opacity": 0}, that.s.iOverlayFade, function (e) {
|
841
|
+
this.style.display = "none";
|
842
|
+
} );
|
843
|
+
|
844
|
+
$(this.dom.background).animate({"opacity": 0}, that.s.iOverlayFade, function (e) {
|
845
|
+
document.body.removeChild( that.dom.background );
|
846
|
+
document.body.removeChild( that.dom.catcher );
|
847
|
+
} );
|
848
|
+
}
|
849
|
+
},
|
850
|
+
|
851
|
+
|
852
|
+
/**
|
853
|
+
* Alter the colspan on any fnOpen rows
|
854
|
+
*/
|
855
|
+
"_fnAdjustOpenRows": function ()
|
856
|
+
{
|
857
|
+
var aoOpen = this.s.dt.aoOpenRows;
|
858
|
+
var iVisible = this.s.dt.oApi._fnVisbleColumns( this.s.dt );
|
859
|
+
|
860
|
+
for ( var i=0, iLen=aoOpen.length ; i<iLen ; i++ ) {
|
861
|
+
aoOpen[i].nTr.getElementsByTagName('td')[0].colSpan = iVisible;
|
862
|
+
}
|
863
|
+
}
|
864
|
+
};
|
865
|
+
|
866
|
+
|
867
|
+
|
868
|
+
|
869
|
+
|
870
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
871
|
+
* Static object methods
|
872
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
873
|
+
|
874
|
+
/**
|
875
|
+
* Rebuild the collection for a given table, or all tables if no parameter given
|
876
|
+
* @method ColVis.fnRebuild
|
877
|
+
* @static
|
878
|
+
* @param object oTable DataTable instance to consider - optional
|
879
|
+
* @returns void
|
880
|
+
*/
|
881
|
+
ColVis.fnRebuild = function ( oTable )
|
882
|
+
{
|
883
|
+
var nTable = null;
|
884
|
+
if ( typeof oTable != 'undefined' )
|
885
|
+
{
|
886
|
+
nTable = oTable.fnSettings().nTable;
|
887
|
+
}
|
888
|
+
|
889
|
+
for ( var i=0, iLen=ColVis.aInstances.length ; i<iLen ; i++ )
|
890
|
+
{
|
891
|
+
if ( typeof oTable == 'undefined' || nTable == ColVis.aInstances[i].s.dt.nTable )
|
892
|
+
{
|
893
|
+
ColVis.aInstances[i].fnRebuild();
|
894
|
+
}
|
895
|
+
}
|
896
|
+
};
|
897
|
+
|
898
|
+
|
899
|
+
ColVis.defaults = {
|
900
|
+
/**
|
901
|
+
* Mode of activation. Can be 'click' or 'mouseover'
|
902
|
+
* @property activate
|
903
|
+
* @type string
|
904
|
+
* @default click
|
905
|
+
*/
|
906
|
+
active: 'click',
|
907
|
+
|
908
|
+
/**
|
909
|
+
* Text used for the button
|
910
|
+
* @property buttonText
|
911
|
+
* @type string
|
912
|
+
* @default Show / hide columns
|
913
|
+
*/
|
914
|
+
buttonText: 'Show / hide columns',
|
915
|
+
|
916
|
+
/**
|
917
|
+
* List of columns (integers) which should be excluded from the list
|
918
|
+
* @property aiExclude
|
919
|
+
* @type array
|
920
|
+
* @default []
|
921
|
+
*/
|
922
|
+
aiExclude: [],
|
923
|
+
|
924
|
+
/**
|
925
|
+
* Show restore button
|
926
|
+
* @property bRestore
|
927
|
+
* @type boolean
|
928
|
+
* @default false
|
929
|
+
*/
|
930
|
+
bRestore: false,
|
931
|
+
|
932
|
+
/**
|
933
|
+
* Restore button text
|
934
|
+
* @property sRestore
|
935
|
+
* @type string
|
936
|
+
* @default Restore original
|
937
|
+
*/
|
938
|
+
sRestore: 'Restore original',
|
939
|
+
|
940
|
+
/**
|
941
|
+
* Show Show-All button
|
942
|
+
* @property bShowAll
|
943
|
+
* @type boolean
|
944
|
+
* @default false
|
945
|
+
*/
|
946
|
+
bShowAll: false,
|
947
|
+
|
948
|
+
/**
|
949
|
+
* Show All button text
|
950
|
+
* @property sShowAll
|
951
|
+
* @type string
|
952
|
+
* @default Restore original
|
953
|
+
*/
|
954
|
+
sShowAll: 'Show All',
|
955
|
+
|
956
|
+
/**
|
957
|
+
* Position of the collection menu when shown - align "left" or "right"
|
958
|
+
* @property sAlign
|
959
|
+
* @type string
|
960
|
+
* @default left
|
961
|
+
*/
|
962
|
+
sAlign: 'left',
|
963
|
+
|
964
|
+
/**
|
965
|
+
* Callback function to tell the user when the state has changed
|
966
|
+
* @property fnStateChange
|
967
|
+
* @type function
|
968
|
+
* @default null
|
969
|
+
*/
|
970
|
+
fnStateChange: null,
|
971
|
+
|
972
|
+
/**
|
973
|
+
* Overlay animation duration in mS
|
974
|
+
* @property iOverlayFade
|
975
|
+
* @type integer|false
|
976
|
+
* @default 500
|
977
|
+
*/
|
978
|
+
iOverlayFade: 500,
|
979
|
+
|
980
|
+
/**
|
981
|
+
* Label callback for column names. Takes three parameters: 1. the
|
982
|
+
* column index, 2. the column title detected by DataTables and 3. the
|
983
|
+
* TH node for the column
|
984
|
+
* @property fnLabel
|
985
|
+
* @type function
|
986
|
+
* @default null
|
987
|
+
*/
|
988
|
+
fnLabel: null,
|
989
|
+
|
990
|
+
/**
|
991
|
+
* Indicate if the column list should be positioned by Javascript,
|
992
|
+
* visually below the button or allow CSS to do the positioning
|
993
|
+
* @property bCssPosition
|
994
|
+
* @type boolean
|
995
|
+
* @default false
|
996
|
+
*/
|
997
|
+
bCssPosition: false,
|
998
|
+
|
999
|
+
/**
|
1000
|
+
* Group buttons
|
1001
|
+
* @property aoGroups
|
1002
|
+
* @type array
|
1003
|
+
* @default []
|
1004
|
+
*/
|
1005
|
+
aoGroups: [],
|
1006
|
+
|
1007
|
+
/**
|
1008
|
+
* Button ordering - 'alpha' (alphabetical) or 'column' (table column
|
1009
|
+
* order)
|
1010
|
+
* @property order
|
1011
|
+
* @type string
|
1012
|
+
* @default column
|
1013
|
+
*/
|
1014
|
+
order: 'column'
|
1015
|
+
};
|
1016
|
+
|
1017
|
+
|
1018
|
+
|
1019
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
1020
|
+
* Static object properties
|
1021
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
1022
|
+
|
1023
|
+
/**
|
1024
|
+
* Collection of all ColVis instances
|
1025
|
+
* @property ColVis.aInstances
|
1026
|
+
* @static
|
1027
|
+
* @type Array
|
1028
|
+
* @default []
|
1029
|
+
*/
|
1030
|
+
ColVis.aInstances = [];
|
1031
|
+
|
1032
|
+
|
1033
|
+
|
1034
|
+
|
1035
|
+
|
1036
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
1037
|
+
* Constants
|
1038
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
1039
|
+
|
1040
|
+
/**
|
1041
|
+
* Name of this class
|
1042
|
+
* @constant CLASS
|
1043
|
+
* @type String
|
1044
|
+
* @default ColVis
|
1045
|
+
*/
|
1046
|
+
ColVis.prototype.CLASS = "ColVis";
|
1047
|
+
|
1048
|
+
|
1049
|
+
/**
|
1050
|
+
* ColVis version
|
1051
|
+
* @constant VERSION
|
1052
|
+
* @type String
|
1053
|
+
* @default See code
|
1054
|
+
*/
|
1055
|
+
ColVis.VERSION = "1.1.2-dev";
|
1056
|
+
ColVis.prototype.VERSION = ColVis.VERSION;
|
1057
|
+
|
1058
|
+
|
1059
|
+
|
1060
|
+
|
1061
|
+
|
1062
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
1063
|
+
* Initialisation
|
1064
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
1065
|
+
|
1066
|
+
/*
|
1067
|
+
* Register a new feature with DataTables
|
1068
|
+
*/
|
1069
|
+
if ( typeof $.fn.dataTable == "function" &&
|
1070
|
+
typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
|
1071
|
+
$.fn.dataTableExt.fnVersionCheck('1.7.0') )
|
1072
|
+
{
|
1073
|
+
$.fn.dataTableExt.aoFeatures.push( {
|
1074
|
+
"fnInit": function( oDTSettings ) {
|
1075
|
+
var init = oDTSettings.oInit;
|
1076
|
+
var colvis = new ColVis( oDTSettings, init.colVis || init.oColVis || {} );
|
1077
|
+
return colvis.button();
|
1078
|
+
},
|
1079
|
+
"cFeature": "C",
|
1080
|
+
"sFeature": "ColVis"
|
1081
|
+
} );
|
1082
|
+
}
|
1083
|
+
else
|
1084
|
+
{
|
1085
|
+
alert( "Warning: ColVis requires DataTables 1.7 or greater - www.datatables.net/download");
|
1086
|
+
}
|
1087
|
+
|
1088
|
+
|
1089
|
+
// Make ColVis accessible from the DataTables instance
|
1090
|
+
$.fn.dataTable.ColVis = ColVis;
|
1091
|
+
$.fn.DataTable.ColVis = ColVis;
|
1092
|
+
|
1093
|
+
|
1094
|
+
return ColVis;
|
1095
|
+
}; // /factory
|
1096
|
+
|
1097
|
+
|
1098
|
+
// Define as an AMD module if possible
|
1099
|
+
if ( typeof define === 'function' && define.amd ) {
|
1100
|
+
define( ['jquery', 'datatables'], factory );
|
1101
|
+
}
|
1102
|
+
else if ( typeof exports === 'object' ) {
|
1103
|
+
// Node/CommonJS
|
1104
|
+
factory( require('jquery'), require('datatables') );
|
1105
|
+
}
|
1106
|
+
else if ( jQuery && !jQuery.fn.dataTable.ColVis ) {
|
1107
|
+
// Otherwise simply initialise as normal, stopping multiple evaluation
|
1108
|
+
factory( jQuery, jQuery.fn.dataTable );
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
|
1112
|
+
})(window, document);
|
1113
|
+
|