Skip to content

Commit

Permalink
Bug SAFeSEA#30/SAFeSEA#22, Move validator Javascript from 'draft.hist…
Browse files Browse the repository at this point in the history
…ory` TWIG [iet:10299492]

* Plus, QA fixes to JS [iet:10289026][iet:10294465]
  • Loading branch information
nfreear committed Feb 2, 2018
1 parent 520e6e7 commit c0a67cb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"semistandard": "^12.0.0"
},
"semistandard": {
"ignore": [ ".cache", ".logs", "app", "templates", "public_html", "hive_*.js", "keyword.group.js" ]
"ignore": [ ".cache/", ".logs/", "app/", "templates/", "X-public_html/", "hive_*.js", "keyword.group.js", "bootstrap*", "jquery*" ]
},
"scripts": {
"symlink-js": "ln -s public_html/assets/openessayist/js openessayist-js",
"test": "semistandard",
"test": "semistandard public_html/assets/openessayist/js/open*.js",
"fix": "semistandard --fix"
}
}
12 changes: 6 additions & 6 deletions public_html/assets/openessayist/js/openessayist.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
OpenEssayist javascript | © The Open University (IET).
*/

// eslint-disable-next-line
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), // eslint-disable-line
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) // eslint-disable-line
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');// eslint-disable-line

// From: base.html.twig.
// <script id="openessayist-init" type="text/javascript">
Expand All @@ -17,7 +17,7 @@ window.jQuery(function ($) {
var L = window.location;
var ga = window.ga;

// Set the URL for the AJAX-based activity logging
// Set the URL for the AJAX-based activity logging.
// Was: openEssayist.LOG_URL = "{{ urlFor('ajax.log.activity') }}";
openEssayist.config = $('script[ data-openessayist ]').data();
var CFG = openEssayist.config;
Expand All @@ -28,7 +28,7 @@ window.jQuery(function ($) {

ga('send', 'pageview', CFG.analytics_prefix + L.pathname);

// Activate the Bootstrap's tooltips
// Activate Bootstrap tooltips.
if ($('[ rel = tooltip ]').length) {
$('[ rel = tooltip ]').tooltip({ container: 'body' });
}
Expand Down
63 changes: 63 additions & 0 deletions public_html/assets/openessayist/js/openessayist.validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*!
OpenEssayist javascript | © The Open University (IET).
*/

// From: user/draft.submit.twig.

window.jQuery(function ($) {
$.blockUI.defaults.baseZ = 1100; // Z-index?

var $form = $('form#post_essay');
var $essay = $form.find('textarea#text');
var $counts = $form.find('[ name = counts ]');
var minlength = $essay.data('minlength') || 200;
var maxlength = $essay.data('maxlength') || (200 * 1000); // Characters. Big!

console.warn('Essay validator (min/maxlength chars):', minlength, maxlength, $form, $essay, $counts);

$form.validate({
rules: {
text:
{
required: true,
minlength: minlength,
maxlength: maxlength
}
},
submitHandler: function (form) {
var essay = $essay.val();
var wcount = essay.match(/[^\s]+/g).length;
var time = wcount / 100;
var msg = '';

if (time < 30) {
msg = 'it will take 15 to 30 seconds...';
} else if (time < 60) {
msg = 'it will take up to one minute ...';
} else {
msg = 'it will take one or two minutes ...';
}

$.blockUI({ message: '<div class="ajax alert alert-info">Starting Analysis. Please wait, ' + msg + '</div>' });
form.submit();
},
highlight: function (element) {
$(element).closest('.control-group').not('.help-block').removeClass('success').addClass('error');
},
success: function (element) {
element
.text('OK!').addClass('valid')
.closest('.control-group').not('.help-block').removeClass('error').addClass('success');
}
});

// NDF, 02-February-2018.
window.Countable.on($essay.get(0), function (counter) {
console.warn('Counts:', counter);

$counts.val(JSON.stringify(counter));
},
{ hardReturns: true });
});

// End.
48 changes: 7 additions & 41 deletions templates/user/draft.submit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<input type="hidden" name="task" id="draft-task" value="{{ task.code }}"/>
<input type="hidden" name="version" id="draft-version" value="{{ form.version }}"/>

<input type="hidden" readonly name="counts" id="draft-counts" title="*** TO HIDE *** work-in-progress!" style="width:99%" />
<input type="hidden" name="counts" id="draft-counts"/>
</div>
</div>

Expand Down Expand Up @@ -134,52 +134,17 @@
<script src="{{ app_base }}/assets/jquery.validation/jquery.validate.min.js"></script>
<script src="https://unpkg.com/[email protected]/Countable.min.js"></script>

<script>
<script src="{{ app_base }}/assets/openessayist/js/openessayist.validator.js"></script>

{# <script>
window.jQuery(function ($) {
$.blockUI.defaults.baseZ = 1100; // Z-index?
var $form = $('form#post_essay');
var $essay = $form.find('textarea#text');
var $counts = $form.find('[ name = counts ]');
var minlength = $essay.data('minlength') || 200;
var maxlength = $essay.data('maxlength') || ( 200 * 1000 ); // Characters. Big!
$('#post_essay').validate({
rules: {
text:
{
required: true,
minlength: minlength,
maxlength: maxlength
}
},
submitHandler: function(form) {
var essay = $essay.val();
var wcount = essay.match(/[^\s]+/g).length;
var time = wcount / 100;
var msg = "";
if (time < 30)
msg = "it will take 15 to 30 seconds...";
else if (time < 60)
msg = "it will take up to one minute ...";
else
msg = "it will take one or two minutes ...";
$.blockUI({ message: '<div class="ajax alert alert-info">Starting Analysis. Please wait, ' + msg + '</div>' });
form.submit();
},
highlight: function(element) {
$(element).closest('.control-group').not(".help-block").removeClass('success').addClass('error');
},
success: function(element) {
element
.text('OK!').addClass('valid')
.closest('.control-group').not(".help-block").removeClass('error').addClass('success');
}
});
// ...
// NDF, 02-February-2018.
window.Countable.on($essay.get(0), function (counter) {
Expand All @@ -191,6 +156,7 @@ window.jQuery(function ($) {
});
</script>
#}
{% endblock %}

{% block injectCSS %}
Expand Down

0 comments on commit c0a67cb

Please sign in to comment.