skycaiji/public/static/js/admin/collector.js

111 lines
50 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
|--------------------------------------------------------------------------
| SkyCaiji (蓝天采集器)
|--------------------------------------------------------------------------
| Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
|--------------------------------------------------------------------------
| 使用协议 https://www.skycaiji.com/licenses
|--------------------------------------------------------------------------
*/
'use strict';function CollectorPattern(formid){this.formid='#'+formid;this.processForm='';this.processBox=''}
CollectorPattern.prototype={constructor:CollectorPattern,init:function(){var $_o=this;$($_o.formid+' .coll-tab a[data-toggle="tab"]').on('shown.bs.tab',function(e){var activeTab=$(e.target).attr('href');$($_o.formid+' [name="tab_link"]').val(activeTab.replace(/^#+/,''))});$($_o.formid+' select[name="config[charset]"]').bind('change',function(){if($(this).val()=='custom'){$($_o.formid+' input[name="config[charset_custom]"]').show()}else{$($_o.formid+' input[name="config[charset_custom]"]').hide()}});$($_o.formid+' #coll_pattern_request_headers .dm-useragent li a').bind('click',function(){$($_o.formid+' [name="config[request_headers][useragent]"]').val($(this).attr('data-useragent'))});$(this.formid).on('click','[name="config[request_headers][open]"]',function(){if($(this).val()==1){$('#c_p_request_headers_open').show()}else{$('#c_p_request_headers_open').hide()}});$($_o.formid+' #coll_pattern_request_headers .add-request-header').bind('click',function(){$_o.add_request_header('','')});$($_o.formid+' .c-p-request-headers').on('click','.delete-request-header',function(){$(this).parents('tr').eq(0).remove()});$(this.formid+' #coll_pattern_source .add-source-url').bind('click',function(){windowModal('添加起始网址',ulink("Cpattern/source"))});$(this.formid+' #coll_pattern_source .glyphicon-trash').bind('click',function(){$_o.source_op('clear_all')});$(this.formid+' #coll_pattern_source').on('click','.glyphicon-edit',function(){var urlTxt=$(this).parents('.input-group').find('[name="config[source_url][]"]').val();urlTxt=urlTxt?urlTxt:'';var url=ulink("Cpattern/source");var urlId=$(this).parents('.input-group').find('[name="config[source_url][]"]').attr('id');urlId=urlId?urlId:'';var options={};if(urlTxt||urlId){options.ajax={type:'post',data:{url:urlTxt,uid:urlId}}}
windowModal('添加起始网址',url,options)});$(this.formid+' #coll_pattern_source').on('click','.glyphicon-remove',function(){var obj=$(this);confirmRight(window.tpl_lang.confirm_delete,function(){obj.parents('.form-group').eq(0).remove()})});eleExchange(this.formid+' #coll_pattern_source','.glyphicon-arrow-up','.glyphicon-arrow-down','.form-group');$(this.formid).find('[name="config[source_url][]"]').eq(0).attr('id',generateUUID());$('#coll_tab a[href="#coll_pattern_link"]').bind('click',function(){if($($_o.formid+' [name="config[source_is_url]"]').is(':checked')){toastr.warning('起始页网址已设置为内容页网址!');$('#panel_coll_pattern_level_url').hide();$('#panel_coll_pattern_cont_url').hide()}else{$('#panel_coll_pattern_level_url').show();$('#panel_coll_pattern_cont_url').show()}});$($_o.formid+' #coll_pattern_link_post .add-url-post').bind('click',function(){$_o.add_url_post('','')});$($_o.formid+' .c-p-url-posts').on('click','.delete-url-post',function(){$(this).parents('tr').eq(0).remove()});$($_o.formid+' #coll_pattern_level_url .add-level-url').bind('click',function(){var url=ulink("Cpattern/level_url");windowModal('添加多级网址规则',url)});$($_o.formid+' #c_p_level_urls').on('click','.name',function(){var parent=$(this).parents('[id^="level_url_"]').eq(0);var objid=parent.attr('id');var level_url=parent.find('[name="config[level_urls][]"]').val();var url=ulink("Cpattern/level_url?objid=_objid_&level_url=_val_",{_objid_:objid,_val_:level_url});windowModal('编辑多级网址规则',url)});$($_o.formid+' #c_p_level_urls').on('click','.delete',function(){var curObj=$(this);confirmRight('确定删除?',function(){curObj.parents('[id^="level_url_"]').eq(0).remove()})});eleExchange($_o.formid+' #c_p_level_urls','.glyphicon-arrow-up','.glyphicon-arrow-down','tr');$($_o.formid+' #coll_pattern_relation_url .add-relation-url').bind('click',function(){var url=ulink("Cpattern/relation_url");windowModal('添加关联页网址规则',url)});$($_o.formid+' #c_p_relation_urls').on('click','.name',function(){var parent=$(this).parents('[id^="relation_url_"]').eq(0);var objid=parent.attr('id');var relation_url=parent.find('[name="config[relation_urls][]"]').val();var url=ulink("Cpattern/relation_url?objid=_objid_&relation_url=_val_",{_objid_:objid,_val_:relation_url});windowModal('编辑关联页网址规则',url)});$($_o.formid+' #c_p_relation_urls').on('click','.delete',function(){var curObj=$(this);confirmRight('确定删除?',function(){curObj.parents('[id^="relation_url_"]').eq(0).remove()})});eleExchange($_o.formid+' #c_p_relation_urls','.glyphicon-arrow-up','.glyphicon-arrow-down','tr');$(this.formid+' #coll_pattern_field').on('click','.add-field,.field-name',function(){var url=ulink("Cpattern/field?field=_field_&objid=_objid_");var field=null;var objid=$(this).parents('tr[id^="field_"]').eq(0).attr('id');var title='添加字段';if(objid){field=$('#'+objid).find('input[name="config[field_list][]"]').val();title='编辑字段'}
url=url.replace('_field_',field?encodeURIComponent(field):'');url=url.replace('_objid_',objid?encodeURIComponent(objid):'');windowModal(title,url)});$(this.formid+' #coll_pattern_field').on('click','.field-del',function(){var obj=$(this);confirmRight(window.tpl_lang.confirm_delete,function(){obj.parents('tr').eq(0).remove()})});$(this.formid+' #coll_pattern_field').on('click','.field-process',function(){var url=ulink("Cpattern/process");var process=$(this).siblings('input[name="config[field_process][]"]').val();var prt=$(this).parents('tr[id^="field_"]').eq(0);var objid=prt.attr('id');var field=prt.find('.field-name').text();windowModal('数据处理:'+field,url,{ajax:{method:'post',data:{objid:objid,process:process}}})});$(this.formid+' #coll_pattern_process').on('click','.add-process',function(){var url=ulink("Cpattern/process?type=common");windowModal('数据处理(通用)',url)});$(this.formid).on('click','[name="config[paging][open]"]',function(){if($(this).val()==1){$('#c_p_paging_open').show()}else{$('#c_p_paging_open').hide()}});$(this.formid+' #coll_pattern_paging').on('click','.add-paging-field',function(){var url=ulink("Cpattern/paging_field");windowModal('分页内容字段',url)});$(this.formid+' #c_p_paging_fields').on('click','.field',function(){var parent=$(this).parents('.paging-field').eq(0);var objid=parent.attr('id');var pagingField=parent.find('[name="config[paging_fields][]"]').val();var url=ulink("Cpattern/paging_field?objid=_objid_&paging_field=_pfield_",{_objid_:objid,_pfield_:pagingField});windowModal('分页内容字段',url)});$(this.formid+' #c_p_paging_fields').on('click','.delete',function(){$(this).parents('.paging-field').eq(0).remove()});eleExchange(this.formid+' #coll_pattern_field','.glyphicon-arrow-up','.glyphicon-arrow-down','tr[id^="field_"]');$(this.formid).on('click','select[name="config[area_module]"],select[name="config[url_rule_module]"],select[name="config[paging][area_module]"],select[name="config[paging][url_rule_module]"]',function(){$_o.rule_module_slt(this)});$(this.formid+' [name="effective"]').val(1)},load:function(config){var $_o=this;if(config){$(this.formid+' [name="config[charset]"]').val(config.charset).trigger('change');$(this.formid+' [name="config[charset_custom]"]').val(config.charset_custom);$(this.formid+' [name="config[url_complete]"][value="'+parseInt(config.url_complete)+'"]').prop('checked','checked');$(this.formid+' [name="config[url_reverse]"][value="'+parseInt(config.url_reverse)+'"]').prop('checked','checked');$(this.formid+' [name="config[page_render]"][value="'+parseInt(config.page_render)+'"]').prop('checked','checked');$(this.formid+' [name="config[url_repeat]"][value="'+parseInt(config.url_repeat)+'"]').prop('checked','checked');if(config.request_headers){var r_h_params=new Array('useragent','cookie','referer');for(var i in r_h_params){$(this.formid+' [name="config[request_headers]['+r_h_params[i]+']"]').val(config.request_headers[r_h_params[i]])}
if(config.request_headers.custom_names){var r_h_vals=config.request_headers.custom_vals?config.request_headers.custom_vals:{};for(var i in config.request_headers.custom_names){$_o.add_request_header(config.request_headers.custom_names[i],r_h_vals[i])}}
if(config.request_headers.open){$(this.formid+' [name="config[request_headers][open]"][value="'+parseInt(config.request_headers.open)+'"]').trigger('click');if(parseInt(config.request_headers.open)>0){$('#coll_pattern_request_headers').addClass('in')}}
if(config.request_headers.download_img){$(this.formid+' [name="config[request_headers][download_img]"][value="'+parseInt(config.request_headers.download_img)+'"]').prop('checked','checked')}}
if(config.source_url){var source_url_html_list='';for(var i in config.source_url){source_url_html_list+=this.source_op('add',{get:1,url:config.source_url[i]})}
if(source_url_html_list){this.source_op('add',{html:source_url_html_list})}}
$(this.formid+' [name="config[source_is_url]"]').attr('checked',config.source_is_url?true:!1);if(config.area||config.area_start||config.area_end){if(config.area){}else if(config.area_start||config.area_end){config.area=config.area_start?config.area_start:'';if(config.area_end){config.area+=(window.tpl_lang.sign_match.replace('{:num}',''))+config.area_end}else{config.area+='(?<content>[\\s\\S]*)'}}
$(this.formid+' [name="config[area]"]').val(config.area);$('#coll_pattern_link_area').addClass('in').attr('aria-expanded',!0).attr('style','')}
if(config.area_module){$(this.formid+' select[name="config[area_module]"]').val(config.area_module).trigger('click').trigger('change')}
if(config.url_rule||config.url_merge){$(this.formid+' [name="config[url_rule]"]').val(config.url_rule);$(this.formid+' [name="config[url_merge]"]').val(config.url_merge);$('#coll_pattern_link_match').addClass('in').attr('aria-expanded',!0).attr('style','')}
if(config.url_rule_module){$(this.formid+' select[name="config[url_rule_module]"]').val(config.url_rule_module).trigger('click').trigger('change')}
if(config.url_must||config.url_ban){$(this.formid+' [name="config[url_must]"]').val(config.url_must);$(this.formid+' [name="config[url_ban]"]').val(config.url_ban);$('#coll_pattern_link_filter').addClass('in').attr('aria-expanded',!0).attr('style','')}
$(this.formid+' [name="config[url_post]"][value="'+parseInt(config.url_post)+'"]').prop('checked','checked');if(config.url_posts){if(config.url_posts.names){var u_p_vals=config.url_posts.vals?config.url_posts.vals:{};for(var i in config.url_posts.names){$_o.add_url_post(config.url_posts.names[i],u_p_vals[i])}}}
if(parseInt(config.url_post)>0){$('#coll_pattern_link_post').addClass('in').attr('aria-expanded',!0).attr('style','')}
if(config.level_urls){for(var i in config.level_urls){$_o.level_url_op('add',{level_url:config.level_urls[i]})}
$('#coll_pattern_level_url').addClass('in').attr('aria-expanded',!0).attr('style','')}
if(config.relation_urls){for(var i in config.relation_urls){$_o.relation_url_op('add',{relation_url:config.relation_urls[i]})}
$('#coll_pattern_relation_url').addClass('in').attr('aria-expanded',!0).attr('style','')}
if(config.field_list&&config.field_list.length>0){this.field_op('clearall');for(var i in config.field_list){var fieldProcess=null;if(config.field_process){fieldProcess=config.field_process[i]}
this.field_op('add',{field:config.field_list[i],process:fieldProcess})}}
if(config.field_title){$(this.formid+' [name="config[field_title]"]').each(function(){if($(this).val()==config.field_title){$(this).prop('checked','checked')}})}
if(config.common_process){$('#coll_pattern_process').addClass('in').attr('aria-expanded',!0).attr('style','');$.ajax({type:'post',url:ulink("Cpattern/process?type=common&op=load"),data:{process:config.common_process},dataType:'html',success:function(data){$('body').append(data)}})}
if(config.paging){for(var i in config.paging){var curEle=$(this.formid+' [name="config[paging]['+i+']"]');if(curEle.is('input:text')||curEle.is('textarea')){curEle.val(config.paging[i])}}
$(this.formid+' [name="config[paging][max]"]').val(parseInt(config.paging.max));if(config.paging.open){$(this.formid+' [name="config[paging][open]"][value="'+parseInt(config.paging.open)+'"]').trigger('click');if(parseInt(config.paging.open)>0){$('#coll_pattern_paging').addClass('in')}}
if(config.paging.area_module){$(this.formid+' select[name="config[paging][area_module]"]').val(config.paging.area_module).trigger('click').trigger('change')}
if(config.paging.url_rule_module){$(this.formid+' select[name="config[paging][url_rule_module]"]').val(config.paging.url_rule_module).trigger('click').trigger('change')}}
if(config.paging_fields){for(var i in config.paging_fields){$_o.paging_field_op('add',{paging_field:config.paging_fields[i]})}}}
$(document).ready(function(){var tab_link_val=$($_o.formid+' [name="tab_link"]').val();if(tab_link_val){tab_link_val='#'+tab_link_val.replace(/^#+/,'');$($_o.formid+' .coll-tab a[data-toggle="tab"]').each(function(){if(tab_link_val==$(this).attr('href')){$(this).trigger('click');return}})}})},source_op:function(op,params){var $_o=this;params=params?params:{};var formObj=params.formObj?params.formObj:'#form_source';if(op=='init'){$(formObj).find('.nav-tabs li a').bind('click',function(){$(formObj).find('input[name="source[type]"]').val($(this).attr('source-type'))});$(formObj).find('div[source-param]').find('input,textarea').bind('change',function(){if($(this).attr("type")=='radio'){return!1}
$(this).parents('div[source-param]').find('input[name="source[param]"]').prop('checked',!0)});if(params.source){$(formObj+' .nav-tabs').find('a[source-type="'+params.source.type+'"]').click();if(params.source.type=='custom'){$(formObj+' textarea[name="source[urls]"]').val(params.source.urls)}else if(params.source.type=='batch'){$(formObj+' input[name="source[url]"]').val(params.source.url);var param_type=params.source.param;if(param_type=='num'){$(formObj+' input[name="source[param_num_start]"]').val(params.source.param_num_start);$(formObj+' input[name="source[param_num_end]"]').val(params.source.param_num_end);$(formObj+' input[name="source[param_num_inc]"]').val(params.source.param_num_inc);if(params.source.param_num_desc){$(formObj+' input[name="source[param_num_desc]"]').attr('checked','checked')}}else if(param_type=='letter'){$(formObj+' input[name="source[param_letter_start]"]').val(params.source.param_letter_start);$(formObj+' input[name="source[param_letter_end]"]').val(params.source.param_letter_end);if(params.source.param_letter_desc){$(formObj+' input[name="source[param_letter_desc]"]').attr('checked','checked')}}else if(param_type=='custom'){$(formObj+' textarea[name="source[param_custom]"]').val(params.source.param_custom)}
$(formObj+' input[name="source[param]"][value="'+param_type+'"]').attr('checked','checked')}else if(params.source.type=='large'){$(formObj+' textarea[name="source[large_urls]"]').val(params.source.large_urls)}else if(params.source.type=='api'){$(formObj+' input[name="source[api]"]').val(params.source.api);$(formObj+' input[name="source[api_json]"]').val(params.source.api_json)}}}else if(op=='add'){var html='';if(params.html){html=params.html}else{var html='<div class="form-group"><div class="input-group">';var regLarge=/[\r\n]/;if(regLarge.test(params.url)){html+='<textarea class="form-control" name="config[source_url][]" rows="5" id="url_'+generateUUID()+'">'+htmlspecialchars(params.url)+'</textarea>'}else{html+='<input type="text" class="form-control" name="config[source_url][]" id="url_'+generateUUID()+'" value="'+htmlspecialchars(params.url)+'">'}
html=html+'<div class="input-group-addon brl_0"><a class="glyphicon glyphicon-edit"></a></div>'+'<div class="input-group-addon brl_0"><a class="glyphicon glyphicon-remove"></a></div>'+'<div class="input-group-addon"><a href="javascript:;" class="glyphicon glyphicon-arrow-up"></a> <a href="javascript:;" class="glyphicon glyphicon-arrow-down"></a></div></div></div>'}
if(params.get){return html}else{this.source_op('clear_null');$($_o.formid+' #coll_pattern_source .c-p-source-urls').append(html)}}else if(op=='add_sub'){$.ajax({type:'POST',dataType:'json',url:$(formObj).attr('action'),data:$(formObj).serialize(),success:function(data){data.data=data.data?data.data:{};if(data.code==1){var source_type=$(formObj).find('input[name="source[type]"]').val();if(source_type=='custom'){$('#myModal').modal('hide');var urls=data.data.urls;var ix=0;var url_html_list='';for(var i in urls){ix++;if(ix==1){if(data.data.uid){$('#'+data.data.uid).val(urls[i]);continue}}
url_html_list+=$_o.source_op('add',{get:1,url:urls[i]})}
if(url_html_list){$_o.source_op('add',{html:url_html_list})}}else if(source_type=='batch'){if(params.preview==1){var urls=data.data.urls;var txt='';for(var i in urls){txt+=urls[i]+"\r\n"}
$(formObj).find('#source_preview').val(txt)}else{if(data.data.uid){$('#'+data.data.uid).val(data.data.url)}else{$_o.source_op('add',{url:data.data.url})}
$('#myModal').modal('hide')}}else if(source_type=='large'){var large_urls=data.data.urls;large_urls=large_urls.join("\r\n");if(data.data.uid){var cur_ele=$('#'+data.data.uid).parents('.form-group').eq(0);var large_ele=$_o.source_op('add',{get:1,url:large_urls});$(cur_ele).replaceWith(large_ele)}else{$_o.source_op('add',{url:large_urls})}
$('#myModal').modal('hide')}else if(source_type=='api'){if(data.data.uid){$('#'+data.data.uid).val(data.data.url)}else{$_o.source_op('add',{url:data.data.url})}
$('#myModal').modal('hide')}}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}else if(op=='clear_null'){$($_o.formid+' #coll_pattern_source').find('[name="config[source_url][]"]').each(function(){if(!$(this).val()){$(this).parents('.form-group').eq(0).remove()}})}else if(op=='clear_all'){confirmRight('是否清空网址?',function(){$($_o.formid+' #coll_pattern_source').find('[name="config[source_url][]"]').each(function(){$(this).parents('.form-group').eq(0).remove()})})}},field_op:function(op,params){var $_o=this;params=params?params:{};var formObj=params.formObj?params.formObj:'#form_field';if(op=='init'){var level_urls=new Array();$('#c_p_level_urls [id^="level_url_"]').each(function(){var levelName=$(this).find('.name').text();level_urls.push('<option value="level_url:'+levelName+'">'+levelName+'</option>')});if(($($_o.formid+' [name="config[source_is_url]"]').is(':checked')!=!0)&&level_urls.length>0){$(formObj+' select[name="field[source]"]').append('<optgroup label="多级页">'+level_urls.join('')+'</optgroup>')}
var relation_urls=new Array();$('#c_p_relation_urls [id^="relation_url_"]').each(function(){var relationName=$(this).find('.name').text();relation_urls.push('<option value="relation_url:'+relationName+'">'+relationName+'</option>')});if(relation_urls.length>0){$(formObj+' select[name="field[source]"]').append('<optgroup label="关联页">'+relation_urls.join('')+'</optgroup>')}
$(formObj+' select[name="field[module]"]').bind('change',function(){$(formObj+' .c-p-field-module').hide();$(formObj+' .c-p-field-module[module="'+$(this).val()+'"]').show();var source_module=new Array('rule','xpath','json','auto');var in_module=!1;for(var i in source_module){if($(this).val()==source_module[i]){in_module=!0;break}}
if(in_module){$(formObj+' .c-p-field-source').show()}else{$(formObj+' select[name="field[source]"]').val('');$(formObj+' .c-p-field-source').hide()}});$(formObj+' select[name="field[xpath_attr]"]').bind('change',function(){if($(this).val()=='custom'){$(formObj+' [name="field[xpath_attr_custom]"]').show()}else{$(formObj+' [name="field[xpath_attr_custom]"]').hide()}});$(formObj+' select[name="field[json_arr]"]').bind('change',function(){if($(this).val()=='implode'){$(formObj+' [name="field[json_arr_implode]"]').show()}else{$(formObj+' [name="field[json_arr_implode]"]').hide()}});$(formObj+' select[name="field[extract_module]"]').bind('change',function(){$('.c-p-field-extract-module').hide();$('.c-p-field-extract-module[extract-module="'+$(this).val()+'"]').show()});$(formObj+' select[name="field[extract_json_arr]"]').bind('change',function(){if($(this).val()=='implode'){$(formObj+' [name="field[extract_json_arr_implode]"]').show()}else{$(formObj+' [name="field[extract_json_arr_implode]"]').hide()}});$(formObj+' select[name="field[extract_xpath_attr]"]').bind('change',function(){if($(this).val()=='custom'){$(formObj+' [name="field[extract_xpath_attr_custom]"]').show()}else{$(formObj+' [name="field[extract_xpath_attr_custom]"]').hide()}});var fieldList=$_o.get_fields();for(var i in fieldList){if(params.field&&params.field.name==fieldList[i]){continue}
$(formObj+' #c_p_field_merge_list').append('<a href="javascript:;" style="margin-right:10px;">[字段:'+fieldList[i]+']</a>');$(formObj+' [name="field[extract]"]').append('<option>'+fieldList[i]+'</option>')}
$(formObj+' #c_p_field_time_format_list').on('click','a',function(){insertAtCaret($(formObj+' [name="field[time_format]"]'),$(this).text())});$(formObj+' #c_p_field_merge_list').on('click','a',function(){insertAtCaret($(formObj+' [name="field[merge]"]'),$(this).text())});$(formObj+' [name="field[rule_multi]"]').on('change',function(){if($(this).is(':checked')){$('#c_p_field_rule_multi_str').show()}else{$('#c_p_field_rule_multi_str').hide()}});$(formObj+' [name="field[xpath_multi]"]').on('change',function(){if($(this).is(':checked')){$('#c_p_field_xpath_multi_str').show()}else{$('#c_p_field_xpath_multi_str').hide()}});$(formObj+' [name="field[page_rule_multi]"]').on('change',function(){if($(this).is(':checked')){$('#c_p_field_page_rule_multi_str').show()}else{$('#c_p_field_page_rule_multi_str').hide()}});$(formObj+' input[name="field[time_start]"],#form_field input[name="field[time_end]"]').datetimepicker({lang:'ch'});$(formObj+' select[name="field[time_format]"]').html();$(formObj).bind('submit',function(){$_o.field_op('add_sub');return!1});if(params.field){for(var i in params.field){var fieldEle=$(formObj+' [name="field['+i+']"]');if(fieldEle.is('input:text')){fieldEle.val(params.field[i])}else if(fieldEle.is('select')){fieldEle.val(params.field[i]).trigger("change")}}
$(formObj+' [name="field[rule_multi]"]').trigger("change");$(formObj+' [name="field[page_rule_multi]"]').trigger("change");$(formObj+' [name="field[xpath_multi]"]').trigger("change");$(formObj+' [name="field[auto]"][value="'+params.field.auto+'"]').prop('checked',!0);$(formObj+' [name="field[rule_multi_type]"][value="'+(params.field.rule_multi_type?params.field.rule_multi_type:'')+'"]').prop('checked',!0);$(formObj+' [name="field[xpath_multi_type]"][value="'+(params.field.xpath_multi_type?params.field.xpath_multi_type:'')+'"]').prop('checked',!0)}}else if(op=='add'){var fieldSource='默认页';if(params.field.source){if('source_url'==params.field.source){fieldSource='起始页'}else if(params.field.source.indexOf('level_url:')>-1){fieldSource='多级页:'+params.field.source.replace('level_url:','')}else if(params.field.source.indexOf('relation_url:')>-1){fieldSource='关联页:'+params.field.source.replace('relation_url:','')}}
var isLoop='';if(params.field.module=='rule'){if(params.field.rule_multi&&params.field.rule_multi_type=='loop'){isLoop=' - 循环入库'}}else if(params.field.module=='xpath'){if(params.field.xpath_multi&&params.field.xpath_multi_type=='loop'){isLoop=' - 循环入库'}}else if(params.field.module=='json'){if(params.field.json_loop){isLoop=' - 循环入库'}}
if(isLoop){$('#c_p_field_loop_tips').show()}
if(params.objid){var eleObj=$($_o.formid+' #'+params.objid);eleObj.find('.field-name').text(params.field.name);eleObj.find('.field-source').text(fieldSource);eleObj.find('.field-module').text(window.tpl_lang['field_module_'+params.field.module]+isLoop);eleObj.find('input[name="config[field_list][]"]').val(url_base64encode(JSON.stringify(params.field)))}else{var ptitle='';if(params.process){ptitle=[];for(var i in params.process){ptitle.push(window.tpl_lang['process_module_'+params.process[i].module]+(params.process[i].title?(''+params.process[i].title):''))}
ptitle=ptitle.join(' / ')}
var html=$_o.clone_tpl('#coll_tpl_field');html.attr('id','field_'+generateUUID());html.find('.field-name').text(params.field.name);html.find('.field-source').text(fieldSource);html.find('.field-module').text(window.tpl_lang['field_module_'+params.field.module]+isLoop);html.find('[name="config[field_list][]"]').val(url_base64encode(JSON.stringify(params.field)));html.find('[name="config[field_process][]"]').val(params.process?url_base64encode(JSON.stringify(params.process)):'');if(params.process){html.find('.field-process').addClass('exist-process')}
html.find('.field-process').attr('title',ptitle);html.find('[name="config[field_title]"]').val(params.field.name);$($_o.formid+' #coll_pattern_field .c-p-field-list').append(html)}}else if(op=='add_sub'){var objid=$(formObj+' input[name="objid"]').val();var checkName=!0;if(objid){var fname=$($_o.formid+' #'+objid).find('.field-name').text();if(fname==$(formObj+' input[name="field[name]"]').val()){checkName=!1}}
if(checkName){var hasName=!1;var fieldList=$_o.get_fields();for(var i in fieldList){if($(formObj+' input[name="field[name]"]').val()==fieldList[i]){hasName=!0;break}}
if(hasName){toastr.error('字段名称已存在!');return!1}}
$.ajax({type:'POST',dataType:'json',url:$(formObj).attr('action'),data:$(formObj).serialize(),success:function(data){if(data.code==1){$('#myModal').modal('hide');$_o.field_op('add',data.data)}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}else if(op=='clearall'){$($_o.formid+' #coll_pattern_field .c-p-field-list tbody').html('')}},process_op:function(op,params){var $_o=this;params=params?params:{};var defaultForm='#form_process';if(op=='init'){$_o.processForm=params.formObj?params.formObj:defaultForm;$_o.processBox=params.boxObj?params.boxObj:'#window_process';if($($_o.processForm).is('form')){$($_o.processForm).bind('submit',function(){$_o.process_op('add_sub');return!1})}
$($_o.processBox+' .process-add').bind('click',function(){var module=$($_o.processBox+' select[name="process[module]"]').val();$_o.process_op('add',{'add_new':1,'module':module})});if($($_o.processForm).prop('inited')==1){return!0}
$($_o.processForm).on('click','.p-m-html-tags a',function(){var tag=$(this).find('span').text();var moduleHtml=$(this).parents('.p-m-html-tags').eq(0).attr('module-html');var tagsObj=$(this).parents('section').eq(0).find('input[data-process="html:'+moduleHtml+'"]');var tags=tagsObj.val()+','+tag;tags=tags.replace(/(^,+)|(,+$)/,'');tagsObj.val(tags)});$($_o.processForm).on('click','.p-m-func-fields .dropdown-toggle',function(){if(!$(this).attr('data-fields-tpl')){$(this).attr('data-fields-tpl',$(this).siblings('.dropdown-menu').html())}
var fieldList=$_o.get_fields();if(fieldList){var html='';for(var i in fieldList){html+='<li><a href="javascript:;" data-val="[字段:'+fieldList[i]+']">[字段:'+fieldList[i]+']</a></li>'}
html=$(this).attr('data-fields-tpl')+html;$(this).siblings('.dropdown-menu').html(html)}});$($_o.processForm).on('click','.p-m-func-fields .dropdown-menu a',function(){var val=$(this).attr('data-val');if(val){var obj=$(this).parents('section').eq(0).find('[data-process="func:func_param"]');obj.val(obj.val()+val)}});$($_o.processForm).on('click','.p-m-if-add',function(){var ifTable=$(this).parents('section').eq(0).find('.p-m-if-table');ifTable.append('<tr>'+ifTable.attr('data-tpl')+'</tr>')});$($_o.processForm).on('click','.p-m-if-del',function(){var tr=$(this).parents('tr').eq(0);if(tr.next().attr('data-p-m-if-tips')=='1'){tr.next().remove()}
tr.remove()});$($_o.processForm).on('focus','[data-process="if:if_field:"]',function(){$_o.p_if_field_select($(this),$(this).val())});$($_o.processForm).on('change','[data-process="if:if_cond:"]',function(){var ifCond=$(this).val();var ifTr=$(this).parents('tr').eq(0);var ifVal=ifTr.find('input[data-process="if:if_val:"],textarea[data-process="if:if_val:"]');var ifTd=ifVal.parents('td').eq(0);ifTd.removeClass('input-group');ifTd.children().hide();ifTd.children('input,textarea').show();if(ifTr.next().attr('data-p-m-if-tips')=='1'){ifTr.next().remove()}
if(ifCond=='func'){ifTd.find('.p-m-if-func').show();if(ifVal.is('input')){ifTd.append('<textarea rows="2" data-process="if:if_val:" placeholder="默认传入当前字段的值" class="form-control"></textarea>');ifTd.find('textarea[data-process="if:if_val:"]').attr('name',ifVal.attr('name')).val(ifVal.val());ifVal.remove()}
$_o.process_op('load_if_func',{box:ifTd})}else{if(ifCond.indexOf('time_')==0){ifTd.addClass('input-group');ifTd.find('.p-m-if-time').show()}
if(ifVal.is('textarea')){ifTd.prepend('<input type="text" data-process="if:if_val:" class="form-control" />');ifTd.find('input[data-process="if:if_val:"]').attr('name',ifVal.attr('name')).val(ifVal.val());ifVal.remove()}}});$($_o.processForm).on('change','.p-m-if-time-val',function(){$(this).parents('tr').eq(0).find('[data-process="if:if_val:"]').val($(this).val())});$($_o.processForm).on('click','.p-m-if-func-info',function(){var tips=$(this).parents('td').eq(0).find('.p-m-if-func-tips').html();var tr=$(this).parents('tr').eq(0);if(tr.next().attr('data-p-m-if-tips')!='1'){tr.after('<tr data-p-m-if-tips="1"><td colspan="4" style="color:#999;">'+tips+'</td></tr>')}else{if(tr.next().is(':hidden')){tr.next().show()}else{tr.next().hide()}}});$($_o.processForm).on('click','.p-m-api-add',function(){var apiTable=$(this).parents('section').eq(0).find('.p-m-api-table');apiTable.append('<tr>'+apiTable.attr('data-tpl')+'</tr>')});$($_o.processForm).on('click','.p-m-api-del',function(){var tr=$(this).parents('tr').eq(0);tr.remove()});$($_o.processForm).on('change','[data-process="api:api_params:val:"]',function(){var val=$(this).val();var ipt=$(this).parents('td').eq(0).find('[data-process="api:api_params:addon:"]');if(val=='time'||val=='custom'){if(val=='time'){ipt.attr('placeholder','默认格式Y-m-d H:i:s')}else{ipt.attr('placeholder','')}
ipt.show()}else{ipt.hide()}});$($_o.processForm).on('change','[data-process="api:api_json_arr"]',function(){var ipt=$(this).parent().find('[data-process="api:api_json_implode"]');if($(this).val()=='implode'){ipt.show()}else{ipt.hide()}});$($_o.processForm).on('click','.sign-wildcard',function(){var toObj=$(this).parent().siblings('[data-process="replace:replace_from"]');cpWildcard(toObj)});$($_o.processForm).on('click','.glyphicon-tags',function(){var panelTitle=$(this).parents('.panel').eq(0).find('.panel-title-title');panelTitle.find('*').hide();panelTitle.find('input').show()});eleExchange($_o.processForm+' .c-p-process-accordion','.glyphicon-arrow-up','.glyphicon-arrow-down','.panel');if(params.process){for(var i in params.process){if(params.process[i]){$_o.process_op('add',params.process[i])}}}
$($_o.processForm).prop('inited',1)}else if(op=='add'){if(!params.module){toastr.error('请选择处理方式');return!1}
params.module=htmlspecialchars(params.module);params.title=params.title?htmlspecialchars(params.title):'';var parentid=$($_o.processForm+' .c-p-process-accordion').attr('id');if(!parentid){parentid='p_accordion_'+generateUUID();$($_o.processForm+' .c-p-process-accordion').attr('id',parentid)}
var dataParent=parentid?('data-parent="#'+parentid+'"'):'';var moduleHtml=$($_o.processBox+' .c-p-process-module[module="'+params.module+'"]').html();if(params.module=='html'){moduleHtml=moduleHtml.replace(/p_m_html_allow/ig,'p_m_html_allow_'+generateUUID());moduleHtml=moduleHtml.replace(/p_m_html_filter/ig,'p_m_html_filter_'+generateUUID())}
var processName='process[i_'+generateUUID()+']';moduleHtml='<input type="hidden" name="'+processName+'[module]" value="'+params.module+'" />'+moduleHtml;var collapseId='p_collapse_'+generateUUID();var html=$_o.clone_tpl('#coll_tpl_process');html.attr('data-name',processName);html.find('a[data-toggle="collapse"]').attr('data-parent','#'+parentid).attr('href','#'+collapseId);if(params.title){html.find('.panel-title > a[data-toggle="collapse"]').text(window.tpl_lang['process_module_'+params.module]+'');html.find('.panel-title-title > a[data-toggle="collapse"]').text(params.title).show()}else{html.find('.panel-title > a[data-toggle="collapse"]').text(window.tpl_lang['process_module_'+params.module]);html.find('.panel-title-title > a[data-toggle="collapse"]').hide()}
html.find('.panel-title-title > input:text').attr('name',processName+'[title]').val(params.title?params.title:'');html.find('.panel-collapse').attr('id',collapseId);if(params.add_new){html.find('.panel-collapse').addClass('in')}
html.find('.panel-body').html(moduleHtml);$($_o.processForm+' .c-p-process-accordion').append(html);$($_o.processForm).show();var curCollapse=$_o.processForm+' #'+collapseId;$(curCollapse).find('[data-process]').each(function(){var eleName=$(this).attr('data-process').split(':');eleName[1]=processName+'['+eleName[1]+']';if(eleName.length>=3){eleName[1]+=eleName[2]?('['+eleName[2]+']'):'[]'}
if(eleName.length>=4){eleName[1]+=eleName[3]?('['+eleName[3]+']'):'[]'}
$(this).attr('name',eleName[1])});if(params.module=='html'){$(curCollapse).find('[data-process="html:html_allow"]').val(params.html_allow?params.html_allow:'');$(curCollapse).find('[data-process="html:html_filter"]').val(params.html_filter?params.html_filter:'');if(params.html_filter){$(curCollapse).find('a[href^="#p_m_html_filter"]').tab('show')}}else if(params.module=='replace'){$(curCollapse).find('[data-process="replace:replace_from"]').val(params.replace_from?params.replace_from:'');$(curCollapse).find('[data-process="replace:replace_to"]').val(params.replace_to?params.replace_to:'')}else if(params.module=='filter'){$(curCollapse).find('[data-process="filter:filter_list"]').val(params.filter_list?params.filter_list:'');$(curCollapse).find('[data-process="filter:filter_replace"]').val(params.filter_replace?params.filter_replace:'');$(curCollapse).find('[data-process="filter:filter_pass"][value="'+params.filter_pass+'"]').prop('checked',!0)}else if(params.module=='tool'){$(curCollapse).find('[data-process="tool:tool_list"]').attr('name',processName+'[tool_list][]');if(params.tool_list){for(var ti in params.tool_list){$(curCollapse).find('[data-process="tool:tool_list"][value="'+params.tool_list[ti]+'"]').prop('checked',!0)}}}else if(params.module=='translate'){$(curCollapse).find('[data-process="translate:translate_from"]').val(params.translate_from?params.translate_from:'');$(curCollapse).find('[data-process="translate:translate_to"]').val(params.translate_to?params.translate_to:'')}else if(params.module=='batch'){$(curCollapse).find('[data-process="batch:batch_list"]').val(params.batch_list?params.batch_list:'')}else if(params.module=='substr'){$(curCollapse).find('[data-process="substr:substr_len"]').val(params.substr_len?params.substr_len:'');$(curCollapse).find('[data-process="substr:substr_end"]').val(params.substr_end?params.substr_end:'')}else if(params.module=='func'){var txtId='cache_process_func_process';if(document.getElementById(txtId)){$(curCollapse).find('[data-process="func:func_name"]').append($('#'+txtId).val())}else{$.ajax({type:'GET',dataType:'json',url:ulink('Cpattern/process_func'),async:!0,data:{module:'process'},success:function(data){if(data.code==1&&data.data){var apps=data.data;var html='';for(var app in apps){if(apps[app].methods){html+='<optgroup label="'+htmlspecialchars(apps[app].name+''+app+'')+'">';var methods=apps[app].methods;for(var m in methods){html+='<option value="'+app+':'+m+'">'+htmlspecialchars(methods[m].comment+''+m)+'</option>'}
html+='</optgroup>'}}
$('#'+txtId).remove();$('body').append('<textarea style="display:none;" id="'+txtId+'"></textarea>');$('#'+txtId).val(html);$(curCollapse).find('[data-process="func:func_name"]').append(html).val(params.func_name?params.func_name:'')}},error:function(data){toastr.error(data)}})}
$(curCollapse).find('[data-process="func:func_name"]').val(params.func_name?params.func_name:'');$(curCollapse).find('[data-process="func:func_param"]').val(params.func_param?params.func_param:'')}else if(params.module=='if'){var ifTrTpl=$(curCollapse).find('.p-m-if-table-tpl');var ifTable=$(curCollapse).find('.p-m-if-table');ifTable.attr('data-tpl',ifTrTpl.html());ifTrTpl.remove();if(params.if_type){$(curCollapse).find('[data-process="if:if_type"]').val(params.if_type)}
if(params.if_logic&&params.if_cond&&params.if_val){params.if_addon=params.if_addon?params.if_addon:{};for(var i in params.if_logic){ifTable.find('tbody').append('<tr data-if-id="'+i+'">'+ifTable.attr('data-tpl')+'</tr>');var curIfTr=ifTable.find('tr[data-if-id="'+i+'"]');curIfTr.find('[data-process="if:if_logic:"]').val(params.if_logic[i]);curIfTr.find('[data-process="if:if_cond:"]').val(params.if_cond[i]).trigger('change');curIfTr.find('[data-process="if:if_val:"]').val(params.if_val[i]);if(params.if_field&&params.if_field[i]){$_o.p_if_field_select(curIfTr.find('[data-process="if:if_field:"]'),params.if_field[i])}
if(params.if_cond[i]=='func'){var ifFuncVal='';if(params.if_addon.func){ifFuncVal=params.if_addon.func[i]}
$_o.process_op('load_if_func',{box:curIfTr,setVal:ifFuncVal});if(params.if_addon.turn){curIfTr.find('[data-process="if:if_addon:turn:"]').val(params.if_addon.turn[i])}}}}}else if(params.module=='api'){var apiTrTpl=$(curCollapse).find('.p-m-api-table-tpl');var apiTable=$(curCollapse).find('.p-m-api-table');apiTable.attr('data-tpl',apiTrTpl.html());apiTrTpl.remove();$(curCollapse).find('[data-process="api:api_url"]').val(params.api_url?params.api_url:'');$(curCollapse).find('[data-process="api:api_type"]').val(params.api_type?params.api_type:'');if(params.api_params){params.api_params.name=params.api_params.name?params.api_params.name:{};params.api_params.val=params.api_params.val?params.api_params.val:{};params.api_params.addon=params.api_params.addon?params.api_params.addon:{};for(var i in params.api_params.name){var trId='p-m-api-param_'+generateUUID();var trTpl='<tr id="'+trId+'">'+apiTable.attr('data-tpl')+'</tr>';apiTable.find('tbody').append(trTpl);apiTable.find('#'+trId+' [data-process="api:api_params:name:"]').val(params.api_params.name[i]);apiTable.find('#'+trId+' [data-process="api:api_params:val:"]').val(params.api_params.val[i]?params.api_params.val[i]:'').trigger('change');apiTable.find('#'+trId+' [data-process="api:api_params:addon:"]').val(params.api_params.addon[i]?params.api_params.addon[i]:'')}}
$(curCollapse).find('[data-process="api:api_json"]').val(params.api_json?params.api_json:'');$(curCollapse).find('[data-process="api:api_json_arr"]').val(params.api_json_arr?params.api_json_arr:'implode').trigger('change');$(curCollapse).find('[data-process="api:api_json_implode"]').val(params.api_json_implode?params.api_json_implode:'')}
if($_o.processForm!=defaultForm){$('#myModal').modal('hide')}}else if(op=='add_sub'){$.ajax({type:'POST',dataType:'json',url:$($_o.processForm).attr('action'),data:$($_o.processForm).serialize(),success:function(data){if(data.code==1){$('#myModal').modal('hide');if(data.data&&data.data.objid){$($_o.formid+' #'+data.data.objid).find('input[name="config[field_process][]"]').val(data.data.process_json?url_base64encode(data.data.process_json):'');if(data.data.process){var process=data.data.process;var ptitle=[];for(var i in process){ptitle.push(window.tpl_lang['process_module_'+process[i].module]+(process[i].title?(''+process[i].title):''))}
ptitle=ptitle.join(' / ');$($_o.formid+' #'+data.data.objid).find('.field-process').addClass('exist-process').attr('title',ptitle)}else{$($_o.formid+' #'+data.data.objid).find('.field-process').removeClass('exist-process').attr('title','')}}}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}else if(op=='delete'){confirmRight('是否删除?',function(){$(params.obj).parents('.panel').eq(0).remove()})}else if(op=='load_if_func'){var box=params.box;var setVal=params.setVal?params.setVal:null;var obj=box.find('[data-process="if:if_addon:func:"]');var txtId='cache_process_func_process_if';if(obj.attr('data-is-loaded')){if(setVal){obj.val(setVal)}}else{if(document.getElementById(txtId)){obj.attr('data-is-loaded',1);obj.append($('#'+txtId).val());if(setVal){obj.val(setVal)}}else{$.ajax({type:'GET',dataType:'json',url:ulink('Cpattern/process_func'),async:!0,data:{module:'processIf'},success:function(data){if(obj.attr('data-is-loaded')){if(setVal){obj.val(setVal)}}else if(data.code==1&&data.data){obj.attr('data-is-loaded',1);var apps=data.data;var html='';for(var app in apps){if(apps[app].methods){html+='<optgroup label="'+htmlspecialchars(apps[app].name+''+app+'')+'">';var methods=apps[app].methods;for(var m in methods){html+='<option value="'+app+':'+m+'">'+htmlspecialchars(methods[m].comment+''+m)+'</option>'}
html+='</optgroup>'}}
$('#'+txtId).remove();$('body').append('<textarea style="display:none;" id="'+txtId+'"></textarea>');$('#'+txtId).val(html);obj.append(html);if(setVal){obj.val(setVal)}}},error:function(data){toastr.error(data);obj.removeAttr('data-is-loaded')}})}}}},p_if_field_select:function(obj,val){var fieldList=this.get_fields();var fieldOptions='<option value="">--全部--</option>';for(var i in fieldList){fieldOptions+='<option value="'+fieldList[i]+'">'+fieldList[i]+'</option>'}
obj.html(fieldOptions).val(val)},paging_field_op:function(op,params){var $_o=this;params=params?params:{};var formObj=params.formObj?params.formObj:'#form_paging_field';if(op=='init'){var fieldList=$_o.get_fields();var fieldOptions='<option value="">--请选择--</option><option value="::all">-全部字段-</option>';for(var i in fieldList){fieldOptions+='<option value="'+fieldList[i]+'">'+fieldList[i]+'</option>'}
$(formObj+' select[name="paging_field[field]"]').html(fieldOptions);$(formObj).bind('submit',function(){$_o.paging_field_op('add_sub');return!1});if(params.paging_field){$(formObj+' select[name="paging_field[field]"]').val(params.paging_field.field);$(formObj+' [name="paging_field[delimiter]"]').val(params.paging_field.delimiter)}}else if(op=='add'){params.paging_field=params.paging_field?params.paging_field:{};var tpl='<a href="javascript:;" class="field" data-field="__field__">__field_name__</a><em class="glyphicon glyphicon-remove-circle delete"></em><input type="hidden" name="config[paging_fields][]" value="__paging_field__" />';var fieldName=params.paging_field.field=='::all'?'-全部字段-':params.paging_field.field;tpl=tpl.replace(/__field__/ig,params.paging_field.field).replace(/__field_name__/ig,htmlspecialchars(fieldName)).replace(/__paging_field__/ig,url_base64encode(JSON.stringify(params.paging_field)));var objid=params.objid;if(!objid){objid='paging_field_'+generateUUID();$('#c_p_paging_fields').append('<div class="param-label paging-field" id="'+objid+'"></div>')}
$('#'+objid).html(tpl)}else if(op=='add_sub'){var objid=$(formObj+' input[name="objid"]').val();var checkField=!0;if(objid){var field=$($_o.formid+' #'+objid).find('.field').attr('data-field');if(field==$(formObj+' select[name="paging_field[field]"]').val()){checkField=!1}}
if(checkField){var hasField=!1;var fieldList=new Array();$('#c_p_paging_fields .paging-field .field').each(function(){fieldList.push($(this).attr('data-field'))});for(var i in fieldList){if($(formObj+' select[name="paging_field[field]"]').val()==fieldList[i]){hasField=!0;break}}
if(hasField){toastr.error('该字段已存在!');return!1}}
$.ajax({type:'POST',dataType:'json',url:$(formObj).attr('action'),data:$(formObj).serialize(),success:function(data){if(data.code==1){$('#myModal').modal('hide');$_o.paging_field_op('add',data.data)}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}},add_default_fields:function(){var $_o=this;confirmRight('添加默认字段会清除当前字段列表,是否继续?',function(){$_o.field_op('clearall');var defFields=new Array({"name":"标题","module":"auto","auto":"title"},{"name":"正文","module":"auto","auto":"content"},{"name":"keywords","module":"auto","auto":"keywords"},{"name":"description","module":"auto","auto":"description"});for(var i in defFields){$_o.field_op('add',{field:defFields[i]})}})},add_request_header:function(name,val){name=name?name:'';val=val?val:'';var $_o=this;var tr=$_o.clone_tpl('#coll_tpl_request_headers');tr.find('[name="config[request_headers][custom_names][]"]').val(name);tr.find('[name="config[request_headers][custom_vals][]"]').val(val);$($_o.formid+' #coll_pattern_request_headers table.c-p-request-headers tbody').append(tr)},add_url_post:function(name,val){name=name?name:'';val=val?val:'';var $_o=this;var tr=$_o.clone_tpl('#coll_tpl_url_posts');tr.find('[name="config[url_posts][names][]"]').val(name);tr.find('[name="config[url_posts][vals][]"]').val(val);$($_o.formid+' #coll_pattern_link_post table.c-p-url-posts tbody').append(tr)},get_fields:function(){var fields=new Array();$(this.formid+' #coll_pattern_field .c-p-field-list').find('.field-name').each(function(){var fieldName=$(this).text();if(fieldName){fields.push(fieldName)}});return fields},level_url_op:function(op,params){var $_o=this;params=params?params:{};var formObj=params.formObj?params.formObj:'#form_level_url';if(op=='init'){$(formObj).bind('submit',function(){$_o.level_url_op('add_sub');return!1});$(formObj).on('click','select[name="level_url[area_module]"],select[name="level_url[url_rule_module]"]',function(){$_o.rule_module_slt(this)});if(params.level_url){for(var i in params.level_url){$(formObj+' [name="level_url['+i+']"]').val(params.level_url[i])}
if(params.level_url.area_module){$(formObj+' select[name="level_url[area_module]"]').val(params.level_url.area_module).trigger('click').trigger('change')}
if(params.level_url.url_rule_module){$(formObj+' select[name="level_url[url_rule_module]"]').val(params.level_url.url_rule_module).trigger('click').trigger('change')}}}else if(op=='add'){if(params.objid){var eleObj=$($_o.formid+' #'+params.objid);eleObj.find('.name').text(params.level_url.name);eleObj.find('input[name="config[level_urls][]"]').val(url_base64encode(JSON.stringify(params.level_url)))}else{var html=$_o.clone_tpl('#coll_tpl_level_url');html.attr('id','level_url_'+generateUUID());html.find('.name').text(params.level_url.name);html.find('[name="config[level_urls][]"]').val(url_base64encode(JSON.stringify(params.level_url)));$($_o.formid+' #c_p_level_urls').append(html)}}else if(op=='add_sub'){var objid=$(formObj+' input[name="objid"]').val();var checkName=!0;if(objid){var name=$($_o.formid+' #'+objid).find('.name').text();if(name==$(formObj+' [name="level_url[name]"]').val()){checkName=!1}}
if(checkName){var hasName=!1;$('#c_p_level_urls [id^="level_url_"] .name').each(function(){if($(formObj+' [name="level_url[name]"]').val()==$(this).text()){hasName=!0;return!1}});if(hasName){toastr.error('该名称已存在!');return!1}}
$.ajax({type:'POST',dataType:'json',url:$(formObj).attr('action'),data:$(formObj).serialize(),success:function(data){if(data.code==1){$('#myModal').modal('hide');$_o.level_url_op('add',data.data)}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}},relation_url_op:function(op,params){var $_o=this;params=params?params:{};var formObj=params.formObj?params.formObj:'#form_relation_url';if(op=='init'){$(formObj).bind('submit',function(){$_o.relation_url_op('add_sub');return!1});$(formObj).on('click','select[name="relation_url[area_module]"],select[name="relation_url[url_rule_module]"]',function(){$_o.rule_module_slt(this)});var optRelations='<optgroup label="关联页">';$('#c_p_relation_urls [id^="relation_url_"] .name').each(function(){if(params.relation_url&&$(this).text()==params.relation_url.name){return!0}
optRelations+='<option value="'+$(this).text()+'">'+$(this).text()+'</option>'});optRelations+='</optgroup>';$(formObj+' [name="relation_url[page]"]').append(optRelations);if(params.relation_url){for(var i in params.relation_url){$(formObj+' [name="relation_url['+i+']"]').val(params.relation_url[i])}
if(params.relation_url.area_module){$(formObj+' select[name="relation_url[area_module]"]').val(params.relation_url.area_module).trigger('click').trigger('change')}
if(params.relation_url.url_rule_module){$(formObj+' select[name="relation_url[url_rule_module]"]').val(params.relation_url.url_rule_module).trigger('click').trigger('change')}}}else if(op=='add'){var relationPage=params.relation_url.page?params.relation_url.page:'内容页';if(params.objid){var eleObj=$($_o.formid+' #'+params.objid);eleObj.find('.name').text(params.relation_url.name);eleObj.find('.page').text(relationPage);eleObj.find('input[name="config[relation_urls][]"]').val(url_base64encode(JSON.stringify(params.relation_url)))}else{var html=$_o.clone_tpl('#coll_tpl_relation_url');html.attr('id','relation_url_'+generateUUID());html.find('.name').text(params.relation_url.name);html.find('.page').text(relationPage);html.find('[name="config[relation_urls][]"]').val(url_base64encode(JSON.stringify(params.relation_url)));$($_o.formid+' #c_p_relation_urls').append(html)}}else if(op=='add_sub'){var objid=$(formObj+' input[name="objid"]').val();var checkName=!0;if(objid){var name=$($_o.formid+' #'+objid).find('.name').text();if(name==$(formObj+' [name="relation_url[name]"]').val()){checkName=!1}}
if(checkName){var hasName=!1;$('#c_p_relation_urls [id^="relation_url_"] .name').each(function(){if($(formObj+' [name="relation_url[name]"]').val()==$(this).text()){hasName=!0;return!1}});if(hasName){toastr.error('该名称已存在!');return!1}}
$.ajax({type:'POST',dataType:'json',url:$(formObj).attr('action'),data:$(formObj).serialize(),success:function(data){if(data.code==1){$('#myModal').modal('hide');$_o.relation_url_op('add',data.data)}else{toastr.error(data.msg)}},error:function(data){toastr.error(data)}});return!1}},rule_module_slt:function(curObj){curObj=$(curObj);var module=curObj.val();if(curObj.attr('data-rule-input')){var ipt=$('[name="'+curObj.attr('data-rule-input')+'"]');ipt.attr('data-val-'+module,ipt.val())}
curObj.off('change').on('change',function(){var obj=$(this);var name=obj.attr('name');var changeModule=obj.val();var op=$('[data-rule-op="'+name+'"]');var set=$('[data-rule-set="'+name+'"]');op.find('[data-module]').hide();set.find('[data-module]').hide();op.find('[data-module="'+changeModule+'"]').show();set.find('[data-module="'+changeModule+'"]').show();if(obj.attr('data-rule-input')){var ipt=$('[name="'+obj.attr('data-rule-input')+'"]');if(!ipt.attr('data-placeholder-')){ipt.attr('data-placeholder-',ipt.prop('placeholder')+' ')}
if(ipt.attr('data-placeholder-'+changeModule)){ipt.prop('placeholder',ipt.attr('data-placeholder-'+changeModule))}else{ipt.prop('placeholder','')}
if(ipt.attr('data-val-'+changeModule)){ipt.val(ipt.attr('data-val-'+changeModule))}else{ipt.val('')}}})},clone_tpl:function(tplId){var tpl=$(tplId).clone();tpl.removeAttr('id');tpl.find('[data-name]').each(function(){$(this).attr('name',$(this).attr('data-name'));$(this).removeAttr('data-name')});return tpl}}