/** * Copyright 2014 Openstack Foundation * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ (function( $ ){ var form = null; var table = null; var form_validator = null; var methods = { init : function(options) { form = $(this); if(form.length>0){ table = $("table",form); form_validator = form.validate({ rules: { add_client_name : { required:true, ValidPlainText:true, maxlength: 125, rows_max_count:[5,table], validate_duplicate_field:[table,'.additional-customer-name'] } }, messages: { 'add_client_name':{ rows_max_count:"You reached the maximum allowed number of Reference Clients.", validate_duplicate_field:'Client name already defined.' } }, onfocusout: false, focusCleanup: true }); $('tbody',table).sortable( { items: '> tr:not(.add-additional-customer)', update: function( event, ui ) { renderOrders(); } } ); $('#add-new-additional-client').click(function(event){ var is_valid = form.valid(); event.preventDefault(); event.stopPropagation(); if (is_valid){ var new_customer = {}; new_customer.name = $('#add_client_name').val(); addAdditionalCustomer(new_customer); $('#add_client_name').val(''); form_validator.resetForm(); } return false; }); $(".remove-additional-customer").live('click',function(event){ var remove_btn = $(this); var tr = remove_btn.parent().parent(); tr.remove(); renderOrders(); event.preventDefault(); event.stopPropagation(); return false; }); } }, serialize: function(){ //remove validator for add controls form_validator.settings.ignore = ".add-control"; var is_valid = form.valid(); //re add rules form_validator.settings.ignore = []; if(!is_valid){ return false; } var res = []; var rows = $("tbody > tr",table); for(var i=0;i tr",table); for(var i=0;i tr",table).length; var row_template = $('' + '' + '' + '' + 'x Remove>'); var directives = { 'td.additional-customer-order':function(arg){ return rows_number;}, 'input.additional-customer-name@value':'name', 'input.additional-customer-name@id' : function(arg){ return 'additional-customer-name_'+(rows_number);}, 'input.additional-customer-name@name' : function(arg){ return 'additional-customer-name_'+(rows_number);} }; var html = row_template.render(new_customer, directives); $(".add-additional-customer",table).before(html); var name = $('#additional-customer-name_'+(rows_number)); name.rules("add",{ required: { depends:function(){ $(this).val($.trim($(this).val())); return true; } }}); name.rules("add", { required:true, ValidPlainText:true, maxlength: 125, validate_duplicate_field:[table,'.additional-customer-name'] }); } // End of closure. }( jQuery ));