Project

General

Profile

« Previous | Next » 

Revision d0ae3939

Added by koszko about 2 years ago

enable opening settings page with certain item immediately in edit mode

View differences:

html/options.html
123 123
      </li>
124 124
    </div>
125 125

  
126
    <!-- Mind the show_*s ids below - their format is assumed in js code -->
126 127
    <input type="radio" name="tabs" id="show_pages" checked></input>
127 128
    <input type="radio" name="tabs" id="show_bags"></input>
128 129
    <input type="radio" name="tabs" id="show_scripts"></input>
html/options_main.js
11 11
 * IMPORT TYPE_PREFIX
12 12
 * IMPORT TYPE_NAME
13 13
 * IMPORT list_prefixes
14
 * IMPORT url_extract_target
14 15
 * IMPORTS_END
15 16
 */
16 17

  
......
304 305

  
305 306
    let ul = ul_by_prefix[prefix];
306 307
    let li = by_id(item_li_id(prefix, item));
308

  
309
    if (li === null) {
310
	add_new_item(prefix, item);
311
	return;
312
    }
313

  
307 314
    ul.reset_work_li(ul, item, storage.get(prefix, item));
308 315
    ul.ul.insertBefore(ul.work_li, li);
309 316
    ul.ul.removeChild(li);
......
357 364
    URL.revokeObjectURL(url);
358 365
}
359 366

  
360
function add_new_item(prefix)
367
function add_new_item(prefix, name)
361 368
{
362 369
    cancel_work(prefix);
363 370

  
......
366 373
    ul.work_li.classList.remove("hide");
367 374
    ul.ul.appendChild(ul.work_li);
368 375

  
376
    if (name !== undefined)
377
	ul.work_name_input.value = name;
369 378
    ul.state = UL_STATE.ADDING_ENTRY;
370 379
}
371 380

  
......
672 681
    cancel_import_but.addEventListener("click", hide_import_window);
673 682
}
674 683

  
684
/*
685
 * If url has a target appended, e.g.
686
 * chrome-extension://hnhmbnpohhlmhehionjgongbnfdnabdl/html/options.html#smyhax
687
 * that target will be split into prefix and item name (e.g. "s" and "myhax")
688
 * and editing of that respective item will be started.
689
 *
690
 * We don't need to worry about the state of the page (e.g. some editing being
691
 * in progress) in jump_to_item() - this function is called at the beginning,
692
 * before callbacks are assigned to buttons, so it is safe to assume lists are
693
 * initialized with items and page is in its virgin state with regard to
694
 * everything else.
695
 */
696
function jump_to_item(url_with_item)
697
{
698
    const parsed_url = url_extract_target(url_with_item);
699

  
700
    if (parsed_url.target === undefined)
701
	return;
702

  
703
    const prefix = parsed_url.target.substring(1, 2);
704

  
705
    if (!list_prefixes.includes(prefix)) {
706
	history.replaceState(null, "", parsed_url.base_url);
707
	return;
708
    }
709

  
710
    by_id(`show_${TYPE_NAME[prefix]}s`).checked = true;
711
    edit_item(prefix, decodeURIComponent(parsed_url.target.substring(2)));
712
}
713

  
675 714
async function main()
676 715
{
677 716
    storage = await get_remote_storage();
......
683 722
	    add_radio_li(prefix, item);
684 723
	}
685 724

  
725
	jump_to_item(document.URL);
726

  
686 727
	let name = TYPE_NAME[prefix];
687 728

  
688 729
	let add_but = by_id(`add_${name}_but`);

Also available in: Unified diff