Import moment in web_form (#5106)

This commit is contained in:
Faris Ansari 2018-03-01 14:56:19 +05:30 committed by GitHub
parent ad9a207ec2
commit e408de3abd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 26 deletions

View file

@ -15,7 +15,6 @@
"public/js/frappe/misc/common.js",
"public/js/frappe/translate.js",
"public/js/frappe/misc/pretty_date.js",
"public/js/lib/moment/moment.min.js",
"public/js/lib/highlight.pack.js",
"public/js/lib/microtemplate.js",
"public/js/frappe/query_string.js",

View file

@ -255,7 +255,7 @@ def get_context(context):
js_path = os.path.join(os.path.dirname(self.web_form_module.__file__), scrub(self.name) + '.js')
if os.path.exists(js_path):
context.script = frappe.render_template(open(js_path, 'r').read().decode('utf-8'), context)
context.script = frappe.render_template(open(js_path, 'r').read(), context)
css_path = os.path.join(os.path.dirname(self.web_form_module.__file__), scrub(self.name) + '.css')
if os.path.exists(css_path):

View file

@ -1,3 +1,5 @@
import moment from 'moment';
frappe.ready(function() {
frappe.file_reading = false;
frappe.form_dirty = false;
@ -19,7 +21,7 @@ frappe.ready(function() {
if(input.files.length) {
var file = input.files[0];
frappe.file_reading = true;
reader.onload = function(e) {
reader.onload = function() {
input.filedata = {
"__file_attachment": 1,
"filename": file.name,
@ -30,14 +32,14 @@ frappe.ready(function() {
(frappe.max_attachment_size * 1024 * 1024)) {
frappe.msgprint(__('Max file size allowed is {0}MB',
[frappe.max_attachment_size]));
input.filedata = null
input.filedata = null;
// clear attachment
$(input).val('');
$(input).attr('data-value', '');
}
frappe.file_reading = false;
}
};
reader.readAsDataURL(file);
}
@ -48,7 +50,7 @@ frappe.ready(function() {
if($(input).attr('data-reqd')) {
$(input).parent().toggleClass('has-error', !!!$(input).val());
}
}
};
// show mandatory fields as red
$('.form-group input, .form-group textarea, .form-group select').on('change', function() {
@ -61,7 +63,9 @@ frappe.ready(function() {
if(maxlength && (($(this).val() || '') + '').length > maxlength-1) {
$(this).val($(this).val().substr(0, maxlength-1));
}
}).each(function() { set_mandatory_class(this); });
}).each(function() {
set_mandatory_class(this);
});
// if changed, set dirty flag
$form.on('change', function() {
@ -118,7 +122,7 @@ frappe.ready(function() {
.removeClass('hidden')
.find(':input:first').focus();
}
};
// add row
$('.btn-add-row').on('click', function() {
@ -170,7 +174,9 @@ frappe.ready(function() {
// set name of child record (if set)
var name = $(this).attr('data-name');
if(name) { d.name = name; }
if(name) {
d.name = name;
}
// check if child table has value
var has_value = false;
@ -195,7 +201,7 @@ frappe.ready(function() {
});
return doc;
}
};
// get data from input elements
// for the given doctype
@ -205,32 +211,32 @@ frappe.ready(function() {
var $input = $(this);
var input_type = $input.attr("data-fieldtype");
var no_attachment = false;
var val;
if(input_type==="Attach") {
// save filedata dict as value
if($input.get(0).filedata) {
var val = $input.get(0).filedata;
val = $input.get(0).filedata;
} else {
// original value
var val = $input.attr('data-value');
val = $input.attr('data-value');
if (!val) {
val = {'__no_attachment': 1}
val = {'__no_attachment': 1};
no_attachment = true;
}
}
} else if(input_type==='Text Editor') {
var val = $input.parent().find('.note-editable').html();
val = $input.parent().find('.note-editable').html();
} else if(input_type==="Check") {
var val = $input.prop("checked") ? 1 : 0;
val = $input.prop("checked") ? 1 : 0;
} else if(input_type==="Date") {
// convert from user format to YYYY-MM-DD
if($input.val()) {
var val = moment($input.val(), moment.defaultFormat).format('YYYY-MM-DD');
val = moment($input.val(), moment.defaultFormat).format('YYYY-MM-DD');
} else {
var val = null;
val = null;
}
} else {
var val = $input.val();
val = $input.val();
}
if(typeof val==='string') {
@ -246,7 +252,7 @@ frappe.ready(function() {
out[$input.attr("name")] = val;
});
return out;
}
};
function save(for_payment) {
if(window.saving)
@ -398,7 +404,7 @@ frappe.ready(function() {
timeFormat: "hh:ii:ss"
});
}
}
};
setup_date_picker($form);

View file

@ -19,6 +19,7 @@
"cookie": "^0.3.1",
"express": "^4.16.2",
"frappe-datatable": "frappe/datatable",
"moment": "^2.20.1",
"redis": "^2.8.0",
"showdown": "^1.8.6",
"socket.io": "^2.0.4",

View file

@ -101,10 +101,9 @@ function get_js_config(output_file, input_files) {
file: path.resolve(assets_path, output_file),
format: 'iife',
name: 'Rollup',
// globals: {
// 'sortablejs': 'window.Sortable',
// 'clusterize.js': 'window.Clusterize'
// },
globals: {
'jquery': 'window.jQuery'
},
sourcemap: true
},
context: 'window',

View file

@ -615,7 +615,6 @@ frappe-datatable@frappe/datatable:
resolved "https://codeload.github.com/frappe/datatable/tar.gz/99701f2477b3fb8180ccafaf2c2746886b13ba53"
dependencies:
clusterize.js "^0.18.0"
lodash "^4.17.5"
sortablejs "^1.7.0"
fresh@0.5.2:
@ -1106,6 +1105,10 @@ mkdirp@^0.5.0:
dependencies:
minimist "0.0.8"
moment@^2.20.1:
version "2.20.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.20.1.tgz#d6eb1a46cbcc14a2b2f9434112c1ff8907f313fd"
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"