| 1 |
/** |
| 2 |
* Builder UX: unsaved-changes guard, mobile shell hint, header title sync with document title field. |
| 3 |
*/ |
| 4 |
(function ($) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
var HINT_KEY = 'ei_builder_shell_hint_v1'; |
| 8 |
|
| 9 |
function getI18n(key, fallback) { |
| 10 |
if (window.easyInvoice && easyInvoice.i18n && easyInvoice.i18n[key]) { |
| 11 |
return easyInvoice.i18n[key]; |
| 12 |
} |
| 13 |
return fallback; |
| 14 |
} |
| 15 |
|
| 16 |
function setupDirtyGuard() { |
| 17 |
var $forms = $('#invoice-form, #quote-form'); |
| 18 |
if (!$forms.length) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
window.eiBuilderDirty = false; |
| 23 |
|
| 24 |
function markDirty() { |
| 25 |
window.eiBuilderDirty = true; |
| 26 |
} |
| 27 |
|
| 28 |
$(document).on('input change', '#invoice-form :input, #quote-form :input', function () { |
| 29 |
markDirty(); |
| 30 |
}); |
| 31 |
|
| 32 |
$(document).on('easy-invoice-saved', function (e, response) { |
| 33 |
if (response && response.success) { |
| 34 |
window.eiBuilderDirty = false; |
| 35 |
} |
| 36 |
}); |
| 37 |
$(document).on('easy-quote-saved', function (e, response) { |
| 38 |
if (response && response.success) { |
| 39 |
window.eiBuilderDirty = false; |
| 40 |
} |
| 41 |
}); |
| 42 |
|
| 43 |
$(window).on('beforeunload', function (e) { |
| 44 |
if (!window.eiBuilderDirty) { |
| 45 |
return; |
| 46 |
} |
| 47 |
e.preventDefault(); |
| 48 |
e.returnValue = getI18n( |
| 49 |
'builder_unsaved_warning', |
| 50 |
'You have unsaved changes. If you leave this page, your changes may be lost.' |
| 51 |
); |
| 52 |
return e.returnValue; |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
function setupShellHint() { |
| 57 |
var $box = $('#ei-builder-shell-hint'); |
| 58 |
if (!$box.length) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
function dismissed() { |
| 63 |
try { |
| 64 |
return window.localStorage.getItem(HINT_KEY) === '1'; |
| 65 |
} catch (err) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
function isLarge() { |
| 71 |
return window.matchMedia && window.matchMedia('(min-width: 1024px)').matches; |
| 72 |
} |
| 73 |
|
| 74 |
function apply() { |
| 75 |
if (dismissed() || isLarge()) { |
| 76 |
$box.addClass('hidden').attr('hidden', 'hidden'); |
| 77 |
return; |
| 78 |
} |
| 79 |
$box.removeClass('hidden').removeAttr('hidden'); |
| 80 |
} |
| 81 |
|
| 82 |
apply(); |
| 83 |
$(window).on('resize', apply); |
| 84 |
|
| 85 |
$(document).on('click', '#ei-builder-shell-hint-dismiss', function () { |
| 86 |
try { |
| 87 |
window.localStorage.setItem(HINT_KEY, '1'); |
| 88 |
} catch (err) {} |
| 89 |
$box.addClass('hidden').attr('hidden', 'hidden'); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
function setupHeaderTitleSync() { |
| 94 |
var $h = $('#ei-builder-header-title'); |
| 95 |
var $inp = $('#invoice-form input[name="title"], #quote-form input[name="title"]').first(); |
| 96 |
if (!$h.length || !$inp.length) { |
| 97 |
return; |
| 98 |
} |
| 99 |
var def = $h.attr('data-ei-default') || ''; |
| 100 |
|
| 101 |
function apply() { |
| 102 |
var v = ($inp.val() || '').trim(); |
| 103 |
$h.text(v || def); |
| 104 |
} |
| 105 |
|
| 106 |
$inp.on('input', apply); |
| 107 |
} |
| 108 |
|
| 109 |
$(function () { |
| 110 |
setupDirtyGuard(); |
| 111 |
setupShellHint(); |
| 112 |
setupHeaderTitleSync(); |
| 113 |
}); |
| 114 |
})(jQuery); |
| 115 |
|