form.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. 'sr': {
  240. 'Attachments': 'Прилози',
  241. 'Email': 'Имејл',
  242. 'Message': 'Порука',
  243. 'Name': 'Назив',
  244. 'Your Email': 'Ваш имејл',
  245. 'Your Message…': 'Ваша порука…',
  246. 'Your Name': 'Ваше име',
  247. },
  248. 'sr-latn-rs': {
  249. 'Attachments': 'Prilozi',
  250. 'Email': 'Imejl',
  251. 'Message': 'Poruka',
  252. 'Name': 'Naziv',
  253. 'Your Email': 'Vaš imejl',
  254. 'Your Message…': 'Vaša poruka…',
  255. 'Your Name': 'Vaše ime',
  256. },
  257. 'sv': {
  258. 'Attachments': 'Bilagor',
  259. 'Email': 'E-post',
  260. 'Message': 'Meddelande',
  261. 'Name': 'Namn',
  262. 'Your Email': 'Din mejl',
  263. 'Your Message…': 'Ditt meddelande…',
  264. 'Your Name': 'Ditt namn',
  265. },
  266. 'zh-cn': {
  267. 'Attachments': '附件',
  268. 'Email': '邮件地址',
  269. 'Message': '消息',
  270. 'Name': '名称',
  271. 'Your Email': '您的邮件地址',
  272. 'Your Message…': '',
  273. 'Your Name': '您的尊姓大名',
  274. },
  275. 'zh-tw': {
  276. 'Attachments': '附件',
  277. 'Email': '電子郵件',
  278. 'Message': '訊息',
  279. 'Name': '名稱',
  280. 'Your Email': '請留下您的電子郵件地址',
  281. 'Your Message…': '',
  282. 'Your Name': '您的尊姓大名',
  283. },
  284. // ZAMMAD_TRANSLATIONS_END
  285. }
  286. };
  287. function Plugin(element, options) {
  288. this.element = element
  289. this.$element = $(element)
  290. this._defaults = defaults;
  291. this._name = pluginName;
  292. this._endpoint_config = '/api/v1/form_config'
  293. this._endpoint_submit = '/api/v1/form_submit'
  294. this._script_location = '/assets/form/form.js'
  295. this._css_location = '/assets/form/form.css'
  296. this._src = document.getElementById('zammad_form_script').src
  297. this.css_location = this._src.replace(this._script_location, this._css_location)
  298. this.endpoint_config = this._src.replace(this._script_location, this._endpoint_config)
  299. this.endpoint_submit = this._src.replace(this._script_location, this._endpoint_submit)
  300. this.options = $.extend(true, {}, defaults, options)
  301. if (!this.options.lang) {
  302. this.options.lang = $('html').attr('lang')
  303. }
  304. if (this.options.lang) {
  305. this.options.lang = this.options.lang.replace(/-.+?$/, '')
  306. this.log('debug', "lang: " + this.options.lang)
  307. }
  308. this._config = {}
  309. this._token = ''
  310. this.init()
  311. }
  312. Plugin.prototype.init = function () {
  313. var _this = this,
  314. params = {}
  315. _this.log('debug', 'init', this._src)
  316. if (!_this.options.noCSS) {
  317. _this.loadCss(_this.css_location)
  318. }
  319. if (_this.options.attachmentSupport === true || _this.options.attachmentSupport === 'true') {
  320. var attachment = {
  321. display: 'Attachments',
  322. name: 'file[]',
  323. tag: 'input',
  324. type: 'file',
  325. repeat: 1,
  326. }
  327. _this.options.attributes.push(attachment)
  328. }
  329. if (_this.options.agreementMessage) {
  330. var agreement = {
  331. display: _this.options.agreementMessage,
  332. name: 'agreement',
  333. tag: 'input',
  334. type: 'checkbox',
  335. id: 'zammad-form-agreement',
  336. required: true,
  337. defaultValue: '',
  338. }
  339. _this.options.attributes.push(agreement)
  340. }
  341. _this.log('debug', 'endpoint_config: ' + _this.endpoint_config)
  342. _this.log('debug', 'endpoint_submit: ' + _this.endpoint_submit)
  343. // load config
  344. if (this.options.test) {
  345. params.test = true
  346. }
  347. params.fingerprint = this.fingerprint()
  348. $.ajax({
  349. method: 'post',
  350. url: _this.endpoint_config,
  351. cache: false,
  352. processData: true,
  353. data: params
  354. }).done(function(data) {
  355. _this.log('debug', 'config:', data)
  356. _this._config = data
  357. }).fail(function(jqXHR, textStatus, errorThrown) {
  358. if (jqXHR.status == 401) {
  359. _this.log('error', 'Faild to load form config, wrong authentication data!')
  360. }
  361. else if (jqXHR.status == 403) {
  362. _this.log('error', 'Faild to load form config, feature is disabled or request is wrong!')
  363. }
  364. else {
  365. _this.log('error', 'Faild to load form config!')
  366. }
  367. _this.noConfig()
  368. });
  369. // show form
  370. if (!this.options.modal) {
  371. _this.render()
  372. }
  373. // bind form on call
  374. else {
  375. this.$element.off('click.zammad-form').on('click.zammad-form', function (e) {
  376. e.preventDefault()
  377. _this.render()
  378. return true
  379. })
  380. }
  381. }
  382. // load css
  383. Plugin.prototype.loadCss = function(filename) {
  384. if (document.createStyleSheet) {
  385. document.createStyleSheet(filename)
  386. }
  387. else {
  388. $('<link rel="stylesheet" type="text/css" href="' + filename + '" />').appendTo('head')
  389. }
  390. }
  391. // send
  392. Plugin.prototype.submit = function() {
  393. var _this = this
  394. // check min modal open time
  395. if (_this.modalOpenTime) {
  396. var currentTime = new Date().getTime()
  397. var diff = currentTime - _this.modalOpenTime.getTime()
  398. _this.log('debug', 'currentTime', currentTime)
  399. _this.log('debug', 'modalOpenTime', _this.modalOpenTime.getTime())
  400. _this.log('debug', 'diffTime', diff)
  401. if (diff < 1000*10) {
  402. alert('Sorry, you look like a robot!')
  403. return
  404. }
  405. }
  406. // disable form
  407. _this.$form.find('button').prop('disabled', true)
  408. $.ajax({
  409. method: 'post',
  410. url: _this.endpoint_submit,
  411. data: _this.getParams(),
  412. cache: false,
  413. contentType: false,
  414. processData: false,
  415. }).done(function(data) {
  416. // Remove the errors from the form.
  417. _this.$form.find('.zammad-form-group--has-error').removeClass('zammad-form-group--has-error')
  418. // Deprecated code, can be removed in future versions:
  419. _this.$form.find('.has-error').removeClass('has-error')
  420. // set errors
  421. if (data.errors) {
  422. $.each(data.errors, function( key, value ) {
  423. _this.$form.find('[name=' + key + ']').closest('.'+ _this.options.prefixCSS +'group').addClass('zammad-form-group--has-error')
  424. // Deprecated code, can be removed in future versions:
  425. _this.$form.find('[name=' + key + ']').closest('.form-group').addClass('has-error')
  426. })
  427. if (data.errors.token) {
  428. alert(data.errors.token)
  429. }
  430. _this.$form.find('button').prop('disabled', false)
  431. return
  432. }
  433. // ticket has been created
  434. _this.thanks(data)
  435. }).fail(function() {
  436. _this.$form.find('button').prop('disabled', false)
  437. alert('The form could not be submitted!')
  438. });
  439. }
  440. // get params
  441. Plugin.prototype.getParams = function() {
  442. var _this = this
  443. var formData = new FormData(_this.$form[0])
  444. /* unfortunaly not working in safari and some IEs - https://developer.mozilla.org/en-US/docs/Web/API/FormData
  445. if (!formData.has('title')) {
  446. formData.append('title', this.options.messageTitle)
  447. }
  448. */
  449. if (!_this.$form.find('[name=title]').val()) {
  450. formData.append('title', this.options.messageTitle)
  451. }
  452. if (this.options.test) {
  453. formData.append('test', true)
  454. }
  455. formData.append('token', this._config.token)
  456. formData.append('fingerprint', this.fingerprint())
  457. _this.log('debug', 'formData', formData)
  458. return formData
  459. }
  460. Plugin.prototype.closeModal = function() {
  461. if (this.$modal) {
  462. this.$modal.remove()
  463. }
  464. }
  465. // render form
  466. Plugin.prototype.render = function(e) {
  467. var _this = this
  468. _this.closeModal()
  469. _this.modalOpenTime = new Date()
  470. _this.log('debug', 'modalOpenTime:', _this.modalOpenTime)
  471. var element = "<div class=\"" + _this.options.prefixCSS + "modal\">\
  472. <div class=\"" + _this.options.prefixCSS + "modal-backdrop js-zammad-form-modal-backdrop\"></div>\
  473. <div class=\"" + _this.options.prefixCSS + "modal-body js-zammad-form-modal-body\">\
  474. <form class=\"zammad-form\"></form>\
  475. </div>\
  476. </div>"
  477. if (!this.options.modal) {
  478. element = '<div><form class="zammad-form"></form></div>'
  479. }
  480. var $element = $(element)
  481. var $form = $element.find('form')
  482. if (this.options.showTitle && this.options.messageTitle != '') {
  483. $form.append('<h2>' + this.options.messageTitle + '</h2>')
  484. }
  485. $.each(this.options.attributes, function(index, value) {
  486. var valueId = _this.options.modal ? value.id + '-modal' : value.id + '-inline'
  487. var item
  488. if (value.type == 'checkbox'){
  489. item = $('<div class="form-group '+ _this.options.prefixCSS +'group"></div>');
  490. } else {
  491. // Deprecated class "form-group" can be removed in future versions.
  492. item = $('<div class="form-group '+ _this.options.prefixCSS +'group"><label for="' + valueId +'"> ' + _this.T(value.display) + '</label></div>');
  493. }
  494. var defaultValue = (typeof value.defaultValue === 'function') ? value.defaultValue() : value.defaultValue;
  495. for (var i=0; i < (value.repeat ? value.repeat : 1); i++) {
  496. if (value.tag === 'input') {
  497. if (value.type === 'checkbox'){
  498. 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>')
  499. item.append(label)
  500. } else {
  501. // Deprecated class "form-control" can be removed in future versions.
  502. 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' : '') + '>')
  503. }
  504. }
  505. else if (value.tag == 'textarea') {
  506. // Deprecated class "form-control" can be removed in future versions.
  507. 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>')
  508. }
  509. }
  510. $form.append(item)
  511. })
  512. $form.append('<button type="submit" class="btn">' + this.options.messageSubmit + '</button')
  513. this.$modal = $element
  514. this.$form = $form
  515. // bind on close
  516. $element.find('.js-zammad-form-modal-backdrop').off('click.zammad-form').on('click.zammad-form', function (e) {
  517. e.preventDefault()
  518. _this.closeModal()
  519. return true
  520. })
  521. // bind form submit
  522. $element.off('submit.zammad-form').on('submit.zammad-form', function (e) {
  523. e.preventDefault()
  524. _this.submit()
  525. return true
  526. })
  527. // show form
  528. if (!this.options.modal) {
  529. _this.$element.html($element)
  530. }
  531. // append modal to body
  532. else {
  533. $('body').append($element)
  534. }
  535. }
  536. // thanks
  537. Plugin.prototype.thanks = function(data) {
  538. var thankYou = this.options.messageThankYou
  539. if (data.ticket && data.ticket.number) {
  540. thankYou = thankYou.replace('%s', data.ticket.number)
  541. }
  542. var message = $('<div class="js-thankyou zammad-form-thankyou">' + thankYou + '</div>')
  543. this.$form.html(message)
  544. }
  545. // unable to load config
  546. Plugin.prototype.noConfig = function(e) {
  547. var message = $('<div class="js-noConfig">' + this.options.messageNoConfig + '</div>')
  548. if (this.$form) {
  549. this.$form.html(message)
  550. }
  551. this.$element.html(message)
  552. }
  553. // log method
  554. Plugin.prototype.log = function() {
  555. var args = Array.prototype.slice.call(arguments)
  556. var level = args.shift()
  557. if (!this.options.debug && level == 'debug') {
  558. return
  559. }
  560. args.unshift(this._name + '||' + level)
  561. console.log.apply(console, args)
  562. var logString = ''
  563. $.each( args, function(index, item) {
  564. logString = logString + ' '
  565. if (typeof item == 'object') {
  566. logString = logString + JSON.stringify(item)
  567. }
  568. else if (item && item.toString) {
  569. logString = logString + item.toString()
  570. }
  571. else {
  572. logString = logString + item
  573. }
  574. })
  575. $('.js-logDisplay').prepend('<div>' + logString + '</div>')
  576. }
  577. // translation method
  578. Plugin.prototype.T = function() {
  579. var string = arguments[0]
  580. var items = 2 <= arguments.length ? slice.call(arguments, 1) : []
  581. if (this.options.lang && this.options.lang !== 'en') {
  582. if (!this.options.translations[this.options.lang]) {
  583. this.log('debug', "Translation '" + this.options.lang + "' needed!")
  584. }
  585. else {
  586. translations = this.options.translations[this.options.lang]
  587. if (!translations[string]) {
  588. this.log('debug', "Translation needed for '" + this.options.lang + "' " + string + "'")
  589. }
  590. string = translations[string] || string
  591. }
  592. }
  593. if (items) {
  594. for (i = 0, len = items.length; i < len; i++) {
  595. item = items[i]
  596. string = string.replace(/%s/, item)
  597. }
  598. }
  599. return string
  600. }
  601. Plugin.prototype.fingerprint = function () {
  602. var canvas = document.createElement('canvas')
  603. var ctx = canvas.getContext('2d')
  604. var txt = 'https://zammad.com'
  605. ctx.textBaseline = 'top'
  606. ctx.font = '12px \'Arial\''
  607. ctx.textBaseline = 'alphabetic'
  608. ctx.fillStyle = '#f60'
  609. ctx.fillRect(125,1,62,20)
  610. ctx.fillStyle = '#069'
  611. ctx.fillText(txt, 2, 15)
  612. ctx.fillStyle = 'rgba(100, 200, 0, 0.7)'
  613. ctx.fillText(txt, 4, 17)
  614. return canvas.toDataURL()
  615. }
  616. $.fn[pluginName] = function (options) {
  617. return this.each(function () {
  618. var instance = $.data(this, 'plugin_' + pluginName)
  619. if (instance) {
  620. instance.$element.empty()
  621. $.data(this, 'plugin_' + pluginName, undefined)
  622. }
  623. $.data(
  624. this, 'plugin_' + pluginName,
  625. new Plugin(this, options)
  626. );
  627. });
  628. }
  629. }(jQuery));