select-field-choices.js
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* globals modulejs */
modulejs.define('SelectFieldChoices', ['jquery', 'SelectElement'], function($, SelectElement) {
'use strict';
function SelectFieldChoices(state_id, city_id, state_url) {
this.state_id = state_id;
this.input_html = $(state_id).parent().html();
this.old_value = $(state_id).val();
this.city_parent_div = $(city_id).parent().parent().parent();
this.state_url = state_url;
}
SelectFieldChoices.prototype.getCurrentStateElement = function() {
return $(this.state_id);
};
SelectFieldChoices.prototype.replaceWith = function(html) {
var parent_div = this.getCurrentStateElement().parent();
parent_div.html(html);
};
SelectFieldChoices.prototype.generateSelect = function(state_list) {
var select_element, option;
select_element = new SelectElement();
select_element.setAttr("name", "profile_data[state]");
select_element.setAttr("id", "state_field");
select_element.setAttr("class", "type-select valid");
state_list.forEach(function(state) {
option = SelectElement.generateOption(state, state);
select_element.addOption(option);
});
return select_element.getSelect();
};
SelectFieldChoices.prototype.replaceStateWithSelectElement = function() {
var klass = this;
$.get(this.state_url, function(response) {
var select_html;
if (response.length > 0) {
select_html = klass.generateSelect(response);
klass.replaceWith(select_html);
if (klass.old_value.length !== 0 && response.include(klass.old_value)) {
klass.getCurrentStateElement().val(klass.old_value);
}
}
});
};
SelectFieldChoices.prototype.replaceStateWithInputElement = function() {
this.replaceWith(this.input_html);
};
SelectFieldChoices.prototype.hideCity = function() {
this.city_parent_div.addClass("mpog_hidden_field");
};
SelectFieldChoices.prototype.showCity = function() {
this.city_parent_div.removeClass("mpog_hidden_field");
};
SelectFieldChoices.prototype.actualFieldIsInput = function() {
return this.getCurrentStateElement().attr("type") === "text";
};
return SelectFieldChoices;
});