diff -urp OriModules/maxlength/maxlength.inc sites/all/modules/maxlength/maxlength.inc --- OriModules/maxlength/maxlength.inc 2009-04-02 02:52:52.000000000 +0300 +++ sites/all/modules/maxlength/maxlength.inc 2009-09-16 22:17:06.000000000 +0300 @@ -10,3 +10,7 @@ define('MAXLENGTH_NODE_TYPE', 'maxlength // Permissions define('ADMINISTER_MAXLENGTH', 'administer maxlength'); + +// Permissions +define('MAXLENGTH_LIMIT_CHARACTERS', 0); +define('MAXLENGTH_LIMIT_WORDS', 1); diff -urp OriModules/maxlength/maxlength.js sites/all/modules/maxlength/maxlength.js --- OriModules/maxlength/maxlength.js 2008-09-29 03:14:48.000000000 +0300 +++ sites/all/modules/maxlength/maxlength.js 2009-09-16 23:35:23.000000000 +0300 @@ -2,16 +2,33 @@ Drupal.maxLength_limit = function (field) { var limit = field.attr("limit"); + var limittype = field.attr("limittype"); - // calculate the remaining count of chars - var remainingCnt = limit - field.val().length; + if (limittype == 0) { // Characters + // calculate the remaining count of chars + var remainingCnt = limit - field.val().length; + + // if there is not remaining char, we clear additional content + if (remainingCnt < 0) { + field.val(field.val().substr(0, limit)); + remainingCnt = 0; + } + } else { // Words + words = $.trim(field.val()).split(/\s+/).length; - // if there is not remaining char, we clear additional content - if (remainingCnt < 0) { - field.val(field.val().substr(0, limit)); - remainingCnt = 0; + remainingCnt = limit - words; + if (words > limit) { + //alert(Drupal.t("Sorry, you reached the maximum amount of words. \n\n The last word in your text will be deleted.")); + while (words > limit) { + less = field.attr("value"); + less = less.slice ( 0, less.length - 1); + field.attr("value", less) ; + words = $.trim(field.val()).split(/\s+/).length; + } + remainingCnt = 0; + } } - + // update the remaing chars text $('#maxlength-'+field.attr('id').substr(5) + ' span.maxlength-counter-remaining').html(remainingCnt.toString()); } @@ -26,8 +43,11 @@ if (Drupal.jsEnabled) { // get all the settings, and save the limits in the fields for (var id in Drupal.settings.maxlength) { var limit = Drupal.settings.maxlength[id]; + var limittype = Drupal.settings.limittype[id]; + var element = $("#"+ id); element.attr("limit", limit); + element.attr("limittype", limittype); // update the count at the page load Drupal.maxLength_limit(element); diff -urp OriModules/maxlength/maxlength.module sites/all/modules/maxlength/maxlength.module --- OriModules/maxlength/maxlength.module 2009-06-06 20:53:32.000000000 +0300 +++ sites/all/modules/maxlength/maxlength.module 2009-09-16 23:57:47.000000000 +0300 @@ -50,9 +50,20 @@ function maxlength_form_alter(&$form, &$ variable_del(MAXLENGTH_NODE_TYPE . $form['field_name']['#value']); variable_del(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_js'); variable_del(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_text'); + variable_del(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_limittype'); + } + // If the editablefields module is installed, this will + // add the word-counter to editablefields. + if ($form_id == 'editablefields_form') { + _maxlength_content_form_alter(&$form, &$form_state, $form_id); } } +function _maxlength_editablefields_form_alter(&$form, &$form_state, $form_id) { + $type = $form['type']['#value']; + _maxlength_content_form_alter(&$form, &$form_state, $form_id); +} + function _maxlength_content_form_alter(&$form, &$form_state, $form_id) { $type = $form['type']['#value']; // update the title as needed @@ -110,6 +121,16 @@ function _maxlength_content_type_form_al '#default_value' => variable_get(MAXLENGTH_NODE_TYPE . $label .'_js_'. $type, '0'), '#description' => t('This will enable a Javascript based count down, as well as the client side validation for the !type field of this content type. If no limit set this is ignored.', array('!type' => $label)), ); + $form['submission'][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_limittype'] = array( + '#type' => 'radios', + '#title' => t('Type of limitation for !label', array('!label' => ucwords($label))), + '#options' => array( + t('Characters'), + t('Words'), + ), + '#default_value' => variable_get(MAXLENGTH_NODE_TYPE . $label .'_limittype', MAXLENGTH_LIMIT_CHARACTERS), + '#description' => t('This will determine if the maxlength is used for limiting the content of !type in characters or words.', array('!type' => $label)), + ); $form['submission'][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_text'] = array( '#type' => 'textarea', '#title' => t('!label count down message', array('!label' => ucwords($label))), @@ -133,6 +154,17 @@ function _maxlength_cck_form_alter(&$for '#description' => t('This will enable a Javascript based count down, as well as the client side validation for this field. If no limit set this is ignored.'), ); + $new_fields[MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_limittype'] = array( + '#type' => 'radios', + '#title' => t('Type of limitation for !label', array('!label' => ucwords($label))), + '#options' => array( + t('Characters'), + t('Words'), + ), + '#default_value' => variable_get(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_limittype', MAXLENGTH_LIMIT_CHARACTERS), + '#description' => t('This will determine if the maxlength is used for limiting the content of !type in characters or words.', array('!type' => $label)), + ); + $new_fields[MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_text'] = array( '#type' => 'textarea', '#title' => t('Count down message'), @@ -150,6 +182,7 @@ function _maxlength_cck_form_submit($for //note, max lenght for the CCK field is stored in this way as for textareas, its not in $element var passed to theme functions. variable_set(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'], $form['#post']['max_length']); variable_set(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_js', $form['#post'][MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_js']); + variable_set(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_limittype', $form['#post'][MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_limittype']); variable_set(MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_text', $form['#post'][MAXLENGTH_NODE_TYPE . $form['field_name']['#value'] .'_text']); } @@ -199,7 +232,11 @@ function _maxlength_format_element(&$ele 'maxlength' => array( 'edit-'. $id => $values['limit'], ), - ); + 'limittype' => array( + 'edit-'. $id => $values['limittype'], + ), + ); + drupal_set_message("format element limit type". $values['limittype']); drupal_add_js($js_settings, 'setting'); $element['#suffix'] = '