# -*- coding: utf-8 -*-
"""
    MoinMoin - CheckList
    
    Creates an HTML checkbox as well as the HTML heading at the level you specify
    so that you can use the wiki as a checklist for repeated tasks such as
    OS installs, inventory checks, training etc.
    
    CheckList utilizes the HTML5 localstorage feature for stateful storage
    without the need for external data storage. Therefore, you must use a 
    browser that supports HTML5 which includes: ie8+, firefox 3.5+, safari 4.0+, 
    chrome 4.0+, opera 10.5+, iphone 2.0+, android 2.0+. The drawback is that
    you must use the same browser for your checklist, but that is kindof the 
    point of this Macro.

    Usage:
        <<CheckList(id_for_checkbox, heading_level_number, heading_text_and_heading_id)>>
        <<CheckList(js=True)>> (REQUIRED)
        <<CheckList(clear_button=True)>> (Optional but nice)
    
    Comments:    
        see @params list below for more info
        Do Not use == Text == for headings. This macro creates headings for you.
        Different than the ToDo parser (http://moinmo.in/ParserMarket/ToDo)
        This creates a javascript src link to the Google CDN for jquery. 
        If this bothers you, let me know and I can rewrite some things.
        Fully works with the <<TableOfContents>> macro.

    @copyright: 2011 Gregory Corey <gregcorey@gmail.com>
    @license: GNU GPL, see COPYING for details.
"""
from MoinMoin import wikiutil
from MoinMoin.parser.text_moin_wiki import Parser as WikiParser

generates_headings = True

def macro_CheckList(macro, dom_id=None, heading=2, text=None, js=None, clear_button=None):
    """
    @param dom_id: The id you want for your checkbox.
    @param heading: The Number level of the heading you are creating 2 == h2 etc
    @param text: The text of the heading and is used to make the id of the 
    heading and is used if you use the TableOfContents macro
    @param js: Leave all params blank except for js=True to create the necessary 
    javascript for this macro. Should be at the top of your page.
    @param clear_button: Leave all params blank except for clear_button=True to 
    create a button to clear all Checklist items. Should be at the top of your page.
   """
    ret = []
    if js:
        return emit_js()
        
    if clear_button:
        return emit_button()
        
    f = macro.request.formatter
    # Escape HTML stuff.
    text_h = wikiutil.escape(text)
    text_h = text_h.strip()
    text_id = wikiutil.escape(text)
    text_id = text_id.replace(' ', '_')
    text_id = text_id.strip()

    ret.extend([f.heading(1,heading,id=text_id)])
    ret.extend([f.rawHTML("<input type='checkbox' class='checklist' name='name_%s' id='id_%s'><label for='id_%s'>" % (dom_id, dom_id, dom_id))])
    ret.extend([f.text(text_h)])
    ret.extend([f.rawHTML("</label>")])
    ret.extend([f.heading(0,heading)])
    
    return ''.join(ret)
    
def emit_js():
    js = """
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function supports_html5_storage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
  	alert(e);
    return false;
  }
}
if(localStorage.getItem('save')){
	$(".checklist").each(function(e){
		var this_id = $(this).attr("id");
	    var check_val = localStorage.getItem(this_id);
	    switch(check_val){
	    	case "true":
	    		check_val = true;
	    		break;
	    	case "false":
	    		check_val = false;
	    		break;
	    }
	    $("#"+this_id).attr("checked", check_val);
	    if(check_val==true){
	    	$("#"+this_id).parent().css({'text-decoration':'line-through'});
	    }
	});
}
	$(".checklist").change(function(){
		var this_id = $(this).attr("id");
		var this_val = $(this).attr("checked");
		localStorage.setItem('save', true);
		localStorage.setItem(this_id, this_val);
		if(this_val==true){
			$(this).parent().css({'text-decoration':'line-through'});
		}
		else{
			$(this).parent().css({'text-decoration':'none'});
		}
	});
	
	$("#clearall").click(function(){
		$(".checklist").each(function(){
			var this_id = $(this).attr("id");
			$(this).attr("checked", false).parent().css({'text-decoration':'none'});
			localStorage.setItem(this_id, false);
		});
	});
});
</script>
    """
    return js
    
def emit_button():
    button = """
    <input type="button" id="clearall" name="clearall" value="Clear All Checklist items" />
    """
    return button
