Created
March 23, 2012 10:20
-
-
Save poochin/2169295 to your computer and use it in GitHub Desktop.
メモ: Tumblr が L キーでどのように Like を実行しているかのコード部
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function _submit_like_toggler(post_id) { | |
| var unlike = $('like_button_' + post_id).hasClassName('already_like') ? true : false; | |
| var like_form_action = $('like_form_' + post_id).action; | |
| var root_id = $('like_button_' + post_id).hasAttribute('data-root-post-id') ? $('like_button_' + post_id).getAttribute('data-root-post-id') : false; | |
| if (unlike) { | |
| $('like_button_' + post_id).removeClassName('already_like'); | |
| $('like_form_' + post_id).action = like_form_action.replace('/unlike', '/like'); | |
| decrement_note_count(post_id); | |
| if (root_id) { | |
| $$('.like_root_' + root_id).invoke('removeClassName', 'already_like') | |
| } | |
| } else { | |
| $('like_button_' + post_id).addClassName('already_like'); | |
| $('like_form_' + post_id).action = like_form_action.replace('/like', '/unlike'); | |
| increment_note_count(post_id); | |
| if (root_id) { | |
| $$('.like_root_' + root_id).invoke('addClassName', 'already_like') | |
| } | |
| } | |
| } | |
| function submit_like(post_id, custom_z r) { | |
| var like_form_action = $('like_form_' + post_id).action; | |
| if (typeof custom_toggler == 'undefined') { | |
| _submit_like_toggler(post_id) | |
| } else { | |
| custom_toggler() | |
| } | |
| new Ajax.Request(like_form_action, { | |
| asynchronous: true, | |
| parameters: Form.serialize($('like_form_' + post_id)), | |
| onFailure: function () { | |
| new Ajax.Request(like_form_action, { | |
| asynchronous: true, | |
| parameters: Form.serialize($('like_form_' + post_id)), | |
| onFailure: function () { | |
| alert(l10n_str.ajax_error); | |
| if (typeof custom_toggler == 'undefined') { | |
| _submit_like_toggler(post_id) | |
| } else { | |
| custom_toggler() | |
| } | |
| } | |
| }) | |
| } | |
| }) | |
| } | |
| /* ... */ | |
| function start_observing_key_commands(auto_paginate) { | |
| Event.observe(document, 'keydown', function (e) { | |
| if (!key_commands_are_suspended) { | |
| var code = e.charCode ? e.charCode : e.keyCode; | |
| if (!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) { | |
| if (code == Event.KEY_LEFT) { | |
| if (!auto_paginate && !$('tumblr_lightbox')) { | |
| if ($('previous_page_link')) location.href = $('previous_page_link').href | |
| } | |
| } else if (code == Event.KEY_RIGHT) { | |
| if (!auto_paginate && !$('tumblr_lightbox')) { | |
| if ($('next_page_link')) location.href = $('next_page_link').href | |
| } | |
| } else if (code == 74 || code == 75 || code == 76) { | |
| var post_positions = new Hash(); | |
| $('posts').childElements().each(function (post) { | |
| if (post.hasClassName('post') && post.id && post.id.indexOf('post_') === 0) { | |
| post_positions.set(post.id, post.offsetTop) | |
| } | |
| }); | |
| var go_to_position = 0; | |
| post_positions.each(function (pair) { | |
| var post_id = pair.key; | |
| var post_position = pair.value; | |
| var current_position = document.viewport.getScrollOffsets().top + 7; | |
| if (code == 74) { | |
| if (post_position > current_position && (post_position < go_to_position || !go_to_position)) { | |
| go_to_position = post_position | |
| } | |
| } else if (code == 75) { | |
| if (post_position < current_position && post_position > go_to_position) { | |
| go_to_position = post_position | |
| } | |
| } else if (code == 76) { | |
| if (post_position == current_position && $('like_form_' + post_id.replace('post_', ''))) { | |
| submit_like(post_id.replace('post_', '')) | |
| } | |
| } | |
| }); | |
| if (go_to_position) window.scrollTo(0, go_to_position - 7) | |
| } | |
| } | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment