Skip to content

Commit

Permalink
a merge implementation helper: "stash/unstash" board operations:
Browse files Browse the repository at this point in the history
 - added _dbg() function for dev/mode console output
 - added a "message" argument to updatePageTitle() to reflect the last state change in the page title
 - added a '.stashed' field to the main NB object to "save to" / "load from" a copy of the board
  • Loading branch information
gf-mse committed Aug 5, 2022
1 parent 776b2f1 commit 0ddc4c0
Showing 1 changed file with 152 additions and 5 deletions.
157 changes: 152 additions & 5 deletions nullboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@

<a href=# class=auto-backup>Auto-backup...</a>

<a href=# class=stash-board-local> =&gt; Stash board</a>
<a href=# class=unstash-board-local> &lt;= Unstash board</a>

<div class="section ui-prefs break">
<a href=# class=title>UI preferences<u>...</u></a>
<div class=details>
Expand Down Expand Up @@ -2656,6 +2659,68 @@ <h3>Auto-backup</h3>

// console.log(uuidv4());

// -----------------------------------------------------------------------

// NB: in this section, camelCase() are methods,
// and snake_case() are common functions

function shallowClone() {
return Object.assign(new this.constructor(), this);
};

// ---------------------------------------------------------------

function clone_note_shallow(n) {
return shallowClone.call(n);
};

function clone_note_deep(n) {
return clone_note_shallow(n);
};

// ---------------------------------------------------------------

function clone_list_shallow(L) {
return shallowClone.call(L);
};

function deepCloneList() {
var result = clone_list_shallow(this);
result.notes = [];

for (var e of this.notes) {
result.notes.push(clone_note_deep(e));
}

return result;
};

function clone_list_deep(L) {
return deepCloneList.call(L);
}

// ---------------------------------------------------------------

function clone_board_shallow(b) {
return shallowClone.call(b);
};

function deepCloneBoard() {
var result = clone_board_shallow(this);
result.lists = [];

for (var e of this.lists) {
result.lists.push(clone_list_deep(e));
}

return result;
}

function clone_board_deep(b) {
return deepCloneBoard.call(b);
}


// -----------------------------------------------------------------------

function Note(text, id)
Expand Down Expand Up @@ -2707,6 +2772,34 @@ <h3>Auto-backup</h3>

}


// -----------------------------------------------------------------------
// -----------------------------------------------------------------------

// todo: clear it out when done -- or adjust to use the current convention
_DEBUG = 1; // yes, we want it global
function _dbg(text) {
if (_DEBUG) {
console.log(`# [dbg] ${text}`);
}
}

function _dump(expression, comment) {
if (_DEBUG) {
// console.log(`ctor: ${expression.constructor}`);
if (expression.constructor === String) {
console.log(`# [dbg] /${expression}/ =>`);
console.log(eval(expression));
} else {
if (comment) { console.log(`${comment}:`); }
console.log(expression);
}
}
}

// # try to add this at the end :
// _dump('NB');

</script>

<script type="text/javascript">
Expand Down Expand Up @@ -3326,7 +3419,7 @@ <h3>Auto-backup</h3>
n.old = $note.hasClass('old');
n.marked = $note.hasClass('mark');
n.new = $note.hasClass('new');
//
//
n.min = $note.hasClass('collapsed');

// n.id = + $note.attr('data-id');
Expand Down Expand Up @@ -3441,7 +3534,7 @@ <h3>Auto-backup</h3>
if (n.old) $n.addClass('old');
if (n.new) $n.addClass('new');
if (n.marked) $n.addClass('mark');
//
//
if (n.min) $n.addClass('collapsed');
$l_notes.append($n);

Expand Down Expand Up @@ -3515,6 +3608,27 @@ <h3>Auto-backup</h3>
return Object.assign(new Board(), demo);
}

/*
* copying a board structure to a "stashed" location
*/
function stashBoardCopyLocal(nb_ref)
{
if (nb_ref.board) {
// var board = nb_ref.board.deepClone();
var board = clone_board_deep(nb_ref.board);
nb_ref.stashed = board;

_dbg(`stashBoardCopyLocal(): stashed a clone of board ${nb_ref.board.id}`);
updatePageTitle(`stashed ${board.id}.${board.revision} (Ok)`);
} else {
alert("stashBoardCopyLocal(): no board to stash!");
return;
}

// just in case
return board; // 'var' in JavaScript has function scope, right?
}

/*
* board export / import
*/
Expand Down Expand Up @@ -3972,16 +4086,19 @@ <h3>Auto-backup</h3>
/*
*
*/
function updatePageTitle()
function updatePageTitle(message)
{
var title = 'Nullboard';

if (NB.board)
{
title = NB.board.title || '(untitled board)' ;
// title = 'NB - ' + (title || '(untitled board)');
title = NB.board.title || '(untitled board)' ;
// title = 'NB - ' + (title || '(untitled board)');
title = 'NB - ' + title + " - " + NB.board.id + '.' + NB.board.revision ;

if (message) {
title = title + ' | ' + message
}
}

document.title = title;
Expand Down Expand Up @@ -5036,6 +5153,33 @@ <h3>Auto-backup</h3>
return false;
});

// --------------------------------------------------

$('.config .stash-board-local').on('click', function(){
// NB.stashed =
stashBoardCopyLocal(NB);
});

$('.config .unstash-board-local').on('click', function(){
var stashed = NB.stashed;
if (stashed) {
console.log("[info] loading stashed board: ", stashed);
stashed.revision ++ ;
NB.board = stashed;

NB.storage.saveBoard(stashed);
NB.storage.setActiveBoard(stashed.id);

showBoard(true);

updatePageTitle('(unstashed)');
} else {
alert("Please stash a board before it can be loaded!");
}
});

// --------------------------------------------------

//
$('.config .f-prefs .ui-fs .less').on('click', function(){
setFontSize( parseInt(10*getFontSize()) / 10. - 0.5 );
Expand Down Expand Up @@ -5157,6 +5301,9 @@ <h3>Auto-backup</h3>
board: null,
storage: null,

// debug merging
stashed: null, // a board clone if set

peek: function(name)
{
return this.storage.getConfig()[name];
Expand Down

0 comments on commit 0ddc4c0

Please sign in to comment.