| 1 | /*global $, post_remember_str */ |
|---|
| 2 | 'use strict'; |
|---|
| 3 | |
|---|
| 4 | $(function() { |
|---|
| 5 | $('#comment-form p:has(input[type=submit][name=preview],button[type=submit][name=preview])'). |
|---|
| 6 | before( |
|---|
| 7 | '<p class="remember"><input type="checkbox" id="c_remember" name="c_remember" /> ' + |
|---|
| 8 | '<label for="c_remember">' + post_remember_str + '</label>' + |
|---|
| 9 | '</p>' |
|---|
| 10 | ); |
|---|
| 11 | |
|---|
| 12 | var cookie = readCookie($.cookie('comment_info')); |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | if (cookie != false) { |
|---|
| 16 | $('#c_name').val(cookie[0]); |
|---|
| 17 | $('#c_mail').val(cookie[1]); |
|---|
| 18 | $('#c_site').val(cookie[2]); |
|---|
| 19 | $('#c_remember').attr('checked','checked'); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | $('#c_remember').click(function() { |
|---|
| 23 | if (this.checked) { |
|---|
| 24 | setCookie(); |
|---|
| 25 | } else { |
|---|
| 26 | dropCookie(); |
|---|
| 27 | } |
|---|
| 28 | }); |
|---|
| 29 | |
|---|
| 30 | $('#c_name').change(function() { |
|---|
| 31 | if ($('#c_remember').get(0).checked) { |
|---|
| 32 | setCookie(); |
|---|
| 33 | } |
|---|
| 34 | }); |
|---|
| 35 | |
|---|
| 36 | $('#c_mail').change(function() { |
|---|
| 37 | if ($('#c_remember').get(0).checked) { |
|---|
| 38 | setCookie(); |
|---|
| 39 | } |
|---|
| 40 | }); |
|---|
| 41 | |
|---|
| 42 | $('#c_site').change(function() { |
|---|
| 43 | if ($('#c_remember').get(0).checked) { |
|---|
| 44 | setCookie(); |
|---|
| 45 | } |
|---|
| 46 | }); |
|---|
| 47 | |
|---|
| 48 | function setCookie() { |
|---|
| 49 | var name = $('#c_name').val(); |
|---|
| 50 | var mail = $('#c_mail').val(); |
|---|
| 51 | var site = $('#c_site').val(); |
|---|
| 52 | var cpath = $('h1 a').attr('href'); |
|---|
| 53 | if (!cpath) { |
|---|
| 54 | cpath = '/'; |
|---|
| 55 | } else { |
|---|
| 56 | cpath = cpath.replace(/.*:\/\/[^\/]*([^?]*).*/g,'$1'); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $.cookie('comment_info', name + '\n' + mail + '\n' + site, {expires: 60, path: cpath}); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | function dropCookie() { |
|---|
| 63 | $.cookie('comment_info','',{expires: -30, path: '/'}); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | function readCookie(c) { |
|---|
| 67 | if (!c) { |
|---|
| 68 | return false; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | var s = c.split('\n'); |
|---|
| 72 | |
|---|
| 73 | if (s.length != 3) { |
|---|
| 74 | dropCookie(); |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | return s; |
|---|
| 79 | } |
|---|
| 80 | }); |
|---|