from app import app
from app.models import Page
from flask import render_template
import os

def render_with_navbar(template, **kwargs):
    pages = {'calendars': Page.query.filter_by(category='calendars').order_by(Page.index.asc()),
                'about': Page.query.filter_by(category='about').order_by(Page.index.asc()),
                'academics': Page.query.filter_by(category='academics').order_by(Page.index.asc()),
                'students': Page.query.filter_by(category='students').order_by(Page.index.asc()),
                'parents': Page.query.filter_by(category='parents').order_by(Page.index.asc()),
                'admissions': Page.query.filter_by(category='admissions').order_by(Page.index.asc())}
    return render_template(template, pages=pages, **kwargs)

#custom widget for rendering a TinyMCE input
def TinyMCE(field):
    uploads = os.listdir(os.path.join('app', app.config['UPLOAD_FOLDER']))
    liststr = "["
    image_extensions = ["png", "jpg", "jpeg", "gif", "bmp"]
    for upload in uploads:
        if '.' in upload and upload.rsplit('.', 1)[1].lower() in image_extensions:
            liststr += "{title: '" + upload + "', value: '/uploads/" + upload + "'},"
    liststr = liststr[:-1] + ']'

    return """  <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
         <script>
            function customInit() {
                setTimeout(function(){
                    window.scrollTo(0, 0);
                    $('form *:input[type!=hidden]:first').focus();
                },0);
            }
            tinymce.init({ 
            oninit: customInit(),
            selector:'#editor', 
            theme: 'modern',
            height: 800,
            convert_urls: false,
            fontsize_formats: '8pt 10pt 11pt 12pt 14pt 18pt 24pt 36pt',
            setup: function(ed) {
                     ed.on('init', function(ed) {
                       ed.target.editorCommands.execCommand("fontSize", false, "11pt");
                     });
                   },
			plugins: [
            'advlist autolink link image lists charmap preview hr anchor pagebreak spellchecker',
            'wordcount visualblocks visualchars code nonbreaking',
            'table contextmenu directionality paste textcolor'
            ],
            table_default_attributes: {
            class: 'table-condensed'
            },
            content_css: '/static/css/tinymce.css',
            toolbar: 'styleselect | fontsizeselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | forecolor backcolor',
            image_list: """ + liststr + """
         });
         </script>
         <textarea id='editor'> %s </textarea>""" % field._value()



@@ -920,23 +920,71 @@
 %5D))%0A    
+uploads.remove(%22.gitignore%22)%0A    image_list = link_
 li
-st
 st
-r
  = %22%5B%22%0A 
@@ -1042,16 +1042,17 @@
  %22bmp%22%5D%0A
+%0A
     for 
@@ -1165,23 +1165,128 @@
         
+image_list += %22%7Btitle: '%22 + upload + %22', value: '/uploads/%22 + upload + %22'%7D,%22%0A        else:%0A            link_
 li
-st
 st
-r
  += %22%7Bti
@@ -1348,29 +1348,74 @@
 %7D,%22%0A
+%0A
     
-liststr = list
+image_list = image_list%5B:-1%5D + '%5D,'%0A    link_list = link_li
 st
-r
 %5B:-1
@@ -2300,31 +2300,8 @@
 chor
- pagebreak spellchecker
 ',%0A 
@@ -2401,23 +2401,8 @@
 enu 
-directionality 
 past
@@ -2782,33 +2782,332 @@
     
-image_list: %22%22%22 + liststr
+plugin_preview_height: 600,%0A            plugin_preview_width: 925,%0A            link_context_toolbar: true,%0A            link_title: false,%0A            image_advtab: true,%0A            image_title: true,%0A            image_description: false,%0A            image_list: %22%22%22 + image_list + %22%22%22%0A            link_list: %22%22%22 + link_list
  + %22

Add list of non-image uploads to TinyMCE hyperlink