From 2a3684a02c9c8a1f2c6f63016b4e0915d66d806c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 13 Jun 2014 10:41:48 +0530 Subject: [PATCH] build: ability to add html files as frappe.templates[filename] and related fixes to query report --- frappe/build.py | 43 +++++++++++-------- frappe/public/build.json | 4 +- frappe/public/html/print_template.html | 6 +-- frappe/public/js/frappe/misc/utils.js | 9 ---- frappe/public/js/frappe/provide.js | 7 +-- frappe/public/js/frappe/views/query_report.js | 32 ++++++++------ frappe/public/js/lib/microtemplate.js | 19 ++++---- frappe/templates/includes/login.js | 1 + frappe/templates/pages/login.html | 4 +- 9 files changed, 66 insertions(+), 59 deletions(-) diff --git a/frappe/build.py b/frappe/build.py index 3f49530dfa..fb057934b0 100644 --- a/frappe/build.py +++ b/frappe/build.py @@ -1,5 +1,5 @@ # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors -# MIT License. See license.txt +# MIT License. See license.txt from __future__ import unicode_literals from frappe.utils.minify import JavascriptMinify @@ -16,7 +16,7 @@ def bundle(no_compress, make_copy=False): # build js files make_asset_dirs(make_copy=make_copy) build(no_compress) - + def watch(no_compress): """watch and rebuild if necessary""" import time @@ -25,18 +25,18 @@ def watch(no_compress): while True: if files_dirty(): build(no_compress=True) - + time.sleep(3) def make_asset_dirs(make_copy=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ - os.path.join(assets_path, 'js'), + os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css')]: - + if not os.path.exists(dir_path): os.makedirs(dir_path) - + # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) @@ -53,7 +53,7 @@ def build(no_compress=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for target, sources in get_build_maps().iteritems(): - pack(os.path.join(assets_path, target), sources, no_compress) + pack(os.path.join(assets_path, target), sources, no_compress) shutil.copy(os.path.join(os.path.dirname(os.path.abspath(frappe.__file__)), 'data', 'languages.txt'), frappe.local.sites_path) # reset_app_html() @@ -79,39 +79,46 @@ def get_build_maps(): else: s = os.path.join(app_path, source) source_paths.append(s) - + build_maps[target] = source_paths except Exception, e: print path raise - + return build_maps timestamps = {} def pack(target, sources, no_compress): from cStringIO import StringIO - + outtype, outtxt = target.split(".")[-1], '' jsm = JavascriptMinify() - + for f in sources: suffix = None if ':' in f: f, suffix = f.split(':') if not os.path.exists(f) or os.path.isdir(f): continue timestamps[f] = os.path.getmtime(f) try: - with open(f, 'r') as sourcefile: + with open(f, 'r') as sourcefile: data = unicode(sourcefile.read(), 'utf-8', errors='ignore') - - if outtype=="js" and (not no_compress) and suffix!="concat" and (".min." not in f): + + extn = f.rsplit(".", 1)[1] + + if outtype=="js" and extn=="js" and (not no_compress) and suffix!="concat" and (".min." not in f): tmpin, tmpout = StringIO(data.encode('utf-8')), StringIO() jsm.minify(tmpin, tmpout) outtxt += unicode(tmpout.getvalue() or '', 'utf-8').strip('\n') + ';' + elif outtype=="js" and extn=="html": + # add to frappe.templates + content = data.replace("\n", " ").replace("'", "\'") + outtxt += """frappe.templates["{key}"] = '{content}';\n""".format(\ + key=f.rsplit("/", 1)[1][:-5], content=content) else: outtxt += ('\n/*\n *\t%s\n */' % f) outtxt += '\n' + data + '\n' - + except Exception, e: print "--Error in:" + f + "--" print frappe.get_traceback() @@ -119,10 +126,10 @@ def pack(target, sources, no_compress): if not no_compress and outtype == 'css': pass #outtxt = cssmin(outtxt) - + with open(target, 'w') as f: f.write(outtxt.encode("utf-8")) - + print "Wrote %s - %sk" % (target, str(int(os.path.getsize(target)/1024))) def files_dirty(): @@ -135,4 +142,4 @@ def files_dirty(): return True else: return False - + diff --git a/frappe/public/build.json b/frappe/public/build.json index 7e122a7279..a033d26f14 100644 --- a/frappe/public/build.json +++ b/frappe/public/build.json @@ -50,7 +50,6 @@ "public/js/lib/center_image.js", "public/js/lib/bootstrap.min.js", "public/js/lib/nprogress.js", - "public/js/lib/microtemplate.js", "public/js/lib/beautify-html.js", "public/js/lib/moment/moment.min.js", "public/js/lib/moment/moment-timezone.min.js", @@ -67,6 +66,9 @@ "public/js/frappe/router.js", "public/js/frappe/desk.js", "public/js/frappe/defaults.js", + "public/js/lib/microtemplate.js", + + "public/html/print_template.html", "public/js/legacy/globals.js", "public/js/legacy/datatype.js", diff --git a/frappe/public/html/print_template.html b/frappe/public/html/print_template.html index ab8d6f634f..aba7ff57dc 100644 --- a/frappe/public/html/print_template.html +++ b/frappe/public/html/print_template.html @@ -6,12 +6,12 @@ - {0} - + {%= title %} +
- {1} + {%= content %}
diff --git a/frappe/public/js/frappe/misc/utils.js b/frappe/public/js/frappe/misc/utils.js index 3da001d209..d4b418a7d2 100644 --- a/frappe/public/js/frappe/misc/utils.js +++ b/frappe/public/js/frappe/misc/utils.js @@ -248,13 +248,4 @@ frappe.utils = { setTimeout(function() { callback(dataURL); }, 10 ); } }, - - with_print_template: function(fn) { - if(!frappe.print_template) { - $.get("/assets/frappe/html/print_template.html?q=4", - function(html) { frappe.print_template = html; fn(); }); - } else { - fn(); - } - } }; diff --git a/frappe/public/js/frappe/provide.js b/frappe/public/js/frappe/provide.js index a6091ec58b..6438b46839 100644 --- a/frappe/public/js/frappe/provide.js +++ b/frappe/public/js/frappe/provide.js @@ -1,8 +1,8 @@ // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors -// MIT License. See license.txt +// MIT License. See license.txt // provide a namespace -if(!window.frappe) +if(!window.frappe) window.frappe = {}; frappe.provide = function(namespace) { // docs: create a namespace // @@ -22,4 +22,5 @@ frappe.provide("locals"); frappe.provide("frappe.settings"); frappe.provide("frappe.utils"); frappe.provide("frappe.ui"); -frappe.provide("frappe.modules"); \ No newline at end of file +frappe.provide("frappe.modules"); +frappe.provide("frappe.templates"); diff --git a/frappe/public/js/frappe/views/query_report.js b/frappe/public/js/frappe/views/query_report.js index 52c8ef5012..c6f0b8feee 100644 --- a/frappe/public/js/frappe/views/query_report.js +++ b/frappe/public/js/frappe/views/query_report.js @@ -133,21 +133,25 @@ frappe.views.QueryReport = Class.extend({ msgprint(__("Run the report first")); return; } - frappe.utils.with_print_template(function() { - var data = []; - $.each(me.data, function(i, d) { - var newd = {}; data.push(newd); - $.each(d, function(k, v) { - newd[k.replace(/ /g, "_").toLowerCase()] = v; }); - }); - var content = tmpl.render(html_format, {data: data, filters:me.get_values(), report:me}); - var html = $.format(frappe.print_template, [ - __(me.report_name), content]); - var w = window.open(); - w.document.write(html); - w.document.close(); - }) + var data = []; + $.each(me.data, function(i, d) { + var newd = {}; data.push(newd); + $.each(d, function(k, v) { + newd[k.replace(/ /g, "_").toLowerCase()] = v; }); + }); + + var content = frappe.render(html_format, + {data: data, filters:me.get_values(), report:me}); + + var html = frappe.render(frappe.templates.print_template, { + title: __(me.report_name), content: content + }); + + var w = window.open(); + w.document.write(html); + w.document.close(); + }, "icon-print"); } diff --git a/frappe/public/js/lib/microtemplate.js b/frappe/public/js/lib/microtemplate.js index f72d21057d..361bd3dd8d 100644 --- a/frappe/public/js/lib/microtemplate.js +++ b/frappe/public/js/lib/microtemplate.js @@ -1,12 +1,13 @@ // Simple JavaScript Templating // Adapted from John Resig - http://ejohn.org/ - MIT Licensed -tmpl = {compiled: {}, debug:{}}; -tmpl.compile = function(str) { + +frappe.template = {compiled: {}, debug:{}}; +frappe.template.compile = function(str) { if(str.indexOf("'")!==-1) { console.log("Warning: Single quotes (') may not work in templates"); } - if(!tmpl.compiled[str]) { - fn_str = "var p=[],print=function(){try{p.push.apply(p,arguments)}catch(e){console.log([p, e]);};};" + + if(!frappe.template.compiled[str]) { + fn_str = "var p=[],print=function(){p.push.apply(p,arguments)};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + @@ -22,12 +23,12 @@ tmpl.compile = function(str) { .split("\r").join("\\'") + "');}return p.join('');"; - tmpl.debug[str] = fn_str; - tmpl.compiled[str] = new Function("obj", fn_str); + frappe.template.debug[str] = fn_str; + frappe.template.compiled[str] = new Function("obj", fn_str); } - return tmpl.compiled[str]; + return frappe.template.compiled[str]; }; -tmpl.render = function(str, data, debug) { - return tmpl.compile(str)(data); +frappe.render = function(str, data, debug) { + return frappe.template.compile(str)(data); }; diff --git a/frappe/templates/includes/login.js b/frappe/templates/includes/login.js index e79af5dfad..3b5dbec159 100644 --- a/frappe/templates/includes/login.js +++ b/frappe/templates/includes/login.js @@ -124,5 +124,6 @@ frappe.ready(function() { window.location.hash = "#login"; login.bind_events(); login.login(); + $(".form-signup, .form-forgot").removeClass("hide"); $(document).trigger('login_rendered'); }); diff --git a/frappe/templates/pages/login.html b/frappe/templates/pages/login.html index 604d57e23d..c3f923d098 100644 --- a/frappe/templates/pages/login.html +++ b/frappe/templates/pages/login.html @@ -43,7 +43,7 @@ -
+ @@ -56,7 +56,7 @@
-
+