Skip to content

Module not working in Page Editor (Sitecore 7.2) #1

@jqmdev

Description

@jqmdev

Hi,

I wanted to use this nice module but it only worked in Content Editor. Problem was that Sitecore used prototype and not jQuery in Page Editor (error "jquery not available"). So I transformed your jQuery code into plain javascript and everything is working fine now. Maybe you can merge this bugfix into your code for others using your module.

I modified the two functions GetOnKeyUpScript and GetOnKeyDownScript in LimitedFieldHelper.cs:

/// <summary>
/// Get the JavaScript code for the onkeyup event of the LimitedField control.
/// </summary>
/// <param name="field">The field.</param>
/// <returns></returns>
public static string GetOnKeyUpScript(ILimitedField field)
{
    var script = new StringBuilder();

    // Check if ctrl key was released.
    script.Append("if (event.keyCode == 17)");
    script.Append("  window.ctrl_down = false;");

    // Strip all characters after max length characters
    script.AppendFormat("if (this.value.length > {0})", field.MaxLength);
    script.AppendFormat("  this.value = this.value.substring(0, {0});", field.MaxLength);

    // Get the field label element and text
    script.Append("var label = this.parentNode.parentNode.previousSibling;");
    script.Append("var labelText = label.innerHTML.split(':')[0];");

    // Determine how many characters are left
    script.AppendFormat("var charsLeft = {0} - this.value.length;", field.MaxLength);
    script.AppendFormat("var charsLeftText = ': (' + charsLeft + ' of {0} characters left)';", field.MaxLength);

    // Display the amount of characters left after the field label text
    script.Append("label.innerHTML = labelText + charsLeftText;");


    return script.ToString();
}

/// <summary>
/// Get the JavaScript code for the onkeydown event of the LimitedField control.
/// </summary>
/// <param name="field">The field.</param>
/// <returns></returns>
public static string GetOnKeyDownScript(ILimitedField field)
{
    var script = new StringBuilder();

    // Allow a certain list of keycodes to be used even when the maximum number of characters has been used
    string allowedKeyCodes = Settings.GetSetting("ParTech.LimitedTextFields.AllowedKeyCodes", "8,9,13,16,17,18,20,27,46,32,35,36,37,38,39,40");
    script.AppendFormat("var allowedKeyCodes = [ {0} ];", allowedKeyCodes);

    // Check if ctrl key was pressed.
    script.Append("window.ctrl_down = (window.ctrl_down || event.keyCode == 17);");

    script.Append("if (window.ctrl_down)");
    script.Append("  return true;");

    script.Append("for (var i = 0, imax = allowedKeyCodes.length; i < imax; i++)");
    script.Append("  if (event.keyCode == allowedKeyCodes[i])");
    script.Append("    return true;");

    // Prevent keydown event when length surpassed max length
    script.AppendFormat("if (this.value.length >= {0}) {{", field.MaxLength);
    script.Append("event.preventDefault();");
    script.Append("}");

    return script.ToString();
}

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions