@@ -1,3 +1,4 @@ | |||||
frappe.provide("frappe.ui"); | |||||
frappe.listview_settings['File'] = { | frappe.listview_settings['File'] = { | ||||
hide_name_column: true, | hide_name_column: true, | ||||
@@ -26,8 +27,10 @@ frappe.listview_settings['File'] = { | |||||
}, | }, | ||||
onload: function(doclist) { | onload: function(doclist) { | ||||
doclist.filter_area = doclist.wrapper.find(".show_filters"); | doclist.filter_area = doclist.wrapper.find(".show_filters"); | ||||
doclist.breadcrumb = $('<ol class="breadcrumb for-file-list"></ol>') | doclist.breadcrumb = $('<ol class="breadcrumb for-file-list"></ol>') | ||||
.insertBefore(doclist.filter_area); | .insertBefore(doclist.filter_area); | ||||
doclist.page.add_menu_item(__("Create Folder"), function() { | doclist.page.add_menu_item(__("Create Folder"), function() { | ||||
var d = frappe.prompt(__("Name"), function(values) { | var d = frappe.prompt(__("Name"), function(values) { | ||||
if((values.value.indexOf("/") > -1)){ | if((values.value.indexOf("/") > -1)){ | ||||
@@ -84,6 +87,18 @@ frappe.listview_settings['File'] = { | |||||
frappe.utils.set_title(doclist.current_folder_name); | frappe.utils.set_title(doclist.current_folder_name); | ||||
}, | }, | ||||
set_primary_action:function(doclist){ | |||||
doclist.page.clear_primary_action(); | |||||
doclist.page.set_primary_action(__("New File"), function() { | |||||
dialog = frappe.ui.get_upload_dialog({ | |||||
"data":{"folder": doclist.current_folder, "from_form": 1} | |||||
}) | |||||
}, "octicon octicon-plus"); | |||||
}, | |||||
post_render_item: function(list, row, data) { | post_render_item: function(list, row, data) { | ||||
if(data.is_folder) { | if(data.is_folder) { | ||||
$(row).find(".list-id").attr("href", "#List/File/" + data.name); | $(row).find(".list-id").attr("href", "#List/File/" + data.name); | ||||
@@ -145,28 +145,12 @@ frappe.ui.form.Attachments = Class.extend({ | |||||
}, | }, | ||||
new_attachment: function(fieldname) { | new_attachment: function(fieldname) { | ||||
var me = this; | var me = this; | ||||
if(!this.dialog) { | |||||
this.dialog = new frappe.ui.Dialog({ | |||||
title: __('Upload Attachment'), | |||||
}); | |||||
if(!this.dialog){ | |||||
this.dialog = frappe.ui.get_upload_dialog({"data": me.get_args(), | |||||
"callback": me.attachment_uploaded, "curr": me, | |||||
"max_width": me.frm.cscript ? me.frm.cscript.attachment_max_width : null, | |||||
"max_height": me.frm.cscript ? me.frm.cscript.attachment_max_height : null}); | |||||
} | } | ||||
this.dialog.show(); | |||||
this.fieldname = fieldname; | |||||
$(this.dialog.body).empty(); | |||||
frappe.upload.make({ | |||||
parent: this.dialog.body, | |||||
args: this.get_args(), | |||||
callback: function(attachment, r) { | |||||
me.attachment_uploaded(attachment, r); | |||||
}, | |||||
onerror: function() { | |||||
me.dialog.hide(); | |||||
}, | |||||
btn: this.dialog.set_primary_action(__("Attach")), | |||||
max_width: this.frm.cscript ? this.frm.cscript.attachment_max_width : null, | |||||
max_height: this.frm.cscript ? this.frm.cscript.attachment_max_height : null, | |||||
}); | |||||
}, | }, | ||||
get_args: function() { | get_args: function() { | ||||
return { | return { | ||||
@@ -213,3 +197,34 @@ frappe.ui.form.Attachments = Class.extend({ | |||||
this.refresh(); | this.refresh(); | ||||
} | } | ||||
}); | }); | ||||
frappe.ui.get_upload_dialog = function(opts){ | |||||
dialog = new frappe.ui.Dialog({ | |||||
title: __('Upload Attachment'), | |||||
}); | |||||
dialog.show(); | |||||
$(dialog.body).empty(); | |||||
frappe.upload.make({ | |||||
parent: dialog.body, | |||||
args: opts.data, | |||||
callback: function(attachment, r) { | |||||
if(opts.callback){ | |||||
opts.callback.call(opts.curr, attachment, r); | |||||
} | |||||
else{ | |||||
dialog.hide(); | |||||
} | |||||
}, | |||||
onerror: function() { | |||||
dialog.hide(); | |||||
}, | |||||
btn: dialog.set_primary_action(__("Attach")), | |||||
max_width: opts.max_width, | |||||
max_height: opts.max_height, | |||||
}); | |||||
return dialog; | |||||
} |
@@ -118,8 +118,12 @@ frappe.ui.Listing = Class.extend({ | |||||
set_primary_action: function() { | set_primary_action: function() { | ||||
var me = this; | var me = this; | ||||
if(this.new_doctype) { | if(this.new_doctype) { | ||||
this.page.set_primary_action(__("New"), function() { | |||||
me.make_new_doc(); }, "octicon octicon-plus"); | |||||
if(this.listview.settings.set_primary_action){ | |||||
this.listview.settings.set_primary_action(this); | |||||
} else { | |||||
this.page.set_primary_action(__("New"), function() { | |||||
me.make_new_doc(me.new_doctype); }, "octicon octicon-plus"); | |||||
} | |||||
} else { | } else { | ||||
this.page.clear_primary_action(); | this.page.clear_primary_action(); | ||||
} | } | ||||
@@ -71,6 +71,7 @@ frappe.upload = { | |||||
} | } | ||||
return; | return; | ||||
} | } | ||||
console.log(args) | |||||
var dataurl = null; | var dataurl = null; | ||||
var _upload_file = function() { | var _upload_file = function() { | ||||
@@ -21,20 +21,21 @@ def upload(): | |||||
# get record details | # get record details | ||||
dt = frappe.form_dict.doctype | dt = frappe.form_dict.doctype | ||||
dn = frappe.form_dict.docname | dn = frappe.form_dict.docname | ||||
folder = frappe.form_dict.folder | |||||
file_url = frappe.form_dict.file_url | file_url = frappe.form_dict.file_url | ||||
filename = frappe.form_dict.filename | filename = frappe.form_dict.filename | ||||
if not filename and not file_url: | if not filename and not file_url: | ||||
frappe.msgprint(_("Please select a file or url"), | frappe.msgprint(_("Please select a file or url"), | ||||
raise_exception=True) | raise_exception=True) | ||||
print folder | |||||
# save | # save | ||||
if filename: | if filename: | ||||
filedata = save_uploaded(dt, dn) | |||||
filedata = save_uploaded(dt, dn, folder) | |||||
elif file_url: | elif file_url: | ||||
filedata = save_url(file_url, dt, dn) | |||||
filedata = save_url(file_url, dt, dn, folder) | |||||
comment = {} | |||||
if dt and dn: | if dt and dn: | ||||
comment = frappe.get_doc(dt, dn).add_comment("Attachment", | comment = frappe.get_doc(dt, dn).add_comment("Attachment", | ||||
_("Added {0}").format("<a href='{file_url}' target='_blank'>{file_name}</a>".format(**filedata.as_dict()))) | _("Added {0}").format("<a href='{file_url}' target='_blank'>{file_name}</a>".format(**filedata.as_dict()))) | ||||
@@ -43,17 +44,17 @@ def upload(): | |||||
"name": filedata.name, | "name": filedata.name, | ||||
"file_name": filedata.file_name, | "file_name": filedata.file_name, | ||||
"file_url": filedata.file_url, | "file_url": filedata.file_url, | ||||
"comment": comment.as_dict() | |||||
"comment": comment.as_dict() if comment else {} | |||||
} | } | ||||
def save_uploaded(dt, dn): | |||||
def save_uploaded(dt, dn, folder): | |||||
fname, content = get_uploaded_content() | fname, content = get_uploaded_content() | ||||
if content: | if content: | ||||
return save_file(fname, content, dt, dn); | |||||
return save_file(fname, content, dt, dn, folder); | |||||
else: | else: | ||||
raise Exception | raise Exception | ||||
def save_url(file_url, dt, dn): | |||||
def save_url(file_url, dt, dn, folder): | |||||
# if not (file_url.startswith("http://") or file_url.startswith("https://")): | # if not (file_url.startswith("http://") or file_url.startswith("https://")): | ||||
# frappe.msgprint("URL must start with 'http://' or 'https://'") | # frappe.msgprint("URL must start with 'http://' or 'https://'") | ||||
# return None, None | # return None, None | ||||
@@ -62,7 +63,8 @@ def save_url(file_url, dt, dn): | |||||
"doctype": "File", | "doctype": "File", | ||||
"file_url": file_url, | "file_url": file_url, | ||||
"attached_to_doctype": dt, | "attached_to_doctype": dt, | ||||
"attached_to_name": dn | |||||
"attached_to_name": dn, | |||||
"folder": folder | |||||
}) | }) | ||||
f.flags.ignore_permissions = True | f.flags.ignore_permissions = True | ||||
try: | try: | ||||
@@ -128,7 +130,8 @@ def get_random_filename(extn=None, content_type=None): | |||||
return random_string(7) + (extn or "") | return random_string(7) + (extn or "") | ||||
def save_file(fname, content, dt, dn, decode=False): | |||||
def save_file(fname, content, dt, dn, folder=None,decode=False): | |||||
print [folder] | |||||
if decode: | if decode: | ||||
if isinstance(content, unicode): | if isinstance(content, unicode): | ||||
content = content.encode("utf-8") | content = content.encode("utf-8") | ||||
@@ -151,6 +154,7 @@ def save_file(fname, content, dt, dn, decode=False): | |||||
"doctype": "File", | "doctype": "File", | ||||
"attached_to_doctype": dt, | "attached_to_doctype": dt, | ||||
"attached_to_name": dn, | "attached_to_name": dn, | ||||
"folder": folder, | |||||
"file_size": file_size, | "file_size": file_size, | ||||
"content_hash": content_hash, | "content_hash": content_hash, | ||||
}) | }) | ||||
@@ -290,4 +294,3 @@ def get_file_name(fname, optional_suffix): | |||||
partial, extn = f[0], "." + f[1] | partial, extn = f[0], "." + f[1] | ||||
return '{partial}{suffix}{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix) | return '{partial}{suffix}{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix) | ||||
return fname | return fname | ||||