first import
[projectpier.git] / public / assets / javascript / modules / attachFiles.js
blob3957ee0efc3fdb6dafb2835a1b96549225e6fc40
1 App.modules.attachFiles = {
3 /**
4 * Add file lang
5 */
6 add_lang : 'Add file',
8 /**
9 * Remove file lang
11 remove_lang : 'Remove file',
13 /**
14 * Error message that is alerthed when we reach the max number of controls in a specific set
16 max_controls_reached_lang : 'Max number of files reached',
18 /**
19 * Max number of controls per set
21 max_controls_per_set : 5,
23 /**
24 * Array of registered attach file sets. Usualy there is only one on the page
26 sets: [],
28 /**
29 * Initialize control
31 * @param integer max_controls
32 * @param string add_lang_value
33 * @param string remove_lang_value
34 * @param string max_controls_lang_value
36 initialize : function(max_controls, add_lang_value, remove_lang_value, max_controls_lang_value) {
37 App.modules.attachFiles.max_controls_per_set = max_controls;
39 App.modules.attachFiles.add_lang = add_lang_value;
40 App.modules.attachFiles.remove_lang = remove_lang_value;
41 App.modules.attachFiles.max_controls_reached_lang = max_controls_lang_value;
44 /**
45 * Initialize set
47 * @param integer set_id
48 * @param string set_prefix
50 initSet : function(set_id, set_prefix) {
51 App.modules.attachFiles.sets[set_id] = {
52 id : set_id,
53 prefix : set_prefix,
54 controls : new Array(1),
55 next_conrol_id : 2,
56 total_controls : 1
58 App.modules.attachFiles.buildSet(set_id);
61 /**
62 * Build set
64 * @param integer set_id
66 buildSet : function(set_id) {
67 var add_button = document.createElement('button');
68 add_button.setAttribute('type', 'button');
69 add_button.appendChild(document.createTextNode( App.modules.attachFiles.add_lang ));
70 add_button.className = 'add_button';
72 add_button.onclick = function() {
73 App.modules.attachFiles.addControl(set_id);
76 $(App.modules.attachFiles.getSetControlId(set_id)).appendChild(add_button);
79 addControl : function(set_id) {
80 var total_controls = App.modules.attachFiles.sets[set_id]['total_controls'];
81 if((App.modules.attachFiles.max_controls_per_set > 0) && (total_controls >= App.modules.attachFiles.max_controls_per_set)) {
82 alert(App.modules.attachFiles.max_controls_reached_lang);
83 return;
84 } // if
86 var control_id = App.modules.attachFiles.sets[set_id]['next_conrol_id'];
87 App.modules.attachFiles.sets[set_id]['next_conrol_id'] = control_id + 1;
89 // Div
90 var control_div = document.createElement('div');
91 control_div.id = 'attachFiles_' + set_id + '_' + control_id;
93 // File input
94 var file_input = document.createElement('input');
95 file_input.setAttribute('type', 'file');
96 file_input.setAttribute('name', App.modules.attachFiles.sets[set_id]['prefix'] + '_' + control_id);
98 // Remove button
99 var remove_button = document.createElement('button');
100 remove_button.setAttribute('type', 'button');
101 remove_button.className = 'remove_button';
102 remove_button.appendChild(document.createTextNode( App.modules.attachFiles.remove_lang ));
104 /*remove_button.controlId = 1;*/
105 remove_button.onclick = function() {
106 App.modules.attachFiles.removeControl(set_id, control_id);
109 control_div.appendChild(file_input);
110 control_div.appendChild(remove_button);
112 $(App.modules.attachFiles.getSetControlsDivId(set_id)).appendChild(control_div);
114 App.modules.attachFiles.sets[set_id]['total_controls'] += 1;
117 removeControl : function(set_id, control_id) {
118 if(control_id == 1) return;
119 $(App.modules.attachFiles.getSetControlsDivId(set_id)).removeChild(
120 $(App.modules.attachFiles.getFileControlId(set_id, control_id))
121 ); // removeChild
122 App.modules.attachFiles.sets[set_id]['total_controls'] -= 1;
126 * Return prefix of specific set
128 * @param integer set_id
130 getSetPrefix : function(set_id) {
131 return App.modules.attachFiles.sets[set_id]['prefix'];
135 * Return all controls in specific set
137 * @param integer set_id
139 getSetControls : function(set_id) {
140 return App.modules.attachFiles.sets[set_id]['controls'];
144 * This function will return ID of set DIV
146 * @param integer set_id
147 * @return string
149 getSetControlId : function(set_id) {
150 return 'attachFiles_' + set_id;
154 * Retun ID of inner DIV that lists all file controls
156 * @param integer set_id
157 * @return string
159 getSetControlsDivId : function(set_id) {
160 return 'attachFilesControls_' + set_id;
164 * This function will return ID of control DIV based on set id and control ID
166 * @param integer set_id
167 * @param integer control_id
168 * @return string
170 getFileControlId : function(set_id, control_id) {
171 return 'attachFiles_' + set_id + '_' + control_id;