Advertisement
twinfacer

Untitled

Sep 22nd, 2017
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('lecta.admin.factories').factory 'PrintCodesService', [
  2.   '$filter'
  3.   ($filter) ->
  4.     formatCertificate = (certificate) ->
  5.       """
  6.        СЕРТИФИКАТ
  7.  
  8.        Код активации: #{certificate.code}
  9.        Активировать до: #{moment(certificate.expires_at).format('DD.MM.YYYY')} г. (включительно)
  10.  
  11.        Чтобы воспользоваться сертификатом:
  12.        1. Зарегистрируйтесь на сайте www.lecta.ru или в приложении LECTA.
  13.        2. Нажмите «Активировать код» и введите код активации.
  14.        -----------------------------------------------------
  15.      """
  16.     formatCertificateAsHtml = (certificate) ->
  17.       """
  18.        <div>
  19.          <h3>Сертификат</h3>
  20.          <p>Код активации: <strong>#{certificate.code}</strong></p>
  21.          <p>Активировать до: <b>#{moment(certificate.expires_at).format('DD.MM.YYYY')}</b> г. (включительно)</p>
  22.          <p class="bot">Чтобы воспользоваться сертификатом:</p>
  23.          <p>1. Зарегистрируйтесь на сайте www.lecta.ru или в приложении LECTA.</p>
  24.          <p>2. Нажмите «Активировать код» и введите код активации.</p>
  25.        </div>
  26.      """
  27.     headTemplate = ->
  28.       """
  29.        <style type="text/css">
  30.          body { font-family: Arial; font-size: 8pt; }
  31.          div { border: 1px dashed #b4b3b4; display: inline-block; padding: 14pt; box-sizing: border-box;}
  32.          p { line-height: 0.6; }
  33.          p.bot { border-top: 1px solid #b4b3b4; margin-top: 15pt; padding-top: 15pt; }
  34.          h3 { font-size: 15pt; font-weight: 400; margin: 0; }
  35.          strong { font-size: 12pt; }
  36.        </style>
  37.        <title>Сертификаты LECTA</title>
  38.      """
  39.     pageTemplate = (body) ->
  40.       """
  41.        <!DOCTYPE html>
  42.        <html>
  43.          <head>
  44.            <meta charset="UTF-8">
  45.            <title>LECTA</title>
  46.          </head>
  47.          <body>#{body}</body>
  48.        </html>
  49.      """
  50.     certificatesAsHtml = (certificates) ->
  51.       certificates.map((certificate) -> formatCertificateAsHtml(certificate)).join('\n')
  52.     {
  53.       view: (certificates) ->
  54.         win = open('LECTA', '_blank')
  55.         win.document.write pageTemplate(certificatesAsHtml(certificates))
  56.         win.focus()
  57.       print: (certificates, no_print) ->
  58.         $iframe = $('.printpage')
  59.         if $iframe.length == 0
  60.           $('<iframe>').hide().addClass('printpage').appendTo 'body'
  61.           $iframe = $('.printpage')
  62.           $iframe.ready -> $iframe.contents().find('head').append headTemplate()
  63.         else
  64.           $iframe.contents().find('body').empty()
  65.         $iframe.ready ->
  66.           $iframe.contents().find('body').append certificatesAsHtml(certificates)
  67.           $iframe.get(0).contentWindow.print()
  68.       download: (certificates) ->
  69.         text = certificates.map((certificate) -> formatCertificate(certificate)).join('')
  70.         day = moment(certificates[0].expires_at).format('DD')
  71.         month = moment(certificates[0].expires_at).format('MM')
  72.         download(text, "certificates_#{certificates.length}_#{day}#{month}.txt", 'text/plain')
  73.     }
  74. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement