Keep current scroll position at object save

Use following script to keep current scroll position at object save :

(function(ui) {
    // ========= SIMPLICITE BOILERPLATE ===========
	if (!ui) return;
	var app = ui.getAjax();   
	Simplicite.UI.hooks.MyObject = function(o, cbk) {
		try {
			var p = o.locals.ui;
			if (p && o.isMainInstance()) {
				p.form.onload = function(ctn, obj) {
					// ========= CALLING SPECIFIC FUNCTIONS ===========
					followScroll();
					// ====================
				};
			}
		}
		catch(e) {
			app.error("Error in Simplicite.UI.hooks.MyObject: " + e.message);
		}
		finally {
			cbk && cbk()
		}
	};
	
	// ========= SPECIFIC FUNCTIONS ===========
	function followScroll(){
		let STORAGE_VAR = 'rpt_y_position';
		if(localStorage.getItem(STORAGE_VAR)){
			$('#work').scrollTop(localStorage.getItem(STORAGE_VAR));
			localStorage.removeItem(STORAGE_VAR);
		}
		$('.btn-save').click(()=>localStorage.setItem(STORAGE_VAR, $('#work').scrollTop()));
	}
})(window.$ui);
2 Likes

Good idea.

CTRL-S is also supported because it triggers the click event.
Another version with a local variable instead of local storage :

	// ========= CALLING SPECIFIC FUNCTIONS ===========
	followScroll('#work');
	...

	// ========= SPECIFIC FUNCTIONS ===========
	var rpt_y_position = null;
	function followScroll(ctn) {
		if (rpt_y_position) {
			$(ctn).scrollTop(rpt_y_position);
			rpt_y_position = null;
		}
		$('.btn-save', ctn).click(() => rpt_y_position = $(ctn).scrollTop()));
	}

Managing inner object list

function followScroll(){
    let STORAGE_VAR = 'rpt_y_position';
    let savePosition = ()=>localStorage.setItem(STORAGE_VAR, $('#work').scrollTop());
    if(localStorage.getItem(STORAGE_VAR)){
        $('#work').scrollTop(localStorage.getItem(STORAGE_VAR));
        localStorage.removeItem(STORAGE_VAR);
    }
    $('.btn-save').click(savePosition);
    
    let crBtn = action => 'div[data-object="InnerObject"] button[data-action="'+action+'"]';
    setTimeout(()=>$(crBtn('listedit')+','+crBtn('addlist')).click(()=>{
        setTimeout(()=>$(crBtn('save')+','+crBtn('cancel')).click(savePosition),1000);
    }),1000);
}

Maybe this should be the default behaviour (without using the localStorage) ?

1 Like