Better js code to apply CKEditor.replace, mainly to select only textarea.name=$name.
code cleaning.
This commit is contained in:
@@ -62,14 +62,14 @@ feature -- Forms ...
|
||||
sum.set_text_value (a_node.summary)
|
||||
end
|
||||
sum.set_label ("Summary")
|
||||
sum.set_description ("This is the summary")
|
||||
sum.set_description ("Text displayed in short view.")
|
||||
sum.set_is_required (False)
|
||||
|
||||
create fset.make
|
||||
|
||||
-- Add summary
|
||||
fset.extend (sum)
|
||||
fset.extend_html_text("<br />")
|
||||
fset.extend_html_text("<br/>")
|
||||
|
||||
-- Add content
|
||||
fset.extend (ta)
|
||||
|
||||
@@ -8,6 +8,7 @@ deferred class
|
||||
CMS_EDITOR
|
||||
|
||||
feature -- Initialisation
|
||||
|
||||
load_assets : STRING
|
||||
-- Loads all assest needed to show the editor
|
||||
deferred
|
||||
@@ -15,57 +16,55 @@ feature -- Initialisation
|
||||
|
||||
feature -- Javascript
|
||||
|
||||
javascript_replace_textarea (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
javascript_replace_textarea (a_textarea : WSF_FORM_TEXTAREA): STRING
|
||||
-- Javascript code that replaces a textarea with the editor. The editor instance should be saved in editor_variable
|
||||
deferred
|
||||
end
|
||||
|
||||
javascript_restore_textarea (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
javascript_restore_textarea (a_textarea : WSF_FORM_TEXTAREA): STRING
|
||||
-- Javascript code that restores a textarea
|
||||
deferred
|
||||
end
|
||||
|
||||
javascript_textarea_to_editor(a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
javascript_textarea_to_editor (a_textarea : WSF_FORM_TEXTAREA): STRING
|
||||
-- Javascript code to display the textarea as a WYSIWIG editor as soon as the document is loaded
|
||||
do
|
||||
Result := javascript_ready(javascript_replace_textarea (a_textarea))
|
||||
Result := javascript_ready (javascript_replace_textarea (a_textarea))
|
||||
end
|
||||
|
||||
javascript_textarea_to_editor_if_selected (a_textarea : WSF_FORM_TEXTAREA; a_select_field : WSF_FORM_SELECT; a_value : STRING) : STRING
|
||||
javascript_textarea_to_editor_if_selected (a_textarea: WSF_FORM_TEXTAREA; a_select_field : WSF_FORM_SELECT; a_value : STRING) : STRING
|
||||
-- Javascript code to display the textarea as a WYSIWIG editor if a_select_field has a_value
|
||||
local
|
||||
initial_replace_code, on_select_replace_code : STRING
|
||||
initial_replace_code, on_select_replace_code: STRING
|
||||
do
|
||||
-- Javascript that replaces the textarea if a_value is selected at load time
|
||||
initial_replace_code := javascript_ready(javascript_if_selected(a_select_field, a_value, javascript_replace_textarea(a_textarea)))
|
||||
initial_replace_code := javascript_ready (javascript_if_selected (a_select_field, a_value, javascript_replace_textarea (a_textarea)))
|
||||
|
||||
-- Javascript code that replaces the textarea as soon as value is selected at a_select_field
|
||||
on_select_replace_code := javascript_ready(
|
||||
javascript_init_editor_variable(a_textarea) +
|
||||
javascript_on_select(a_select_field, a_value,
|
||||
javascript_init_editor_variable (a_textarea) +
|
||||
javascript_on_select (a_select_field, a_value,
|
||||
-- If a_value is selected, replace textarea
|
||||
javascript_replace_textarea(a_textarea),
|
||||
javascript_replace_textarea (a_textarea),
|
||||
|
||||
-- Otherwise restore it
|
||||
javascript_restore_textarea(a_textarea)
|
||||
javascript_restore_textarea (a_textarea)
|
||||
)
|
||||
)
|
||||
|
||||
Result := initial_replace_code + " " + on_select_replace_code
|
||||
end
|
||||
|
||||
javascript_init_editor_variable(a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
javascript_init_editor_variable (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
-- Returns the javascript code that initializes a local variable to store the editor instance
|
||||
do
|
||||
Result := "var " + editor_variable(a_textarea) + "; "
|
||||
Result := "var " + editor_variable (a_textarea) + "; "
|
||||
end
|
||||
|
||||
|
||||
|
||||
javascript_if_selected(a_select_field : WSF_FORM_SELECT; a_value : STRING; a_code : STRING) : STRING
|
||||
javascript_if_selected (a_select_field : WSF_FORM_SELECT; a_value : STRING; a_code : STRING) : STRING
|
||||
-- Javascript that executes a_code if a_value is selected at a_select_field
|
||||
do
|
||||
Result := "if($('#" + field_id(a_select_field) + "').val() == %"" + a_value + "%"){ " + a_code + " }"
|
||||
Result := "if($('#" + field_id (a_select_field) + "').val() == %"" + a_value + "%"){ " + a_code + " }"
|
||||
end
|
||||
|
||||
javascript_ready (a_code : STRING) : STRING
|
||||
@@ -77,8 +76,8 @@ feature -- Javascript
|
||||
javascript_on_select (a_select_field : WSF_FORM_SELECT; a_value : STRING; a_then : STRING; a_else : STRING) : STRING
|
||||
-- Javascript code that executes a_then if at the given select_field the given string value is selected, otherwise it executes a_else
|
||||
do
|
||||
Result := "$('#" + field_id(a_select_field) + "').change(function(){" +
|
||||
javascript_if_selected(a_select_field, a_value, a_then) +
|
||||
Result := "$('#" + field_id (a_select_field) + "').change(function(){" +
|
||||
javascript_if_selected (a_select_field, a_value, a_then) +
|
||||
"else{" +
|
||||
a_else +
|
||||
"}" +
|
||||
@@ -100,7 +99,10 @@ feature -- Helper
|
||||
editor_variable (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
-- Returns the variable name that stores the editor instance of the given textarea
|
||||
do
|
||||
Result := "editor_" + a_textarea.name
|
||||
Result := "cms_ckeditor_" + a_textarea.name
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
|
||||
@@ -23,15 +23,20 @@ feature -- Javascript
|
||||
javascript_replace_textarea (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
-- <Precursor>
|
||||
do
|
||||
-- Replaces the textarea with an editor instance. Save the instance in a variable
|
||||
Result := editor_variable(a_textarea) + " = CKEDITOR.replace( '" + a_textarea.name + "' );"
|
||||
-- Replaces the textarea with an editor instance. Save the instance in a variable
|
||||
Result := "$(%"textarea[name="+ a_textarea.name +"]%").each(function() {"
|
||||
Result.append (editor_variable (a_textarea) + " = CKEDITOR.replace(this);")
|
||||
Result.append ("});")
|
||||
end
|
||||
|
||||
javascript_restore_textarea (a_textarea : WSF_FORM_TEXTAREA) : STRING
|
||||
-- <Precursor>
|
||||
do
|
||||
-- Replaces the textarea with an editor instance. Save the instance in a variable
|
||||
Result := "if (" + editor_variable(a_textarea) + " != undefined) " + editor_variable(a_textarea) + ".destroy();"
|
||||
-- Replaces the textarea with an editor instance. Save the instance in a variable
|
||||
Result := "if (" + editor_variable (a_textarea) + " != undefined) " + editor_variable (a_textarea) + ".destroy();"
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
|
||||
@@ -24,15 +24,15 @@ feature -- Initialisation
|
||||
make (a_name: like name)
|
||||
-- <Precursor>
|
||||
do
|
||||
precursor(a_name)
|
||||
Precursor (a_name)
|
||||
|
||||
-- By default we don't replace the textarea by an editor
|
||||
editor := False;
|
||||
editor_activated := False;
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
editor : BOOLEAN
|
||||
editor_activated : BOOLEAN
|
||||
-- True if the textarea should be replaced by the editor. Default is false.
|
||||
|
||||
format_field : detachable WSF_FORM_SELECT
|
||||
@@ -45,13 +45,13 @@ feature -- Editor
|
||||
show_as_editor
|
||||
-- The textarea will be replaced by a wysiwyg editor
|
||||
do
|
||||
editor := True
|
||||
editor_activated := True
|
||||
end
|
||||
|
||||
show_as_editor_if_selected (a_select_field : WSF_FORM_SELECT; a_value : STRING)
|
||||
-- Replaces the textarea only if a_select_field has a_value (or the value gets selected)
|
||||
do
|
||||
editor := True
|
||||
editor_activated := True
|
||||
format_field := a_select_field
|
||||
condition_value := a_value
|
||||
end
|
||||
@@ -61,8 +61,8 @@ feature -- Conversion
|
||||
append_item_to_html (a_theme: WSF_THEME; a_html: STRING_8)
|
||||
do
|
||||
-- Add javascript to replace textarea with editor
|
||||
precursor(a_theme, a_html)
|
||||
if editor then
|
||||
Precursor (a_theme, a_html)
|
||||
if editor_activated then
|
||||
a_html.append (load_assets)
|
||||
a_html.append ("<script type=%"text/javascript%">");
|
||||
if attached format_field as l_field and then attached condition_value as l_value then
|
||||
|
||||
Reference in New Issue
Block a user