form.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. (function ($) {
  2. /*
  3. *
  4. * provides feedback form for zammad
  5. *
  6. <button id="zammad-feedback-form">Feedback</button>
  7. <script id="zammad_form_script" src="http://localhost:3000/assets/form/form.js"></script>
  8. <script>
  9. $(function() {
  10. $('#zammad-feedback-form').ZammadForm({
  11. messageTitle: 'Feedback Form', // optional
  12. messageSubmit: 'Submit', // optional
  13. messageThankYou: 'Thank you for your inquiry (#%s)! We\'ll contact you as soon as possible.', // optional
  14. messageNoConfig: 'Unable to load form config from server. Maybe feature is disabled.', // optional
  15. showTitle: true,
  16. lang: 'de', // optional, <html lang="xx"> will be used per default
  17. modal: true,
  18. attachmentSupport: false,
  19. attributes: [
  20. {
  21. display: 'Name',
  22. name: 'name',
  23. tag: 'input',
  24. type: 'text',
  25. placeholder: 'Your Name',
  26. defaultValue: '',
  27. },
  28. {
  29. display: 'Email',
  30. name: 'email',
  31. tag: 'input',
  32. type: 'email',
  33. required: true,
  34. placeholder: 'Your Email',
  35. defaultValue: function () {return User.email;},
  36. },
  37. {
  38. display: 'Message',
  39. name: 'body',
  40. tag: 'textarea',
  41. required: true,
  42. placeholder: 'Your Message…',
  43. defaultValue: '',
  44. rows: 7,
  45. },
  46. {
  47. display: 'Attachments',
  48. name: 'file[]',
  49. tag: 'input',
  50. type: 'file',
  51. repeat: 3,
  52. },
  53. ]
  54. });
  55. });
  56. </script>
  57. */
  58. var pluginName = 'ZammadForm',
  59. defaults = {
  60. lang: undefined,
  61. debug: false,
  62. noCSS: false,
  63. prefixCSS: 'zammad-form-',
  64. showTitle: false,
  65. messageTitle: 'Zammad Form',
  66. messageSubmit: 'Submit',
  67. messageThankYou: 'Thank you for your inquiry! We\'ll contact you as soon as possible.',
  68. messageNoConfig: 'Unable to load form config from server. Maybe feature is disabled.',
  69. attachmentSupport: false,
  70. attributes: [
  71. {
  72. display: 'Name',
  73. name: 'name',
  74. tag: 'input',
  75. type: 'text',
  76. id: 'zammad-form-name',
  77. required: true,
  78. placeholder: 'Your Name',
  79. defaultValue: '',
  80. },
  81. {
  82. display: 'Email',
  83. name: 'email',
  84. tag: 'input',
  85. type: 'email',
  86. id: 'zammad-form-email',
  87. required: true,
  88. placeholder: 'Your Email',
  89. defaultValue: '',
  90. },
  91. {
  92. display: 'Message',
  93. name: 'body',
  94. tag: 'textarea',
  95. id: 'zammad-form-body',
  96. required: true,
  97. placeholder: 'Your Message…',
  98. defaultValue: '',
  99. rows: 7,
  100. },
  101. ],
  102. translations: {
  103. // ZAMMAD_TRANSLATIONS_START
  104. 'cs': {
  105. 'Attachments': 'Přílohy',
  106. 'Email': 'Email',
  107. 'Message': 'Zpráva',
  108. 'Name': 'Jméno',
  109. 'Your Email': 'Váš e-mail',
  110. 'Your Message…': 'Vaše zpráva…',
  111. 'Your Name': 'Vaše jméno',
  112. },
  113. 'de': {
  114. 'Attachments': 'Anhänge',
  115. 'Email': 'E-Mail',
  116. 'Message': 'Nachricht',
  117. 'Name': 'Name',
  118. 'Your Email': 'Ihre E-Mail',
  119. 'Your Message…': 'Ihre Nachricht…',
  120. 'Your Name': 'Ihr Name',
  121. },
  122. 'es': {
  123. 'Attachments': 'Adjuntos',
  124. 'Email': 'Correo electrónico',
  125. 'Message': 'Mensaje',
  126. 'Name': 'Nombre',
  127. 'Your Email': 'Tu correo electrónico',
  128. 'Your Message…': 'Su mensaje…',
  129. 'Your Name': 'tu Nombre',
  130. },
  131. 'et': {
  132. 'Attachments': 'Manused',
  133. 'Email': 'E-post',
  134. 'Message': 'Teade',
  135. 'Name': 'Nimi',
  136. 'Your Email': 'Sinu Meiliaadress',
  137. 'Your Message…': 'Sinu Teade…',
  138. 'Your Name': 'Sinu Nimi',
  139. },
  140. 'fa': {
  141. 'Attachments': 'ضمایم',
  142. 'Email': 'رایانامه',
  143. 'Message': 'پیام',
  144. 'Name': 'نام',
  145. 'Your Email': 'ایمیل شما',
  146. 'Your Message…': 'پیام شما…',
  147. 'Your Name': 'نام شما',
  148. },
  149. 'fr': {
  150. 'Attachments': 'Pièces jointes',
  151. 'Email': 'E-mail',
  152. 'Message': 'Message',
  153. 'Name': 'Nom',
  154. 'Your Email': 'Votre email',
  155. 'Your Message…': 'Votre message…',
  156. 'Your Name': 'Votre nom',
  157. },
  158. 'hr': {
  159. 'Attachments': 'Privici',
  160. 'Email': 'E-Mail',
  161. 'Message': 'Poruka',
  162. 'Name': 'Ime',
  163. 'Your Email': 'Vaš e-mail',
  164. 'Your Message…': 'Vaša poruka…',
  165. 'Your Name': 'Vaše ime',
  166. },
  167. 'hu': {
  168. 'Attachments': 'Csatolmányok',
  169. 'Email': 'E-mail',
  170. 'Message': 'Üzenet',
  171. 'Name': 'Név',
  172. 'Your Email': 'Az Ön e-mail címe',
  173. 'Your Message…': 'Az Ön üzenete…',
  174. 'Your Name': 'Az Ön neve',
  175. },
  176. 'it': {
  177. 'Attachments': 'Allegati',
  178. 'Email': 'Email',
  179. 'Message': 'Messaggio',
  180. 'Name': 'Nome',
  181. 'Your Email': 'Il tuo indirizzo e-mail',
  182. 'Your Message…': 'Il tuo messaggio…',
  183. 'Your Name': 'Il tuo nome',
  184. },
  185. 'lt': {
  186. 'Attachments': 'Prisegtukai',
  187. 'Email': 'El. paštas',
  188. 'Message': 'Žinutė',
  189. 'Name': 'Vardas',
  190. 'Your Email': 'Jūsų el. paštas',
  191. 'Your Message…': 'Jūsų žinutė…',
  192. 'Your Name': 'Jūsų vardas',
  193. },
  194. 'nl': {
  195. 'Attachments': 'Bijlagen',
  196. 'Email': 'E-mail',
  197. 'Message': 'Bericht',
  198. 'Name': 'Naam',
  199. 'Your Email': 'Je e-mailadres',
  200. 'Your Message…': 'Je bericht…',
  201. 'Your Name': 'Je naam',
  202. },
  203. 'pl': {
  204. 'Attachments': 'Załączniki',
  205. 'Email': 'E-mail',
  206. 'Message': 'Wiadomość',
  207. 'Name': 'Nazwa',
  208. 'Your Email': 'Adres e-mail',
  209. 'Your Message…': 'Twoja wiadomość…',
  210. 'Your Name': 'Imię i nazwisko',
  211. },
  212. 'pt-br': {
  213. 'Attachments': 'Anexos',
  214. 'Email': 'Email',
  215. 'Message': 'Mensagem',
  216. 'Name': 'Nome',
  217. 'Your Email': 'Seu Email',
  218. 'Your Message…': 'Sua Mensagem…',
  219. 'Your Name': 'Seu nome',
  220. },
  221. 'ro': {
  222. 'Attachments': 'Atașamente',
  223. 'Email': 'E-mail',
  224. 'Message': 'Mesaj',
  225. 'Name': 'Nume',
  226. 'Your Email': 'Adresă de e-mail',
  227. 'Your Message…': 'Mesajul tău…',
  228. 'Your Name': 'Numele tău',
  229. },
  230. 'ru': {
  231. 'Attachments': 'Вложения',
  232. 'Email': 'Электронная почта',
  233. 'Message': 'Сообщение',
  234. 'Name': 'Имя',
  235. 'Your Email': 'Ваша почта',
  236. 'Your Message…': 'Ваше сообщение…',
  237. 'Your Name': 'Ваше имя',
  238. },
  239. 'sk': {
  240. 'Attachments': 'Prílohy',
  241. 'Email': 'E-mail',
  242. 'Message': 'Správa',
  243. 'Name': 'Meno',
  244. 'Your Email': 'Váš e-mail',
  245. 'Your Message…': 'Vaša správa…',
  246. 'Your Name': 'Vaše meno',
  247. },
  248. 'sr': {
  249. 'Attachments': 'Прилози',
  250. 'Email': 'Имејл',
  251. 'Message': 'Порука',
  252. 'Name': 'Назив',
  253. 'Your Email': 'Ваш имејл',
  254. 'Your Message…': 'Ваша порука…',
  255. 'Your Name': 'Ваше име',
  256. },
  257. 'sr-latn-rs': {
  258. 'Attachments': 'Prilozi',
  259. 'Email': 'Imejl',
  260. 'Message': 'Poruka',
  261. 'Name': 'Naziv',
  262. 'Your Email': 'Vaš imejl',
  263. 'Your Message…': 'Vaša poruka…',
  264. 'Your Name': 'Vaše ime',
  265. },
  266. 'sv': {
  267. 'Attachments': 'Bilagor',
  268. 'Email': 'E-post',
  269. 'Message': 'Meddelande',
  270. 'Name': 'Namn',
  271. 'Your Email': 'Din mejl',
  272. 'Your Message…': 'Ditt meddelande…',
  273. 'Your Name': 'Ditt namn',
  274. },
  275. 'zh-cn': {
  276. 'Attachments': '附件',
  277. 'Email': '邮件地址',
  278. 'Message': '消息',
  279. 'Name': '名称',
  280. 'Your Email': '您的邮件地址',
  281. 'Your Message…': '',
  282. 'Your Name': '您的尊姓大名',
  283. },
  284. 'zh-tw': {
  285. 'Attachments': '附件',
  286. 'Email': '電子郵件',
  287. 'Message': '訊息',
  288. 'Name': '名稱',
  289. 'Your Email': '請留下您的電子郵件地址',
  290. 'Your Message…': '',
  291. 'Your Name': '您的尊姓大名',
  292. },
  293. // ZAMMAD_TRANSLATIONS_END
  294. }
  295. };
  296. function Plugin(element, options) {
  297. this.element = element
  298. this.$element = $(element)
  299. this._defaults = defaults;
  300. this._name = pluginName;
  301. this._endpoint_config = '/api/v1/form_config'
  302. this._endpoint_submit = '/api/v1/form_submit'
  303. this._script_location = '/assets/form/form.js'
  304. this._css_location = '/assets/form/form.css'
  305. this._src = document.getElementById('zammad_form_script').src
  306. this.css_location = this._src.replace(this._script_location, this._css_location)
  307. this.endpoint_config = this._src.replace(this._script_location, this._endpoint_config)
  308. this.endpoint_submit = this._src.replace(this._script_location, this._endpoint_submit)
  309. this.options = $.extend(true, {}, defaults, options)
  310. if (!this.options.lang) {
  311. this.options.lang = $('html').attr('lang')
  312. }
  313. if (this.options.lang) {
  314. this.options.lang = this.options.lang.replace(/-.+?$/, '')
  315. this.log('debug', "lang: " + this.options.lang)
  316. }
  317. this._config = {}
  318. this._token = ''
  319. this.init()
  320. }
  321. Plugin.prototype.init = function () {
  322. var _this = this,
  323. params = {}
  324. _this.log('debug', 'init', this._src)
  325. if (!_this.options.noCSS) {
  326. _this.loadCss(_this.css_location)
  327. }
  328. if (_this.options.attachmentSupport === true || _this.options.attachmentSupport === 'true') {
  329. var attachment = {
  330. display: 'Attachments',
  331. name: 'file[]',
  332. tag: 'input',
  333. type: 'file',
  334. repeat: 1,
  335. }
  336. _this.options.attributes.push(attachment)
  337. }
  338. if (_this.options.agreementMessage) {
  339. var agreement = {
  340. display: _this.options.agreementMessage,
  341. name: 'agreement',
  342. tag: 'input',
  343. type: 'checkbox',
  344. id: 'zammad-form-agreement',
  345. required: true,
  346. defaultValue: '',
  347. }
  348. _this.options.attributes.push(agreement)
  349. }
  350. _this.log('debug', 'endpoint_config: ' + _this.endpoint_config)
  351. _this.log('debug', 'endpoint_submit: ' + _this.endpoint_submit)
  352. // load config
  353. if (this.options.test) {
  354. params.test = true
  355. }
  356. params.fingerprint = this.fingerprint()
  357. $.ajax({
  358. method: 'post',
  359. url: _this.endpoint_config,
  360. cache: false,
  361. processData: true,
  362. data: params
  363. }).done(function(data) {
  364. _this.log('debug', 'config:', data)
  365. _this._config = data
  366. }).fail(function(jqXHR, textStatus, errorThrown) {
  367. if (jqXHR.status == 401) {
  368. _this.log('error', 'Faild to load form config, wrong authentication data!')
  369. }
  370. else if (jqXHR.status == 403) {
  371. _this.log('error', 'Faild to load form config, feature is disabled or request is wrong!')
  372. }
  373. else {
  374. _this.log('error', 'Faild to load form config!')
  375. }
  376. _this.noConfig()
  377. });
  378. // show form
  379. if (!this.options.modal) {
  380. _this.render()
  381. }
  382. // bind form on call
  383. else {
  384. this.$element.off('click.zammad-form').on('click.zammad-form', function (e) {
  385. e.preventDefault()
  386. _this.render()
  387. return true
  388. })
  389. }
  390. }
  391. // load css
  392. Plugin.prototype.loadCss = function(filename) {
  393. if (document.createStyleSheet) {
  394. document.createStyleSheet(filename)
  395. }
  396. else {
  397. $('<link rel="stylesheet" type="text/css" href="' + filename + '" />').appendTo('head')
  398. }
  399. }
  400. // send
  401. Plugin.prototype.submit = function() {
  402. var _this = this
  403. // check min modal open time
  404. if (_this.modalOpenTime) {
  405. var currentTime = new Date().getTime()
  406. var diff = currentTime - _this.modalOpenTime.getTime()
  407. _this.log('debug', 'currentTime', currentTime)
  408. _this.log('debug', 'modalOpenTime', _this.modalOpenTime.getTime())
  409. _this.log('debug', 'diffTime', diff)
  410. if (diff < 1000*10) {
  411. alert('Sorry, you look like a robot!')
  412. return
  413. }
  414. }
  415. // disable form
  416. _this.$form.find('button').prop('disabled', true)
  417. $.ajax({
  418. method: 'post',
  419. url: _this.endpoint_submit,
  420. data: _this.getParams(),
  421. cache: false,
  422. contentType: false,
  423. processData: false,
  424. }).done(function(data) {
  425. // Remove the errors from the form.
  426. _this.$form.find('.zammad-form-group--has-error').removeClass('zammad-form-group--has-error')
  427. // Deprecated code, can be removed in future versions:
  428. _this.$form.find('.has-error').removeClass('has-error')
  429. // set errors
  430. if (data.errors) {
  431. $.each(data.errors, function( key, value ) {
  432. _this.$form.find('[name=' + key + ']').closest('.'+ _this.options.prefixCSS +'group').addClass('zammad-form-group--has-error')
  433. // Deprecated code, can be removed in future versions:
  434. _this.$form.find('[name=' + key + ']').closest('.form-group').addClass('has-error')
  435. })
  436. if (data.errors.token) {
  437. alert(data.errors.token)
  438. }
  439. _this.$form.find('button').prop('disabled', false)
  440. return
  441. }
  442. // ticket has been created
  443. _this.thanks(data)
  444. }).fail(function() {
  445. _this.$form.find('button').prop('disabled', false)
  446. alert('The form could not be submitted!')
  447. });
  448. }
  449. // get params
  450. Plugin.prototype.getParams = function() {
  451. var _this = this
  452. var formData = new FormData(_this.$form[0])
  453. /* unfortunaly not working in safari and some IEs - https://developer.mozilla.org/en-US/docs/Web/API/FormData
  454. if (!formData.has('title')) {
  455. formData.append('title', this.options.messageTitle)
  456. }
  457. */
  458. if (!_this.$form.find('[name=title]').val()) {
  459. formData.append('title', this.options.messageTitle)
  460. }
  461. if (this.options.test) {
  462. formData.append('test', true)
  463. }
  464. formData.append('token', this._config.token)
  465. formData.append('fingerprint', this.fingerprint())
  466. _this.log('debug', 'formData', formData)
  467. return formData
  468. }
  469. Plugin.prototype.closeModal = function() {
  470. if (this.$modal) {
  471. this.$modal.remove()
  472. }
  473. }
  474. // render form
  475. Plugin.prototype.render = function(e) {
  476. var _this = this
  477. _this.closeModal()
  478. _this.modalOpenTime = new Date()
  479. _this.log('debug', 'modalOpenTime:', _this.modalOpenTime)
  480. var element = "<div class=\"" + _this.options.prefixCSS + "modal\">\
  481. <div class=\"" + _this.options.prefixCSS + "modal-backdrop js-zammad-form-modal-backdrop\"></div>\
  482. <div class=\"" + _this.options.prefixCSS + "modal-body js-zammad-form-modal-body\">\
  483. <form class=\"zammad-form\"></form>\
  484. </div>\
  485. </div>"
  486. if (!this.options.modal) {
  487. element = '<div><form class="zammad-form"></form></div>'
  488. }
  489. var $element = $(element)
  490. var $form = $element.find('form')
  491. if (this.options.showTitle && this.options.messageTitle != '') {
  492. $form.append('<h2>' + this.options.messageTitle + '</h2>')
  493. }
  494. $.each(this.options.attributes, function(index, value) {
  495. var valueId = _this.options.modal ? value.id + '-modal' : value.id + '-inline'
  496. var item
  497. if (value.type == 'checkbox'){
  498. item = $('<div class="form-group '+ _this.options.prefixCSS +'group"></div>');
  499. } else {
  500. // Deprecated class "form-group" can be removed in future versions.
  501. item = $('<div class="form-group '+ _this.options.prefixCSS +'group"><label for="' + valueId +'"> ' + _this.T(value.display) + '</label></div>');
  502. }
  503. var defaultValue = (typeof value.defaultValue === 'function') ? value.defaultValue() : value.defaultValue;
  504. for (var i=0; i < (value.repeat ? value.repeat : 1); i++) {
  505. if (value.tag === 'input') {
  506. if (value.type === 'checkbox'){
  507. var label = $('<label for="' + valueId + '"><input type="' + value.type + '" name="' + value.name + '" id="' + valueId + '" class="' + _this.options.prefixCSS + 'checkbox" ' + (value.required === true ? ' required' : '') + '>' + _this.T(value.display) + '</label>')
  508. item.append(label)
  509. } else {
  510. // Deprecated class "form-control" can be removed in future versions.
  511. item.append('<input class="form-control '+ _this.options.prefixCSS +'control" id="' + valueId + '" name="' + value.name + '" type="' + value.type + '" placeholder="' + _this.T(value.placeholder) + '" value="' + (defaultValue || '') + '"' + (value.required === true ? ' required' : '') + '>')
  512. }
  513. }
  514. else if (value.tag == 'textarea') {
  515. // Deprecated class "form-control" can be removed in future versions.
  516. item.append('<textarea class="form-control '+ _this.options.prefixCSS +'control" id="' + valueId + '" name="' + value.name + '" placeholder="' + _this.T(value.placeholder) + '" rows="' + value.rows + '"' + (value.required === true ? ' required' : '') + '>' + (defaultValue || '') + '</textarea>')
  517. }
  518. }
  519. $form.append(item)
  520. })
  521. $form.append('<button type="submit" class="btn">' + this.options.messageSubmit + '</button')
  522. this.$modal = $element
  523. this.$form = $form
  524. // bind on close
  525. $element.find('.js-zammad-form-modal-backdrop').off('click.zammad-form').on('click.zammad-form', function (e) {
  526. e.preventDefault()
  527. _this.closeModal()
  528. return true
  529. })
  530. // bind form submit
  531. $element.off('submit.zammad-form').on('submit.zammad-form', function (e) {
  532. e.preventDefault()
  533. _this.submit()
  534. return true
  535. })
  536. // show form
  537. if (!this.options.modal) {
  538. _this.$element.html($element)
  539. }
  540. // append modal to body
  541. else {
  542. $('body').append($element)
  543. }
  544. }
  545. // thanks
  546. Plugin.prototype.thanks = function(data) {
  547. var thankYou = this.options.messageThankYou
  548. if (data.ticket && data.ticket.number) {
  549. thankYou = thankYou.replace('%s', data.ticket.number)
  550. }
  551. var message = $('<div class="js-thankyou zammad-form-thankyou">' + thankYou + '</div>')
  552. this.$form.html(message)
  553. }
  554. // unable to load config
  555. Plugin.prototype.noConfig = function(e) {
  556. var message = $('<div class="js-noConfig">' + this.options.messageNoConfig + '</div>')
  557. if (this.$form) {
  558. this.$form.html(message)
  559. }
  560. this.$element.html(message)
  561. }
  562. // log method
  563. Plugin.prototype.log = function() {
  564. var args = Array.prototype.slice.call(arguments)
  565. var level = args.shift()
  566. if (!this.options.debug && level == 'debug') {
  567. return
  568. }
  569. args.unshift(this._name + '||' + level)
  570. console.log.apply(console, args)
  571. var logString = ''
  572. $.each( args, function(index, item) {
  573. logString = logString + ' '
  574. if (typeof item == 'object') {
  575. logString = logString + JSON.stringify(item)
  576. }
  577. else if (item && item.toString) {
  578. logString = logString + item.toString()
  579. }
  580. else {
  581. logString = logString + item
  582. }
  583. })
  584. $('.js-logDisplay').prepend('<div>' + logString + '</div>')
  585. }
  586. // translation method
  587. Plugin.prototype.T = function() {
  588. var string = arguments[0]
  589. var items = 2 <= arguments.length ? slice.call(arguments, 1) : []
  590. if (this.options.lang && this.options.lang !== 'en') {
  591. if (!this.options.translations[this.options.lang]) {
  592. this.log('debug', "Translation '" + this.options.lang + "' needed!")
  593. }
  594. else {
  595. translations = this.options.translations[this.options.lang]
  596. if (!translations[string]) {
  597. this.log('debug', "Translation needed for '" + this.options.lang + "' " + string + "'")
  598. }
  599. string = translations[string] || string
  600. }
  601. }
  602. if (items) {
  603. for (i = 0, len = items.length; i < len; i++) {
  604. item = items[i]
  605. string = string.replace(/%s/, item)
  606. }
  607. }
  608. return string
  609. }
  610. Plugin.prototype.fingerprint = function () {
  611. var canvas = document.createElement('canvas')
  612. var ctx = canvas.getContext('2d')
  613. var txt = 'https://zammad.com'
  614. ctx.textBaseline = 'top'
  615. ctx.font = '12px \'Arial\''
  616. ctx.textBaseline = 'alphabetic'
  617. ctx.fillStyle = '#f60'
  618. ctx.fillRect(125,1,62,20)
  619. ctx.fillStyle = '#069'
  620. ctx.fillText(txt, 2, 15)
  621. ctx.fillStyle = 'rgba(100, 200, 0, 0.7)'
  622. ctx.fillText(txt, 4, 17)
  623. return canvas.toDataURL()
  624. }
  625. $.fn[pluginName] = function (options) {
  626. return this.each(function () {
  627. var instance = $.data(this, 'plugin_' + pluginName)
  628. if (instance) {
  629. instance.$element.empty()
  630. $.data(this, 'plugin_' + pluginName, undefined)
  631. }
  632. $.data(
  633. this, 'plugin_' + pluginName,
  634. new Plugin(this, options)
  635. );
  636. });
  637. }
  638. }(jQuery));