user-notes.js 5.91 KB
/*
Copyright (c) 2012, Bartłomiej Nitoń
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

$(function() {
    
  $("button#delete_user_note").click(delete_user_note);
  $("button#goto_lemma").click(goto_lemma);

// czysci pamiec sesji o wybranej notatce    
  $("#menu_option").click(function(){
    $.ajax({url: ajax_note_session_clear,
           async: false});
  });
  
	$("#public-notes-paginate").paginate({
		count 		: publicNotesPagesCount,
		start 		: 1,
		display     : 10,
		border					: true,
		border_color			: '#fff',
		text_color  			: 'black',
		background_color    	: '#D8D8D8',	
		border_hover_color		: '#ccc',
		text_hover_color  		: '#000',
		background_hover_color	: '#fff', 
		images					: false,
		mouse					: 'press',
		onChange     			: function(page){
			$("#public-notes-table").empty();
			$("#public-notes-table").load(ajax_notes_page, {page: page, notes_private: false});
		}
	});
	
	$("#private-notes-paginate").paginate({
		count 		: privateNotesPagesCount,
		start 		: 1,
		display     : 10,
		border					: true,
		border_color			: '#fff',
		text_color  			: 'black',
		background_color    	: '#D8D8D8',	
		border_hover_color		: '#ccc',
		text_hover_color  		: '#000',
		background_hover_color	: '#fff', 
		images					: false,
		mouse					: 'press',
		onChange     			: function(page){
			$("#private-notes-table").empty();
			$("#private-notes-table").load(ajax_notes_page, {page: page, notes_private: true});
		}
	});
	
	$(".user_notes_container").tabs();
	
	$("#public-notes-table").load(ajax_notes_page, {page: 1, notes_private: false});
	$("#private-notes-table").load(ajax_notes_page, {page: 1, notes_private: true});
});

var publicNotesPagesCount = 0;
var privateNotesPagesCount = 0;

function goto_lemma()
{
	var id = get_selected_note();
    if(id == undefined)
	{
	  alert('Zaznacz notatkę aby przejść do powiązanego z nią hasła.');
	  return false;
	}
	
    lemma_view_location = window.location.toString();
    // trzeba by to moze z czasem poprawic na cos madrzejszego
    lemma_view_location = lemma_view_location.replace('/notatki/', '');
    window.location = lemma_view_location;
}

function delete_user_note() {
	var id = get_selected_note();
	if(id == undefined)
	{
	  alert('Zaznacz notatkę aby ją usunąć.');
	  return false;
	}
	
	message_id = id.replace('note_', '');
	
    $.ajaxJSON({
      method: 'post',
      url: ajax_remove_note,
      data: {
        message_id: message_id
      },

    callback: function(result) {      
		$('#'+id).remove();
		$('#lemma_note_text').empty();
		selected_notes_row_id = -1;
    },
    error_callback: function(xhr, status, error) {
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'can not remove') {
        error_alert('Usuwać można jedynie notatki prywatne.');
        return false;
      }
      else if (result == 'already removed') {
          error_alert('Notatka została usunięta, odśwież widok.');
          return false;
      }
      else
        return true;
    },
  });
  return false;
}

function get_selected_note() {
	return $('[id^=note_][selected=selected]').first().attr('id');
}

function get_lemma_from_note(id)
{  	
	// znajdz identyfikator hasla do ktorego dodano notatke
	message_id = id.replace('note_', '');
	
    $.ajaxJSON({
      method: 'post',
      url: ajax_get_lemma_from_note,
      data: {
        message_id: message_id
      },

    callback: function(result) {    
        if(result['new_notes_count']>0)
          $('#menu_option.notes a').text('Notatki ['+result['new_notes_count']+']');
        else
          $('#menu_option.notes a').text('Notatki');
    },
    error_callback: function(xhr, status, error) {
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
        if (result == 'already removed') {
            error_alert('Notatka została usunięta, odśwież widok.');
            return false;
        }
        else
          return true;
    },
  });
}

function selectNote(note_id) {
	clearNoteSelection();
	$('#' + note_id).attr('selected', "selected");
	$('#' + note_id).children().each(function(){
		$(this).css('background-color', "LightSteelBlue");
	});
	message_id = note_id.replace('note_', '');
	$('#lemma_note_text').load(ajax_get_note_text, 'id='+message_id, function(){
        $('#'+note_id+' td#new').text('   ');
        get_lemma_from_note(note_id);
	});
}

function clearNoteSelection() {
	$('[id^=note_][selected=selected]').each(function(){
		$(this).removeAttr('selected');
		$(this).children().each(function(){
			$(this).css('background-color', "");
		});
	});
}