# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/act-plain"

module Qwik
  class Action
    def plg_edit_wysiwyg
      return "" if @req.user.nil?
      [:span, {:class=>"attribute"},
	[:a, {:href=>@req.base+".wysiwyg"}, _("Edit in this page")]]
    end

    def ext_wysiwyg
      c_require_pagename
      c_require_page_exist
      c_require_member
      c_require_no_path_args
      c_require_no_ext_args

      if 0 < @req.query.length
	html = @req.query["text"]
#	puts html
	contents = "<html>\n"+html+"\n</html>\n" 
	return ext_save(contents)
      end

      return wysiwyg_show(@site, @req.base)
    end

    def wysiwyg_show(site, pagename)
      editor = WysiwygEditor.new(self)
      str = site[pagename].load
      editor_html = editor.get_html(pagename, str)
      return c_plain(pagename+" : "+_("Edit in this page")){
	[
	  editor.html_warning,
	  [:div, {:class=>"day"},
	    [:div, {:class=>"section"},
	      editor_html]]
	]
      }
    end

  end

  class WysiwygEditor
    def initialize(action)
      @action = action
    end

    def _(s)
      @action._(s)
    end

    def get_html(pagename, str)
      [:div, {:class=>"wysiwyg"},
	[:style, "@import '.theme/css/wema.css';"],
	html_toolbar,
	html_editor(str), # important
	html_form(pagename, str),
	[:script, {:type=>"text/javascript", :src=>".theme/js/wema.js"}, ""],
	[:script, {:type=>"text/javascript", :src=>".theme/js/wysiwyg.js"}, ""],
      ]
    end

    def html_warning
      [:div, {:class=>"message"},
	[:div, {:class=>"day"},
	  [:h2, _("This is experimental function.")],
	  [:p, _("The contents will be translated to html."), " ",
	    _("Please use this function only if you understand what will happen.")]]]
    end

    private

    def html_toolbar
      [:div, {:class=>"wema", :id=>"wysiwyg_toolbar",
	  :style=>"left:0px;top:0px;"},
	[:div, {:class=>"menubar"},
	  [:span, {:class=>"handle"}, "handle"],
	  [:span, {:class=>"close"}, " "]], # noop
	[:div, {:class=>"cont", :id=>"toolbar"},
	  [:span, {:class=>"file"},
	    [:a, {:href=>"javascript:save()"}, "Save"]],
	  [:span,
	    [:a, {:href=>"javascript:command('bold')"}, "B"],
	    [:a, {:href=>"javascript:command('italic')"}, "I"]],
	  [:span,
	    [:a, {:href=>"javascript:command('InsertUnorderedList')"},"UL"],
	    [:a, {:href=>"javascript:command('InsertOrderedList')"}, "OL"]],
	  [:span,
	    [:a, {:href=>"javascript:markup('h2')"}, "H2"],
	    [:a, {:href=>"javascript:markup('h3')"}, "H3"],
	    [:a, {:href=>"javascript:markup('h4')"}, "H4"]],
	  [:span,
	    [:a, {:href=>"javascript:command('CreateLink')"}, "Link"],
	    [:a, {:href=>"javascript:image()"}, "Image"]],
	  [:span,
	    [:a, {:href=>"javascript:deleteTags()"}, "normal"]]]]
    end

    def html_editor(str)
      w = @action.c_parse(str) # without resolve
      [:div, {:id=>"weditor", :contenteditable=>"true"}, w]
    end

    def html_form(pagename, str)
      now = Time.now.to_i.to_s
      disp = $debug ? "block" : "none"
      md5hex = str.md5hex
      [:div, {:style=>"display:#{disp};"},
	[:form, {:action=>pagename+".wysiwyg", :method=>"post",
	    :name=>"form", :id=>"form"},
	  [:input, {:type=>"submit", :value=>"Submit"}],
	  [:input, {:type=>"text", :name=>"mode", :value=>"save"}],
	  [:input, {:type=>"text", :name=>"md5hex", :value=>md5hex}],
	  [:textarea, {:name=>"text", :id=>"text"}, ""]]]
    end
  end

end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActWysiwyg < Test::Unit::TestCase
    include TestSession

    def test_plg_edit_wysiwyg
      assert_wiki([:span, {:class=>"attribute"},
		    [:a, {:href=>"1.wysiwyg"}, "Edit in this page"]],
		  "{{edit_wysiwyg}}")
    end

    def test_ext_wysiwyg
      t_add_user

      page = @site.create_new
      page.store("*t")

      session("/test/1.wysiwyg")
      assert_text("1 : Edit in this page", "title")

      session("POST /test/1.wysiwyg?text=ttt")
      assert_text("Page is saved.", "title")
#     str = (@site.dir.path+"#{page.key}.txt").open {|f| f.read }
#     assert_equal("<html>\nttt\n</html>\n", str)
      assert_equal("<html>\nttt\n</html>\n", page.load)

      # test_wysiwyg_bug
      session("/test/TextFormatSimple.wysiwyg")
      assert_text("TextFormatSimple : Edit in this page", "title")
    end
  end
end
