summaryrefslogtreecommitdiff
path: root/data/js/misc.js
blob: 89572e82e4f06b05bb707fe688a8797ce8019521 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// search box
byId('sq').onfocus = function () {
  if(this.value == mt('_menu_emptysearch')) {
    this.value = '';
    this.style.fontStyle = 'normal'
  }
};
byId('sq').onblur = function () {
  if(this.value.length < 1) {
    this.value = mt('_menu_emptysearch');
    this.style.fontStyle = 'italic'
  }
};


function ulist_redirect(type, path, formcode, args) {
  var r = new RegExp('/('+type+'[0-9]+).*$');
  location.href = location.href.replace(r, '/$1')+path
    +'?formcode='+formcode
    +';ref='+encodeURIComponent(location.pathname+location.search)
    +';'+args;
}


// VN Voting (/v+)
if(byId('votesel'))
  byId('votesel').onchange = function() {
    var s = this.options[this.selectedIndex].value;
    if(s == -2)
      s = prompt(mt('_vnpage_uopt_othervote'), '');
    if(!s || s == -3)
      return;
    if(s != -1 && (!s.match(/^([1-9]|10)([\.,][0-9])?$/) || s > 10 || s < 1)) {
      alert(mt('_vnpage_uopt_invvote'));
      this.selectedIndex = 0;
      return;
    }
    s = s.replace(',', '.');
    if(s == 1 && !confirm(mt('_vnpage_uopt_1vote')))
      return;
    if(s == 10 && !confirm(mt('_vnpage_uopt_10vote')))
      return;
    if(s > 0 || s == -1)
      ulist_redirect('v', '/vote', this.name, 'v='+s);
  };


// VN Wishlist dropdown box (/v+)
if(byId('wishsel'))
  byId('wishsel').onchange = function() {
    if(this.selectedIndex != 0)
      ulist_redirect('v', '/wish', this.name, ';s='+this.options[this.selectedIndex].value);
  };


// Release & VN list dropdown box (/r+ and /v+)
if(byId('listsel'))
  byId('listsel').onchange = function() {
    if(this.selectedIndex != 0)
      ulist_redirect('[rv]', '/list', this.name, 'e='+this.options[this.selectedIndex].value);
  };


// NSFW VN image toggle (/v+)
(function() {
  var msg = byId('nsfw_show');
  if(msg) {
    var img = byId('nsfw_hid');
    byName(msg, 'a')[0].onclick = function() {
      setClass(msg, 'hidden', true);
      setClass(img, 'hidden', false);
      return false;
    };
    img.onclick = function() {
      setClass(msg, 'hidden', false);
      setClass(img, 'hidden', true);
    };
  }
})();


// NSFW toggle for screenshots (/v+)
if(byId('nsfwhide'))
  byId('nsfwhide').onclick = function() {
    var shown = 0;
    var l = byClass(byId('screenshots'), 'a', 'scrlnk');
    for(var i=0; i<l.length; i++) {
      if(hasClass(l[i], 'nsfw')) {
        var hidden = !hasClass(l[i], 'hidden');
        setClass(l[i], 'hidden', hidden);
        if(!hidden)
          shown++;
      } else
        shown++;
    }
    setText(byId('nsfwshown'), shown);
    return false;
  };


// Notification list onclick
(function(){
  var d = byId('notifies');
  if(!d)
    return;
  var l = byClass(d, 'td', 'clickable');
  for(var i=0; i<l.length; i++)
    l[i].onclick = function() {
      var baseurl = location.href.replace(/\/u([0-9]+)\/notifies.*$/, '/u$1/notify/');
      location.href = baseurl + this.id.replace(/notify_/, '');
    };
})();


// BBCode spoiler tags
(function(){
  var l = byClass('b', 'spoiler');
  for(var i=0; i<l.length; i++) {
    l[i].onmouseover = function() { setClass(this, 'spoiler', false); setClass(this, 'spoiler_shown', true)  };
    l[i].onmouseout = function()  { setClass(this, 'spoiler', true);  setClass(this, 'spoiler_shown', false) };
  }
})();


// vndb.org domain check
// (let's just keep this untranslatable, nobody cares anyway ^^)
if(location.hostname != 'vndb.org') {
  addBody(tag('div', {id:'debug'},
    tag('h2', 'This is not VNDB!'),
    'The real VNDB is ',
    tag('a', {href:'http://vndb.org/'}, 'here'),
    '.'
  ));
}


// make some fields readonly when patch flag is set (/r+/edit)
(function(){
  function sync() {
    byId('doujin').disabled =
      byId('resolution').disabled =
      byId('voiced').disabled =
      byId('ani_story').disabled =
      byId('ani_ero').disabled =
      byId('patch').checked;
  };
  if(byId('jt_box_rel_geninfo')) {
    sync();
    byId('patch').onclick = sync;
  }
})();


