Path: blob/main/website/GAUSS/inputs-ext/address/address.js
2945 views
/**1Address editable input.2Internally value stored as {city: "Moscow", street: "Lenina", building: "15"}34@class address5@extends abstractinput6@final7@example8<a href="#" id="address" data-type="address" data-pk="1">awesome</a>9<script>10$(function(){11$('#address').editable({12url: '/post',13title: 'Enter city, street and building #',14value: {15city: "Moscow",16street: "Lenina",17building: "15"18}19});20});21</script>22**/23(function ($) {24"use strict";2526var Address = function (options) {27this.init('address', options, Address.defaults);28};2930//inherit from Abstract input31$.fn.editableutils.inherit(Address, $.fn.editabletypes.abstractinput);3233$.extend(Address.prototype, {34/**35Renders input from tpl3637@method render()38**/39render: function() {40this.$input = this.$tpl.find('input');41},4243/**44Default method to show value in element. Can be overwritten by display option.4546@method value2html(value, element)47**/48value2html: function(value, element) {49if(!value) {50$(element).empty();51return;52}53var html = $('<div>').text(value.city).html() + ', ' + $('<div>').text(value.street).html() + ' st., bld. ' + $('<div>').text(value.building).html();54$(element).html(html);55},5657/**58Gets value from element's html5960@method html2value(html)61**/62html2value: function(html) {63/*64you may write parsing method to get value by element's html65e.g. "Moscow, st. Lenina, bld. 15" => {city: "Moscow", street: "Lenina", building: "15"}66but for complex structures it's not recommended.67Better set value directly via javascript, e.g.68editable({69value: {70city: "Moscow",71street: "Lenina",72building: "15"73}74});75*/76return null;77},7879/**80Converts value to string.81It is used in internal comparing (not for sending to server).8283@method value2str(value)84**/85value2str: function(value) {86var str = '';87if(value) {88for(var k in value) {89str = str + k + ':' + value[k] + ';';90}91}92return str;93},9495/*96Converts string to value. Used for reading value from 'data-value' attribute.9798@method str2value(str)99*/100str2value: function(str) {101/*102this is mainly for parsing value defined in data-value attribute.103If you will always set value by javascript, no need to overwrite it104*/105return str;106},107108/**109Sets value of input.110111@method value2input(value)112@param {mixed} value113**/114value2input: function(value) {115if(!value) {116return;117}118this.$input.filter('[name="city"]').val(value.city);119this.$input.filter('[name="street"]').val(value.street);120this.$input.filter('[name="building"]').val(value.building);121},122123/**124Returns value of input.125126@method input2value()127**/128input2value: function() {129return {130city: this.$input.filter('[name="city"]').val(),131street: this.$input.filter('[name="street"]').val(),132building: this.$input.filter('[name="building"]').val()133};134},135136/**137Activates input: sets focus on the first field.138139@method activate()140**/141activate: function() {142this.$input.filter('[name="city"]').focus();143},144145/**146Attaches handler to submit form in case of 'showbuttons=false' mode147148@method autosubmit()149**/150autosubmit: function() {151this.$input.keydown(function (e) {152if (e.which === 13) {153$(this).closest('form').submit();154}155});156}157});158159Address.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {160tpl: '<div class="editable-address"><label><span>City: </span><input type="text" name="city" class="input-small"></label></div>'+161'<div class="editable-address"><label><span>Street: </span><input type="text" name="street" class="input-small"></label></div>'+162'<div class="editable-address"><label><span>Building: </span><input type="text" name="building" class="input-mini"></label></div>',163164inputclass: ''165});166167$.fn.editabletypes.address = Address;168169}(window.jQuery));170171