PageSourceSearch

https://modernantenna.jp/js/lib/auto_list.js

js modernantenna.jp collected 2026-10-10 00:44:56 UTC 8,073 bytes, 276 lines download raw bytes

1
2/**
3 * 入力項目のグループを自動的に増やしていく
4 *
5 *サーバ側での auto_list 関数を併用する必要がある
6 */
7$.widget("phf.auto_list",{
8	options: {
9		//option
10		selector : ".js_auto_list",
11		
12		//event
13		clone : function(){}
14		
15		//method
16	},
17	
18	/**
19	 * 新しい行を追加
20	 */
21	clone_list: function($list_item) {
22		//console.debug($list_item);
23		var $elm=this.element;
24		var ins=this;
25		var opt=ins.options;
26		var selector=opt.selector;
27		
28		if(!ins.is_last($list_item)){
29			return false;
30		}
31		
32		var list_id=$list_item.attr("list_id");
33		var max_index=ins.max_index($list_item);
34		
35		var $tmpl=$list_item;
36		//console.debug($tmpl);
37		if(!ins.is_tmpl($list_item)){
38			ins.list_live($(":input",$list_item),$list_item);
39			$tmpl=ins.get_tmpl($list_item);
40		}
41		
42		$new_elm=$($tmpl.outerHTML().replace(new RegExp("\-\-\\(auto_list"+list_id+":index\\)\-\-","g"),max_index+1));
43		
44		$new_elm.removeAttr('style').css({'opacity':'0'});
45		//$new_elm.insertBefore($tmpl).removeClass("js_list_tmpl").show();
46		$new_elm.insertBefore($tmpl).removeClass("js_list_tmpl").animate({'opacity':1},{'duration':333,'complete':function(){}});
47		
48		opt.clone($new_elm);	//event
49		/*特別記述*/
50		if($('#editor_'+(max_index+1)).is('*')){if(CKEDITOR.instances['editor_'+(max_index+1)]){}else{CKEDITOR.replace('editor_'+(max_index+1));}}
51		for(var i=0;i<=(max_index+1);i++){
52			if($('#editor1_'+i).is('*')){if(CKEDITOR.instances['editor1_'+i]){}else{CKEDITOR.replace('editor1_'+i);}}
53			if($('#editor2_'+i).is('*')){if(CKEDITOR.instances['editor2_'+i]){}else{CKEDITOR.replace('editor2_'+i);}}
54			if($('#editor3_'+i).is('*')){if(CKEDITOR.instances['editor3_'+i]){}else{CKEDITOR.replace('editor3_'+i);}}
55			if($('#editor4_'+i).is('*')){if(CKEDITOR.instances['editor4_'+i]){}else{CKEDITOR.replace('editor4_'+i);}}
56			if($('#editor5_'+i).is('*')){if(CKEDITOR.instances['editor5_'+i]){}else{CKEDITOR.replace('editor5_'+i);}}
57		}
58	},
59	
60	/**
61	 * 行削除
62	 */
63	delete_line: function($list_item) {
64		var $elm=this.element;
65		var ins=this;
66		var opt=ins.options;
67		var selector=opt.selector;
68		
69		if(!ins.is_last($list_item)){
70			$list_item.animate({'opacity':0},{'duration':333,'complete':function(){
71				$list_item.remove();
72			}});
73		}
74	},
75	
76	/**
77	 * 余分データ登録防止の為にnameに加えた変更を元に戻す
78	 */
79	list_live: function($input,$list_item) {
80		//console.debug($input);
81		var $elm=this.element;
82		var ins=this;
83		var opt=ins.options;
84		var selector=opt.selector;
85		var list_id=$list_item.attr("list_id");
86		
87		$input.attr("name",function(i,val){if(isset(val))return val.replace("__dummy__"+list_id,'')});
88	},
89	
90	//------------------------------------------
91	// common function
92	//------------------------------------------
93	
94	/**
95	 * 最大のlist_indexを取得
96	 */
97	max_index: function($list_item) {
98		var $elm=this.element;
99		var ins=this;
100		var opt=ins.options;
101		var selector=opt.selector;
102		
103		var indexes=ins.siblings($list_item).andSelf().not(".js_list_tmpl").map(function(){
104			return parseInt($(this).attr("list_index"));
105		}).toArray();
106		//console.debug(indexes);
107		
108		var max_index=-1;
109		if(indexes.length){
110			max_index=max(indexes);
111		};
112		
113		return parseInt(max_index);
114	},
115	
116	/**
117	 * テンプレートかどうか
118	 */
119	is_tmpl: function($list_item) {
120		var $elm=this.element;
121		var ins=this;
122		var opt=ins.options;
123		var selector=opt.selector;
124		
125		var flag=$list_item.is(".js_list_tmpl");
126		return flag;
127	},
128		
129	/**
130	 * 指定されたリストのテンプレートを取得
131	 */
132	get_tmpl: function($list_item) {
133		var $elm=this.element;
134		var ins=this;
135		var opt=ins.options;
136		var selector=opt.selector;
137		
138		var $result=$list_item.nextUntil(":not("+selector+")").andSelf().filter(".js_list_tmpl").eq(0);
139		return $result;
140	},
141	
142	/**
143	 * (テンプレートを除いて)リストの最後の要素かどうか
144	 */
145	is_last: function($list_item) {
146		var $elm=this.element;
147		var ins=this;
148		var opt=ins.options;
149		var selector=opt.selector;
150		
151		var flag=($list_item.is(selector) && !$list_item.nextUntil(":not("+selector+")",":not(.js_list_tmpl)").length);
152		return flag;
153	},
154
155	/**
156	 * 指定されたリストのテンプレートを除く最後のリスト
157	 */
158	get_last: function($list_item) {
159		var $elm=this.element;
160		var ins=this;
161		var opt=ins.options;
162		var selector=opt.selector;
163		
164		var $result=$list_item.nextUntil(":not("+selector+")").andSelf().not(".js_list_tmpl").last();
165		return $result;
166	},
167	
168	/**
169	 * 指定されたリスト要素と同じリストの要素を全て選択
170	 */
171	siblings: function($list_item) {
172		var $elm=this.element;
173		var ins=this;
174		var opt=ins.options;
175		var selector=opt.selector;
176		
177		//console.debug($list_item.prevUntil(":not("+selector+")").add($list_item.nextUntil(":not("+selector+")")).add($list_item).not(".js_list_tmpl"));
178		var $result=$list_item.prevUntil(":not("+selector+")").add($list_item.nextUntil(":not("+selector+")")).add($list_item).not(".js_list_tmpl");
179		return $result;
180	},
181	
182	/**
183	 * その要素が属しているリストを取得
184	 */
185	current_list: function($target) {
186		var $elm=this.element;
187		var ins=this;
188		var opt=ins.options;
189		var selector=opt.selector;
190		
191		return $target.parents(selector).eq(0);
192	},
193		
194	/**
195	 * その要素が属しているリストを親のリストも含めて全て取得
196	 */
197	current_lists: function($target) {
198		var $elm=this.element;
199		var ins=this;
200		var opt=ins.options;
201		var selector=opt.selector;
202		
203		return $target.parents(selector);
204	},
205		
206	//------------------------------------------
207	// init 処理
208	//------------------------------------------
209	
210	_init: function() {
211		var $elm=this.element;
212		var ins=this;
213		var opt=ins.options;
214		var selector=opt.selector;
215		
216		//行の削除
217		$(document).on("click",".js_list_delete",function(){
218		//$(".js_list_delete").live("click",function(){
219			if(confirm('このデータを削除します\nよろしいですか?')){
220				var $this=$(this);
221				//var $list_item=$this.parents(selector);
222				var $list_item=$this.parents(selector).eq(0);
223				ins.delete_line($list_item);
224			}
225			return false;
226		});
227		
228		//最後の要素はテンプレート用(サーバサイドもテンプレート用に一つ余分に出力するようにする)
229		$(selector).filter(function(){
230			return !$(this).next(selector).length;	//:last-childではリスト系以外のタグに対応できない為不可。ただこの処理は重いかもしれない
231		}).addClass('js_list_tmpl');
232		
233		//リストの最後の空データやテンプレートを加工(これらのデータを登録させない為にnameを変更する等)
234		$(".js_list_tmpl").hide();
235		$($(".js_list_tmpl").get().reverse()).each(function (){
236			var $val=$(this);
237			var $list_item=$(this);
238			$input=$val.find(":input");
239			var list_id=$list_item.attr("list_id");
240			$input.attr("name", function(i,val){if(isset(val) && val.indexOf("__dummy__"+list_id)==-1)return "__dummy__"+list_id+val;} );
241			
242			//ページのロード時点で最大より一つ多く生成しておく
243			var $last=ins.get_last($val);
244			if(!$last.length){
245				//リストがまだ一つもない場合
246				ins.clone_list($val);
247			}else{
248				$last.find(":input").last().change();
249			}
250		});
251		
252		//リストの最後の要素に何か入力されたら次の要素を用意
253		var clone_event={};
254
255		$(document).on("change keyup paste",selector+" :input",clone_event=function(){
256		//$(selector).find(":input").live("change keyup paste",clone_event=function(){
257			var $this=$(this);
258			ins.current_lists($this).each(function(key){
259				var $val=$(this);
260				ins.clone_list($val);
261			});
262		});
263		//装飾テキストエリアもバインド(ckeditorに対応)
264		$(document.body).delegate(selector+" iframe","mouseover",function(){
265			$("iframe").each(function(i){
266				var $iframe=$(this);
267				if(!$iframe.data("auto_list_bind")){
268					$iframe.data("auto_list_bind",true);
269					$(this).contents().delegate("body","change keyup paste",function(){ clone_event.apply($iframe.get(0)); });
270				}
271			});
272		})
273		
274	}
275});
276

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.