Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame^] | 1 | {% block title %} {{ title }} {% endblock %} |
| 2 | |
| 3 | {% block header %}<h2>{{ title }}</h2>{% endblock %} |
| 4 | |
| 5 | {% block content %} |
| 6 | {% macro render_fields(docfields) -%} |
| 7 | {% for df in docfields -%} |
| 8 | {% if df.fieldtype == "Data" -%} |
| 9 | <fieldset> |
| 10 | <label>{{ df.label }}</label> |
| 11 | <input class="form-control" type="text" placeholder="Type {{ df.label }}" |
| 12 | data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}" |
| 13 | {% if doc and doc.get(df.fieldname) -%} value="{{ doc[df.fieldname] }}" {%- endif %}> |
| 14 | </fieldset> |
| 15 | {% elif df.fieldtype == "Check" -%} |
| 16 | <fieldset class="checkbox"> |
| 17 | <label><input type="checkbox" data-fieldname="{{ df.fieldname }}" |
| 18 | data-fieldtype="{{ df.fieldtype }}" |
| 19 | {% if doc and frappe.utils.cint(doc.get(df.fieldname)) -%} checked="checked" {%- endif %}> |
| 20 | {{ df.label }}</label> |
| 21 | </fieldset> |
| 22 | {% elif df.fieldtype in ("Select", "Link") -%} |
| 23 | <fieldset> |
| 24 | {% set select_options = frappe.get_list(df.options)|map(attribute="name") |
| 25 | if df.fieldtype == "Link" else df.options.split("\n") %} |
| 26 | <label>{{ df.label }}</label> |
| 27 | <select class="form-control" data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}"> |
| 28 | {% for value in select_options -%} |
| 29 | {% if doc and doc.get(df.fieldname) == value -%} |
| 30 | <option selected="selected">{{ value }}</option> |
| 31 | {% else -%} |
| 32 | <option>{{ value }}</option> |
| 33 | {%- endif %} |
| 34 | {%- endfor %} |
| 35 | </select> |
| 36 | </fieldset> |
| 37 | {%- endif %} |
| 38 | {%- endfor %} |
| 39 | {%- endmacro %} |
| 40 | |
| 41 | <div class="container content"> |
| 42 | <ul class="breadcrumb"> |
| 43 | <li><a href="index">Home</a></li> |
| 44 | <li><a href="addresses">My Addresses</a></li> |
| 45 | <li class="active"><i class="icon-map-marker icon-fixed-width"></i> {{ title }}</li> |
| 46 | </ul> |
| 47 | <h3><i class="icon-map-marker icon-fixed-width"></i> {{ title }}</h3> |
| 48 | <button type="button" class="btn btn-primary pull-right" id="address-save"><i class="icon-ok"></i> |
| 49 | {{ doc and "Save" or "Insert" }}</button> |
| 50 | <div class="clearfix"></div> |
| 51 | <hr> |
| 52 | <div id="address-error" class="alert alert-danger" style="display:none"></div> |
| 53 | <form autocomplete="on"> |
| 54 | <div class="row"> |
| 55 | <section class="col-md-6"> |
| 56 | {{ render_fields(meta.left_fields) }} |
| 57 | </section> |
| 58 | <section class="col-md-6"> |
| 59 | {{ render_fields(meta.right_fields) }} |
| 60 | </section> |
| 61 | </section> |
| 62 | </form> |
| 63 | </div> |
| 64 | |
| 65 | <script> |
| 66 | ;(function() { |
| 67 | console.log("yoyo"); |
| 68 | frappe.ready(function() { |
| 69 | bind_save(); |
| 70 | }); |
| 71 | |
| 72 | var bind_save = function() { |
| 73 | $("#address-save").on("click", function() { |
| 74 | console.log("clicked!"); |
| 75 | |
| 76 | var fields = { |
| 77 | name: "{{ docname or '' }}" |
| 78 | }; |
| 79 | |
| 80 | $("form").find("[data-fieldname]").each(function(i, input) { |
| 81 | var $input = $(input); |
| 82 | var fieldname = $(input).attr("data-fieldname"); |
| 83 | var fieldtype = $(input).attr("data-fieldtype"); |
| 84 | |
| 85 | if(fieldtype == "Check") { |
| 86 | fields[fieldname] = $input.is(":checked") ? 1 : 0; |
| 87 | } else { |
| 88 | fields[fieldname] = $input.val(); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | frappe.call({ |
| 93 | btn: $(this), |
| 94 | type: "POST", |
| 95 | method: "shopping_cart.templates.pages.address.save_address", |
| 96 | args: { fields: fields, address_fieldname: get_url_arg("address_fieldname") }, |
| 97 | error_msg: "#address-error", |
| 98 | callback: function(r) { |
| 99 | if(get_url_arg("address_fieldname")) { |
| 100 | window.location.href = "cart"; |
| 101 | } else { |
| 102 | window.location.href = "address?name=" + encodeURIComponent(r.message); |
| 103 | } |
| 104 | } |
| 105 | }); |
| 106 | }); |
| 107 | }; |
| 108 | })(); |
| 109 | </script> |
| 110 | |
| 111 | <!-- no-sidebar --> |
| 112 | {% endblock %} |