bazarr/static/semantic/components/form.min.js

1 line
20 KiB
JavaScript
Raw Normal View History

!function(e,t,n,r){"use strict";t=void 0!==t&&t.Math==Math?t:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),e.fn.form=function(t){var i,a=e(this),o=a.selector||"",l=(new Date).getTime(),s=[],u=arguments[0],c=arguments[1],d="string"==typeof u,f=[].slice.call(arguments,1);return a.each(function(){var p,m,g,h,v,b,y,x,k,E,w,C,V,R,F,S,A=e(this),T=this,D=[],O=!1;(S={initialize:function(){S.get.settings(),d?(F===r&&S.instantiate(),S.invoke(u)):(F!==r&&F.invoke("destroy"),S.verbose("Initializing form validation",A,v),S.bindEvents(),S.set.defaults(),S.instantiate())},instantiate:function(){S.verbose("Storing instance of module",S),F=S,A.data(V,S)},destroy:function(){S.verbose("Destroying previous module",F),S.removeEvents(),A.removeData(V)},refresh:function(){S.verbose("Refreshing selector cache"),p=A.find(x.field),m=A.find(x.group),g=A.find(x.message),A.find(x.prompt),h=A.find(x.submit),A.find(x.clear),A.find(x.reset)},submit:function(){S.verbose("Submitting form",A),A.submit()},attachEvents:function(t,n){n=n||"submit",e(t).on("click"+R,function(e){S[n](),e.preventDefault()})},bindEvents:function(){S.verbose("Attaching form events"),A.on("submit"+R,S.validate.form).on("blur"+R,x.field,S.event.field.blur).on("click"+R,x.submit,S.submit).on("click"+R,x.reset,S.reset).on("click"+R,x.clear,S.clear),v.keyboardShortcuts&&A.on("keydown"+R,x.field,S.event.field.keydown),p.each(function(){var t=e(this),n=t.prop("type"),r=S.get.changeEvent(n,t);e(this).on(r+R,S.event.field.change)})},clear:function(){p.each(function(){var t=e(this),n=t.parent(),r=t.closest(m),i=r.find(x.prompt),a=t.data(y.defaultValue)||"",o=n.is(x.uiCheckbox),l=n.is(x.uiDropdown);r.hasClass(k.error)&&(S.verbose("Resetting error on field",r),r.removeClass(k.error),i.remove()),l?(S.verbose("Resetting dropdown value",n,a),n.dropdown("clear")):o?t.prop("checked",!1):(S.verbose("Resetting field value",t,a),t.val(""))})},reset:function(){p.each(function(){var t=e(this),n=t.parent(),i=t.closest(m),a=i.find(x.prompt),o=t.data(y.defaultValue),l=n.is(x.uiCheckbox),s=n.is(x.uiDropdown),u=i.hasClass(k.error);o!==r&&(u&&(S.verbose("Resetting error on field",i),i.removeClass(k.error),a.remove()),s?(S.verbose("Resetting dropdown value",n,o),n.dropdown("restore defaults")):l?(S.verbose("Resetting checkbox value",n,o),t.prop("checked",o)):(S.verbose("Resetting field value",t,o),t.val(o)))})},determine:{isValid:function(){var t=!0;return e.each(b,function(e,n){S.validate.field(n,e,!0)||(t=!1)}),t}},is:{bracketedRule:function(e){return e.type&&e.type.match(v.regExp.bracket)},shorthandFields:function(e){var t=e[Object.keys(e)[0]];return S.is.shorthandRules(t)},shorthandRules:function(t){return"string"==typeof t||e.isArray(t)},empty:function(e){return!e||0===e.length||(e.is('input[type="checkbox"]')?!e.is(":checked"):S.is.blank(e))},blank:function(t){return""===e.trim(t.val())},valid:function(t){var n=!0;return t?(S.verbose("Checking if field is valid",t),S.validate.field(b[t],t,!1)):(S.verbose("Checking if form is valid"),e.each(b,function(e,t){S.is.valid(e)||(n=!1)}),n)}},removeEvents:function(){A.off(R),p.off(R),h.off(R),p.off(R)},event:{field:{keydown:function(t){var n=e(this),r=t.which,i=n.is(x.input),a=n.is(x.checkbox),o=n.closest(x.uiDropdown).length>0,l=13;r==27&&(S.verbose("Escape key pressed blurring field"),n.blur()),t.ctrlKey||r!=l||!i||o||a||(O||(n.one("keyup"+R,S.event.field.keyup),S.submit(),S.debug("Enter pressed on input submitting form")),O=!0)},keyup:function(){O=!1},blur:function(t){var n=e(this),r=n.closest(m),i=S.get.validation(n);r.hasClass(k.error)?(S.debug("Revalidating field",n,i),i&&S.validate.field(i)):"blur"==v.on&&i&&S.validate.field(i)},change:function(t){var n=e(this),r=n.closest(m),i=S.get.validation(n);i&&("change"==v.on||r.hasClass(k.error)&&v.revalidate)&&(clearTimeout(S.timer),S.timer=setTimeout(function(){S.debug("Revalidating field",n,S.get.validation(n)),S.validate.field(i)},v.delay))}}},get:{ancillaryValue:function(e){return!(!e.type||!e.value&&!S.is.bracketedRule(e))&&(e.value!==r?e.value:e.type.match(v.regExp.bracket)[1]+"