Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mooeypoo
Last active January 9, 2019 22:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mooeypoo/48514d2841da5083c5b0 to your computer and use it in GitHub Desktop.
Save mooeypoo/48514d2841da5083c5b0 to your computer and use it in GitHub Desktop.
Anodyne Nova Mod - Add a wordcount to mission posts
<?php
/**
* Add word count to Nova mission posts.
* @author Moriel Schottlender mooeypoo@gmail.com
*
* This content should go in the write_missionpost_js.php page in your
* `application/views/_base_override/admin/js/` folder. If the file does not yet exist in
* that folder, you can create it by copy/pasting the entire content of this page
* and saving it as the `write_missionpost_js.php` file.
* If the file already exists, copy over everything between the <script ...> </script>
* tags.
*/
?>
<?php
/**
* DIRTYTRICK: Extend the existing javascript page so we don't need to copy its
* code over here and maintain it on each upgrade.
*/
include( MODPATH.'core/views/_base/admin/js/write_missionpost_js.php' );
?>
<script type="text/javascript">
/**
* Add a wordcount counter to the mission post box with a friendly
* 'warning' when a post goes over some limit that is defined in the settings.
*
* If no limit is defined, the code will fall back to having no limit
* message at all.
*
* NOTE: This does not actually limit users in anything; the 'limit' is simply
* a way to show a note to the user encouraging them to split the post to parts.
*
* INSTALLATION:
* In order to set a word limit, please add these setting keys to user-defined
* settings in Nova:
* * 'logcountLimit' (Numerical) The number of words to limit. 0 for no limit.
* * 'logcountLimitMessage' (String) The message to display in case the post goes
* above the limit.
*
* @author Moriel Schottlender mooeypoo@gmail.com
*/
$( document ).ready( function (){
var logcountLimit, logcountLimitMessage,
$wordCountContainer,
$textarea = $( '#writepost #content-textarea' ),
calcWordCount = function ( text ) {
var words, chars;
text = $.trim( text );
if ( text.length === 0 ) {
words = 0;
chars = 0;
} else {
words = text.replace(/\s+/gi, ' ').split(' ').length || 0,
chars = text.length
}
return {
'w': words,
'c': chars
};
},
updateWordcountMessage = function ( wordCountObject ) {
var aboveLimit, splitMsg;
wordCountObject = wordCountObject || { 'w': 0, 'c': 0 };
aboveLimit = logcountLimit > 0 && wordCountObject.w >= logcountLimit;
splitMsg = aboveLimit ? logcountLimitMessage : '';
// Update message
$wordCountContainer
.text( wordCountObject.w + ' words, ' + wordCountObject.c + ' characters. ' + splitMsg )
.toggleClass( 'flash-error', aboveLimit );
};
// Initialize
<?php
// Take the settings from Nova settings, or fall back to defaults.
if ( strlen( $this->settings->get_setting( 'logcountLimitMessage' ) ) > 0 ) {
$logcountLimitMessage = '"'.htmlspecialchars($this->settings->get_setting( 'logcountLimitMessage' )).'"';
} else {
// Default message
$logcountLimitMessage = "' Please consider splitting the post.'";
}
if ( intval( $this->settings->get_setting( 'logcountLimit' )) > 0 ) {
$logcountLimit = intval( $this->settings->get_setting( 'logcountLimit' ) );
} else {
// Default word limit is 0 (off)
$logcountLimit = 0;
}
?>
logcountLimit = <?php echo $logcountLimit; ?>;
logcountLimitMessage = <?php echo $logcountLimitMessage; ?>;
$wordCountContainer = $( '<div>' )
.addClass( 'content-wordcount' );
// Attach to document
$textarea.parent().append( $wordCountContainer );
// Initial count
updateWordcountMessage( calcWordCount( $textarea.val() ) );
// Update on input
$textarea.on( 'input', function() {
updateWordcountMessage( calcWordCount( $( this ).val() ) );
} );
} );
</script>
@ravensghost
Copy link

Hi I'm having problem with this code:

  1. Can't get it to display on mission post/ personal logs
  2. Every time I try to add the message variable it cause the site to crash and go white screen, is it meant to go in the nova controller?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment