function scrollToTop() { var $root = $('html, body'); $root.animate({ scrollTop: 0 }, 500); } var enableLeaveWarning = false; $(function() { // bootstrap $.fn.typeahead.Constructor.prototype.customLookup = function (event) { var items this.query = this.$element.val() if (this.query.length < this.options.minLength) { return this.shown ? this.hide() : this } items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source return items ? this.process(items) : this }; // Leave confirm $('input, textarea, select').change(function() { var $this = $(this), readonly = $this.prop('readonly'), disabled = $this.prop('disabled'); if (!readonly && !disabled) { // ignore search form var $form = $this.closest('form'); if ($form.length) { var id = $form.attr('id'); if (id != 'search_form') { enableLeaveWarning = true; } } } }); $(window).bind('beforeunload', function(e) { if (enableLeaveWarning) { return '★ Data still NOT SAVED, are you want to leave this page? ★'; } }); var checkedSubmit = false, checkedSubmitLastEnableLeaveWarning = false; $(':submit').click(function() { checkedSubmit = true; checkedSubmitLastEnableLeaveWarning = enableLeaveWarning; enableLeaveWarning = false; }); // validator $.validator.setDefaults({ errorClass: 'text-error', invalidHandler: function(event, validator) { var errors = validator.numberOfInvalids(); if (errors) { if (checkedSubmit) { enableLeaveWarning = checkedSubmitLastEnableLeaveWarning; checkedSubmit = false; } validator.showErrors(); // show errors before fix IE placeholder position if ($.browser.msie) { $(window).resize(); // fix IE placeholder position } } } }); // select2 $.fn.select2.defaults = $.extend({}, $.fn.select2.defaults, { minimumInputLength: 0, allowClear: true, placeholder: 'Please Select...' }); $(document).on('blur', 'input[type=text], input[type=textarea]', function() { if ($.browser.msie) { $(window).resize(); // fix IE placeholder position } }); });