11//= require jquery.timeago.js
22
33$ ( document ) . ready ( function ( ) {
4- $ ( "form.answer" ) . submit ( function ( ) {
4+ /*--------------
5+ START Autosaving
6+ ----------------*/
7+ // debounced object holds a set of debounced functions, one for each form present in the page. Note,
8+ // each debounced function stored at funcs is created on demand, i.e. once the user changes any element of a form
9+ var debounced = ( function ( ) {
10+ var funcs = { } ;
11+ return {
12+ has : function ( id ) {
13+ return funcs [ id ] !== undefined ;
14+ } ,
15+ get : function ( id ) {
16+
17+ return funcs [ id ] ;
18+ } ,
19+ set : function ( id , func ) {
20+ funcs [ id ] = DMPROADMAP . debounce ( func , 5000 ) ;
21+ }
22+ }
23+ } ) ( ) ;
24+ // This function triggers a form submit, if and only if the answer has not been optimistically locked
25+ function autoSaving ( ) {
26+ if ( $ ( this ) . closest ( '.question-form' ) . find ( '.answer-locking' ) . children ( ) . length === 0 ) {
27+ $ ( this ) . closest ( 'form.answer' ) . submit ( ) ;
28+ }
29+ } ;
30+ /*--------------
31+ END Autosaving
32+ ----------------*/
33+ // Listener for submit event triggered
34+ $ ( '.question-form' ) . on ( 'submit' , 'form.answer' , function ( ) {
35+ var id = $ ( this ) . attr ( 'data-autosave' ) ;
36+ if ( debounced . has ( id ) ) {
37+ debounced . get ( id ) . cancel ( ) ; //Cancels the execution of its debounced function, if not already, since submit() could have been trigerred through Save button
38+ }
539 var container = $ ( this ) . closest ( '.question-form' ) ;
640 var saving = container . find ( '.saving-message' ) ;
741 saving . show ( ) ;
842 } ) ;
9- $ ( "form.answer fieldset input, form.answer fieldset select" ) . change ( function ( ) {
43+ // Listener for changes at any element value from question-form
44+ $ ( '.question-form' ) . on ( 'change' , 'form.answer fieldset input, form.answer fieldset select' , function ( ) {
1045 var unsaved = $ ( this ) . closest ( '.question-form' ) . find ( '.answer-unsaved' ) ;
1146 unsaved . show ( ) ;
1247 var notAnswered = $ ( this ) . closest ( '.question-form' ) . find ( '.not-answered' ) ;
1348 notAnswered . hide ( ) ;
1449 } ) ;
15- $ . fn . change_answer = function ( editor ) {
16- editor . on ( 'change' , function ( event ) {
50+ // Listener for changes at any element value from question-form. This triggers the debounced function
51+ $ ( '.question-form' ) . on ( 'change' , 'form.answer fieldset input, form.answer fieldset select' , function ( ) {
52+ var id = $ ( this ) . closest ( 'form.answer' ) . attr ( 'data-autosave' ) ;
53+ if ( ! debounced . has ( id ) ) {
54+ debounced . set ( id , autoSaving ) ;
55+ }
56+ debounced . get ( id ) . apply ( $ ( this ) , [ id ] ) ;
57+ } ) ;
58+ // Function bounded to Jquery scope that setup event handlers for tinymce instances
59+ $ . fn . tinymce_answer_events = function ( editor ) {
60+ editor . on ( 'change' , function ( ) {
1761 var unsaved = $ ( '#' + editor . id ) . closest ( '.question-form' ) . find ( '.answer-unsaved' ) ;
1862 unsaved . show ( ) ;
1963 var notAnswered = $ ( '#' + editor . id ) . closest ( '.question-form' ) . find ( '.not-answered' ) ;
2064 notAnswered . hide ( ) ;
2165 } ) ;
66+ editor . on ( 'blur' , function ( ) {
67+ var id = $ ( '#' + editor . id ) . closest ( 'form.answer' ) . attr ( 'data-autosave' ) ;
68+ $ ( '#' + editor . id ) . val ( $ ( '#' + editor . id ) . tinymce ( ) . getContent ( ) ) ; // Forces Updating content textarea with the value from tinymce
69+ if ( ! debounced . has ( id ) ) {
70+ debounced . set ( id , autoSaving ) ;
71+ }
72+ debounced . get ( id ) . apply ( $ ( '#' + editor . id ) , [ id ] ) ;
73+ } ) ;
74+ editor . on ( 'focus' , function ( ) {
75+ var id = $ ( '#' + editor . id ) . closest ( 'form.answer' ) . attr ( 'data-autosave' ) ;
76+ if ( debounced . has ( id ) ) {
77+ debounced . get ( id ) . cancel ( ) ; //Cancels the execution of its debounced function either because user transitioned from question with options
78+ // to the comments or because textarea lost focus and gained again before the delay being met
79+ }
80+ } ) ;
2281 }
2382 $ . fn . init_answer_status = function ( ) {
24- $ ( 'abbr.timeago' ) . timeago ( ) ;
83+ $ ( 'abbr.timeago' ) . timeago ( ) ; //TODO examine if possible refactoring as event-delegated
2584 }
2685 $ . fn . init_answer_status ( ) ;
2786} ) ;
0 commit comments