// Batch edit dropdown box (/u+/wish and /u+/votes)
if(byId('batchedit'))
  byId('batchedit').onchange = function() {
    if(this.selectedIndex == 0)
      return true;
    var frm = this;
    while(frm.nodeName.toLowerCase() != 'form')
      frm = frm.parentNode;
    frm.submit();
  };


// collapse/expand row groups (/u+/list)
(function(){
  var table = byId('expandall');
  if(!table)
    return;
  while(table.nodeName.toLowerCase() != 'table')
    table = table.parentNode;
  var heads = byClass(table, 'td', 'collapse_but');
  var allhid = false;

  function sethid(l, h, hid) {
    var i;
    for(i=0; i<l.length; i++) {
      setClass(l[i], 'hidden', hid);
      // Set the hidden class on the input checkbox, if it exists. This
      // prevents the "select all" functionality from selecting it if the row
      // is not visible.
      var sel = byName(l[i], 'input')[0];
      if(sel)
        setClass(sel, 'hidden', hid);
    }
    for(i=0; i<h.length; i++)
      setText(h[i], allhid ? collapsed_icon : expanded_icon);
  }

  function alltoggle() {
    allhid = !allhid;
    setText(byId('expandall'), allhid ? collapsed_icon : expanded_icon);
    sethid(byClass(table, 'tr', 'collapse'), heads, allhid);
    return false;
  }

  function singletoggle() {
    var l = byClass(table, 'tr', 'collapse_'+this.id);
    sethid(l, [this], !hasClass(l[0], 'hidden'));
  }

  byId('expandall').onclick = alltoggle;
  for(var i=0; i<heads.length; i++)
    heads[i].onclick = singletoggle;
  alltoggle();
})();


// mouse-over price information / disclaimer
(function(){
  if(byId('buynow')) {
    var l = byClass(byId('buynow'), 'acronym', 'pricenote');
    for(var i=0; i<l.length; i++) {
      l[i].buynow_last = l[i].title;
      l[i].title = null;
      ddInit(l[i], 'bottom', function(acr) {
        return tag('p', {onmouseover:ddHide, style:'padding: 3px'},
          acr.buynow_last, tag('br', null),
          '* The displayed price only serves as an indication and',
          tag('br', null), 'usually excludes shipping. Actual price may differ.'
        );
      });
    }
  }
})();


// set note input box (/u+/list)
if(byId('not') && byId('vns'))
  byId('vns').onchange = function () {
    if(this.options[this.selectedIndex].value == 999)
      byId('not').value = prompt(mt('_rlist_setnote_prompt'), '');
    return true;
  };


// expand/collapse release listing (/p+)
(function(){
  var lnk = byId('expandprodrel');
  if(!lnk)
    return;
  function setexpand() {
    var exp = !(getCookie('prodrelexpand') == 1);
    setText(lnk, exp ? mt('_js_collapse') : mt('_js_expand'));
    setClass(byId('prodrel'), 'collapse', !exp);
  };
  lnk.onclick = function () {
    setCookie('prodrelexpand', getCookie('prodrelexpand') == 1 ? 0 : 1);
    setexpand();
    return false;
  };
  setexpand();
})();


// Language selector
(function(){
  var d = byId('lang_select');
  var flag = byName(d, 'acronym')[0];
  ddInit(d, 'bottom', function(lnk) {
    var lst = tag('ul', null);
    for(var i=0; i<VARS.l10n_lang.length; i++) {
      var ln = VARS.l10n_lang[i];
      var icon = tag('acronym', {'class':'icons lang '+ln[0]}, ' ');
      lst.appendChild(tag('li', {'class':'lang_selector'}, hasClass(flag, ln[0])
        ? tag('i', icon, ln[1])
        : tag('a', {href:'/setlang?lang='+ln[0]+';ref='+encodeURIComponent(location.pathname+location.search)}, icon, ln[1])
      ));
    }
    return lst;
  });
  d.onclick = function() {return false};
})();


// "check all" checkbox
(function(){
  function set() {
    var l = byName('input');
    for(var i=0; i<l.length; i++)
      if(l[i].type == this.type && l[i].name == this.name && !hasClass(l[i], 'hidden'))
        l[i].checked = this.checked;
  }
  var l = byClass('input', 'checkall');
  for(var i=0; i<l.length; i++)
    if(l[i].type == 'checkbox')
      l[i].onclick = set;
})();


// search tabs
(function(){
  function click() {
    var str = byId('q').value;
    if(str.length > 1) {
      this.href = this.href.split('?')[0];
      if(this.href.indexOf('/g') >= 0 || this.href.indexOf('/i') >= 0)
        this.href += '/list';
      this.href += '?q=' + encodeURIComponent(str);
    }
    return true;
  };
  if(byId('searchtabs')) {
    var l = byName(byId('searchtabs'), 'a');
    for(var i=0; i<l.length; i++)
      l[i].onclick = click;
  }
})();


// spam protection on all forms
setTimeout(function() {
  for(var i=1; i<document.forms.length; i++)
    document.forms[i].action = document.forms[i].action.replace(/\/nospam\?/,'');
}, 500);