@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
3 lines • 12.2 kB
JavaScript
/* @license */
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`kendo.core.min.js`),require(`kendo.data.min.js`),require(`kendo.button.min.js`)):typeof define==`function`&&define.amd?define([`exports`,`kendo.core.min`,`kendo.data.min`,`kendo.button.min`],t):(e=typeof globalThis<`u`?globalThis:e||self,t((e.kendo=e.kendo||{},e.kendo._globals=e.kendo._globals||{},e.kendo._globals.Smartpastebutton={}),e.kendo._globals.Core,e.kendo._globals.Data,e.kendo._globals.Button))})(this,function(e,t,n,r){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});let i={requestStart:`requestStart`,requestEnd:`requestEnd`,error:`error`,click:`click`},a={smartPasteButton:`k-smart-paste-button`};function o(e){if(kendo.isBlank(e)||kendo.isString(e)&&kendo.isEmpty(e.trim()))return e;if(Array.isArray(e)){for(let t=0;t<e.length;t++)if(kendo.isPresent(e[t])&&!(kendo.isString(e[t])&&kendo.isEmpty(e[t].trim())))return o(e[t]);return e}if(kendo.isDate(e))return e;let t=new Date(e);return isNaN(t.getTime())?e:t}function s(e){if(kendo.isBlank(e)||kendo.isString(e)&&kendo.isEmpty(e.trim()))return e;if(Array.isArray(e)){for(let t=0;t<e.length;t++)if(kendo.isPresent(e[t])&&!(kendo.isString(e[t])&&kendo.isEmpty(e[t].trim())))return s(e[t]);return e}if(kendo.isDate(e))return e;if(kendo.isString(e)){let t=e.trim().match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?Z?$/);if(t){let e=parseInt(t[4],10),n=parseInt(t[5],10),r=parseInt(t[6],10),i=new Date;return i.setHours(e,n,r,0),i}let n=e.trim().match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?(?:\s*(AM|PM))?$/i);if(n){let e=parseInt(n[1],10),t=parseInt(n[2],10),r=n[3]?parseInt(n[3],10):0,i=n[4]?n[4].toLowerCase():null;i&&(i===`am`&&e===12?e=0:i===`pm`&&e!==12&&(e+=12));let a=new Date;return a.setHours(e,t,r,0),a}}return e}function c(e){if(kendo.isBlank(e)||kendo.isString(e)&&kendo.isEmpty(e.trim()))return e;if(Array.isArray(e)){for(let t=0;t<e.length;t++)if(kendo.isPresent(e[t])&&!(kendo.isString(e[t])&&kendo.isEmpty(e[t].trim())))return c(e[t]);return e}if(kendo.isDate(e))return e;if(kendo.isString(e)){let t=e.trim().match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?Z?$/);if(t){let e=parseInt(t[1],10),n=parseInt(t[2],10)-1,r=parseInt(t[3],10),i=parseInt(t[4],10),a=parseInt(t[5],10),o=parseInt(t[6],10);return new Date(e,n,r,i,a,o,0)}}let t=new Date(e);return isNaN(t.getTime())?e:t}function l(e){return kendo.isBlank(e)||kendo.isString(e)&&kendo.isEmpty(e.trim())?[]:Array.isArray(e)?e.map(function(e){return kendo.isString(e)?e.trim():e}).filter(function(e){return kendo.isPresent(e)&&!(kendo.isString(e)&&kendo.isEmpty(e))}):kendo.isString(e)?e.split(`,`).map(function(e){return e.trim()}).filter(function(e){return e.length>0}):[e]}let u=[`true`,`yes`,`on`],d={bubbles:!0,detail:{fromSmartComponents:!0}},f=[`input`,`textarea`,`select`];function p(e){let t=window.getComputedStyle(e);return e.type===`hidden`||t.display===`none`||t.visibility===`hidden`}function m(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function h(e,t,n=!1){let r=n?t.querySelector(f.join(`,`))??t:t,i=r.getAttribute(`data-smartpaste-description`);if(i)return i;if(r.id){let t=e.querySelectorAll(`label[for='${r.id}']`);if(t.length===1){let e=t[0].textContent?.trim();if(e)return e}}return r.getAttribute(`name`)??r.id??null}function g(e,t){return!t?.customInputs||!Array.isArray(t.customInputs)?!1:t.customInputs.some(t=>e.matches(t.identifier)||e.closest(t.identifier))}function _(e){return e instanceof HTMLInputElement&&e.type===`radio`}function v(e){return e.type===`checkbox`?`boolean`:e.type===`number`?`number`:e instanceof HTMLSelectElement||_(e)?`fixed-choices`:`string`}function y(e){return Array.from(e.querySelectorAll(`option`)).filter(e=>!!e.value).map(e=>e.textContent??``)}function b(e,t){return Array.from(t.querySelectorAll(`input[type=radio]`)).filter(t=>t.name===e).map(e=>h(t,e)).filter(e=>e!==null)}function x(e){let t=f.join(`,`);return!Array.isArray(e)||e.length===0?t:`${t},${e.map(e=>e.identifier).join(`,`)}`}function S(e,t,n){let r=Array.from(e.querySelectorAll(`input[type=radio]`)).filter(e=>e.name===t).map(t=>({element:t,description:h(e,t)})),i=r.find(e=>e.description===n);if(i)return i.element;let a=r.filter(e=>e.description&&e.description.includes(n));return a.length===1?a[0].element:null}function C(e,t){e instanceof HTMLSelectElement?T(e,t.toString()):e instanceof HTMLInputElement&&(e.type===`radio`||e.type===`checkbox`)?E(e,t):(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)&&O(e,t)}function w(e){let t=e?.toString().toLowerCase();return u.includes(t)}function T(e,t){let n=k(e,t);n!==null&&e.selectedIndex!==n&&(A(e),e.selectedIndex=n,j(e))}function E(e,t){let n=w(t);e.checked!==n&&(A(e),e.checked=n,j(e))}function D(e){let t=new Date(e);return isNaN(t.getTime())?e:t.toISOString().split(`T`)[0]}function O(e,t){let n=t?.toString()??``,r=e instanceof HTMLInputElement&&e.type===`date`?D(n):n;e.value!==r&&(A(e),e.value=r,j(e))}function k(e,t){let n=Array.from(e.querySelectorAll(`option`)),r=n.find(e=>e.textContent===t);if(r)return n.indexOf(r);let i=n.filter(e=>e.textContent&&e.textContent.includes(t));return i.length===1?n.indexOf(i[0]):null}function A(e){e.dispatchEvent(new CustomEvent(`beforeinput`,d))}function j(e){e.dispatchEvent(new CustomEvent(`input`,d)),e.dispatchEvent(new CustomEvent(`change`,d))}var M=class{constructor(e){this.options=e}get formElement(){return this.options.getElement()?.closest(`form`)??null}populateFormFields(e,t){e.fieldValues&&t.forEach(t=>{let n=e.fieldValues[t.field];if(n==null)return;let r;if(typeof n==`string`)r=n;else if(typeof n==`number`||typeof n==`boolean`)r=String(n);else return;let i=r.trim();this.populateFormField(t,i)})}extractFormConfig(){if(!this.formElement)return[];let e=[],t=x(this.options.customInputs),n=Array.from(this.formElement.querySelectorAll(t));for(let t of n){let n=null,r=g(t,this.options);if(m(t)&&!r){if(p(t))continue;n=this.processNativeFormControl(t)}else r&&(n=this.processCustomInput(t,e));n&&e.push(n)}return e}populateFormField(e,t){if(_(e.element)){let n=e.element,r=S(this.formElement,n.name,t);r&&C(r,!0)}else e.type===`kendo-input`?this.options.setKendoInputValue&&this.options.setKendoInputValue(e,t):C(e.element,t)}processNativeFormControl(e){let t=_(e),n=e.name||e.id;if(!n||!t&&!h(this.formElement,e))return null;let r={field:n,description:t?null:h(this.formElement,e),element:e,type:v(e)};return e instanceof HTMLSelectElement?{...r,allowedValues:y(e)}:t?{...r,allowedValues:b(n,this.formElement)}:r}processCustomInput(e,t){let n=e.id||e.getAttribute(`name`)||``;if(!t.some(t=>t.field===n&&t.element===e)&&this.options.getSmartPasteField){let t={field:n,description:h(this.formElement,e,!0),element:e,type:`kendo-input`};return this.options.getSmartPasteField(t)||null}return null}};let N={id:`smartpastebutton`,name:`SmartPasteButton`,category:`web`,description:`The SmartPasteButton widget is an extension of the Kendo UI Button that enables users to paste unstructured text have it intelligently distributed into relevant fields.`,depends:[`core`,`button`,`data`]};(function(e,t){let n=window.kendo,r=n.ui,u=r.Button,d=u.extend({init:function(t,r){let i=this;n.isPresent(r.text)&&e(t).text(r.text),u.fn.init.call(this,t,e.extend({icon:this.options.icon,click:i._click.bind(i)},r)),i.smartPaste=new M({customInputs:[{identifier:`.k-dropdownlist`},{identifier:`.k-autocomplete`},{identifier:`.k-multiselect`},{identifier:`.k-combobox`},{identifier:`.k-datepicker`},{identifier:`.k-timepicker`},{identifier:`.k-dateinput`},{identifier:`.k-datetimepicker`},{identifier:`.k-dropdowntree`},{identifier:`.k-timepicker`},{identifier:`.k-switch`},{identifier:`.k-textarea`},{identifier:`.k-radio-list`},{identifier:`.k-checkbox-list`},{identifier:`.k-rating`},{identifier:`.k-maskedtextbox`},{identifier:`.k-numerictextbox`},{identifier:`.k-textbox`},{identifier:`.k-checkbox`}],getElement:i._getElement.bind(i),getSmartPasteField:i._extractFormFieldFromKendoInput.bind(i),setKendoInputValue:i._setKendoWidgetValue.bind(i)}),i.element.addClass(a.smartPasteButton),this._isListening=!1,i.options.service&&(n.isString(i.options.service)||i.options.service.url)&&(i.transport=new n.data.AISmartPasteTransport({service:i.options.service,success:i._serviceSuccess.bind(i),error:i._serviceError.bind(i)}))},options:{name:`SmartPasteButton`,enable:!0,cancelIcon:`x`,icon:`paste-sparkle`,fillMode:null,rounded:null,size:null,formFields:null,text:null,service:null},events:[i.requestStart,i.requestEnd,i.error],setOptions:function(e){let t=this;t.destroy(),n.deepExtend(t.options,e),t.init(t.element,t.options)},destroy:function(){let e=this;e.element.off(`.kendoSmartPasteButton`),e.element.removeClass(a.smartPasteButton),e.smartPaste=null,u.fn.destroy.call(e)},_click:function(e){let t=this,r=t.smartPaste.formElement;if(!r){t.trigger(i.error,{error:`No form found`});return}if(!t._isListening){if(!t.options.service||!n.isString(t.options.service)&&!t.options.service.url){t.trigger(i.error,{error:`Service must be defined!`});return}let e=t.options.formFields||t._extractFormFields(r);if(n.isPresent(e)&&!n.isEmpty(e)){let n=navigator.clipboard.readText();t._requestService(e,n)}else t.trigger(i.error,{error:`No form fields found or extracted`})}},_getKendoFieldIdentifier(t){let n=e(t);return n.attr(`data-role`)?n.attr(`id`)||null:e(n.find(`[data-role]`)).attr(`id`)||null},_extractFormFields:function(e){let t=this,n;try{n=t.smartPaste.extractFormConfig()}catch(e){return t.trigger(i.error,{error:`Failed to extract form configuration: `+e.message}),null}return!n||n.length===0?(t.trigger(i.error,{error:`No form fields could be extracted from the form`}),null):n},_getElement:function(){return e(this.element)[0]||null},_setKendoWidgetValue:function(t,r){let i=n.widgetInstance(e(t.element)),a=i?.options.name;if(i&&i.value&&a!=`RadioButton`){if(a==`CheckBox`&&e(t.element).closest(`.k-checkbox-list`).length>0)return;a==`TimePicker`?r=s(r):a==`DateTimePicker`?r=c(r):a==`DatePicker`||a==`DateInput`?r=o(r):(a==`MultiSelect`||a==`CheckBoxGroup`)&&(r=l(r)),i.value(r),e(i.element).trigger(`change`)}},_extractFormFieldFromKendoInput:function(t){let n=this,r=t.element;if(e(r).length){if(!t.field){let e=n._getKendoFieldIdentifier(r);if(!e){t=null;return}t.field=e}if(e(r).attr(`id`)==t.field)return t;let i=e(r).find(`[id="${t.field}"]`);t.element=e(i)[0]}return t},_requestService:function(e,t){let r=this;!n.isPresent(e)||n.isEmpty(e)||(r._isListening=!0,r._updateIcon(),t.then(function(t){e=e.map(function(e){return{field:e.field,type:e.type,description:e.description,allowedValues:e.allowedValues}});let n={formFields:e,content:t};r.transport.read(n),r.trigger(i.requestStart,n)}).catch(function(){r._isListening=!1,r._updateIcon()}))},_serviceSuccess:function(e){let t=this;if(!t.smartPaste){t.trigger(i.error,{error:`SmartPaste instance not available during service success`});return}let n=t.smartPaste.formElement;if(!n){t.trigger(i.error,{error:`Form element not found during service success`}),t._isListening=!1,t._updateIcon();return}let r=t._extractFormFields(n);if(!r){t._isListening=!1,t._updateIcon();return}try{t.smartPaste.populateFormFields(e,r)}catch(e){t.trigger(i.error,{error:`Failed to populate form fields: `+e.message}),t._isListening=!1,t._updateIcon();return}t._isListening=!1,t._updateIcon(),t.trigger(i.requestEnd,e)},_serviceError:function(e){let t=this;t._isListening=!1,t._updateIcon();let n=e&&e.responseText||`Unknown error`;t.trigger(i.error,{error:n})},_setFormFieldValue:function(e,t){this.smartPaste.populateFormFields(e,t)},_updateIcon:function(){let t=this,r=t.options,i=t.element.find(`.k-icon, .k-svg-icon`),a=t.element.find(`.k-button-text`),o=t._isListening?this.options.cancelIcon:this.options.icon;a.length&&a.text().trim().length>0&&(t._isListening?e(a).text(`Cancel`):e(a).text(r.text||`Smart Paste`)),i.length&&n.ui.icon(i,{icon:o})}});n.cssProperties.registerPrefix(`SmartPasteButton`,`k-button-`),r.plugin(d)})(window.kendo.jQuery);var P=kendo;e.__meta__=N,e.default=P});
//# sourceMappingURL=kendo.smartpastebutton.min.js.map