This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpvg_functions.min.js
34 lines (34 loc) · 17.1 KB
/
cpvg_functions.min.js
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
var volatile_data=function(a){var a=ko.toJS(a),b;for(b in a)this[b]=ko.observable(a[b]);this.setMutipleData=function(a){for(var b in a)this[b](a[b])};this.setMutipleObservableData=function(a){for(var b in a)if("undefined"!=typeof a[b]())this[b](a[b]())};this.setData=function(a,b,d){if("json"==d)this[a]=ko.toJS(b);else if("array-append"==d){this[a]||(this[a]=ko.observableArray([]));for(var f in b)this[a].push(b[f])}else this[a]||(this[a]=ko.observable([])),this[a](b)}},static_data=function(a){for(var b in a)this[b]=
a[b];this.setData=function(a,b){this[a]=b};this.setMutiple=function(a){for(var b in a)this[b]=a[b]}},fieldtype_option=function(a,b){this.name=ko.observable(a);this.label=ko.observable(b);this.type=ko.observable("cpvg_text");this.hide_empty=ko.observable(!1);this.extra_options={checkboxes:ko.observableArray([])};this.temp_data={};this.type.subscribe(function(){for(var a in this)"options"==a.slice(0,7)&&delete this[a]}.bind(this));this.removeOptionCtp=function(){viewModel.current_fieldtype_options.remove(this)};
this.getOutputVar=function(a){"undefined"==typeof this["options"+(a+1)]&&(this["options"+(a+1)]=ko.observable());return this["options"+(a+1)]};this.moveOptionCtpUp=function(){var a=viewModel.current_fieldtype_options.indexOf(this);0!=a&&(a-=1);viewModel.current_fieldtype_options.remove(this);viewModel.current_fieldtype_options.splice(a,0,this)};this.moveOptionCtpDown=function(){var a=viewModel.current_fieldtype_options.indexOf(this);0!=viewModel.current_fieldtype_options().length&&(a+=1);viewModel.current_fieldtype_options.remove(this);
viewModel.current_fieldtype_options.splice(a,0,this)}},basic_param=function(a){this.section=ko.observable();this.name=ko.observable();this.parameter=ko.observable();this.parameter.subscribe(function(){"undefined"!=typeof viewModel.getParamData("choices")?this.select_input_value(!1):this.select_input_value(!0)}.bind(this));this.select_value=ko.observableArray();this.input_value=ko.observable("");this.select_input_value=ko.observable(!1);this.select_input_value.subscribe(function(a){a?jQuery(".cvpg-multi-select").attr("disabled",
"disabled"):(jQuery(".cvpg-multi-select").removeAttr("disabled"),this.input_value(""))}.bind(this));this.value=ko.dependentObservable({read:function(){if(!0==this.select_input_value()||"true"==this.select_input_value())return this.input_value();void 0==this.select_value()[0]&&this.select_value().shift();return this.select_value()},write:function(){},owner:this});this.operator=ko.observable();this.type=ko.observable("CHAR");for(var b in a)if("false"==a[b])this[b](!1);else if("true"==a[b])this[b](!0);
else this[b](a[b])},viewModel={view_type:"post",siteurl:"",touchscreen:ko.observable(!1),available_fieldtypes:{types:[],options:{}},available_template_files:[],available_post_types:[],available_all_fields:[],available_fields:{},available_custom_fields:{},available_extra_data:{},selected_post_type:ko.observable(),selected_field_section:ko.observable(),selected_post_template:ko.observable(),current_fieldtype_options:ko.observableArray([]),temp_data:{},acf_enabled:!0,available_listviews_names:ko.observableArray([]),
selected_list:{id:ko.observable(""),name:ko.observable(""),original_name:"",template:ko.observable(),index:ko.observable(-1)},available_param_names:[{value:"filter",text:"Filters"},{value:"order",text:"Order"},{value:"pagination",text:"Pagination"},{value:"usersorting",text:"User Sorting"}],available_param_data:{filter:"",order:"",pagination:"",usersorting:""},selected_param:{type:ko.observable("filter"),index:ko.observable(0),item:"",visible_form:ko.observable(!1)},current_params:{order:ko.observableArray([]),
filter:ko.observableArray([]),pagination:ko.observableArray([]),usersorting:ko.observableArray([])},post_page_name:ko.observable("Insert name for page/post"),setData:function(a,b,c){if("undefined"==typeof this[a])switch(c){case "array":case "assocArray":case "arrayObservables":this[a]=ko.observableArray([]);break;case "observable":this[a]=ko.observable();break;case "observableArray":this[a](b)}switch(c){case "observable":case "observableArray":this[a](b);break;case "assocArray":this[a]=convert_assoc_array(b);
break;case "arrayObservables":for(var e in b)this[a].push({value:ko.observable(b[e])});break;default:this[a]=b}if("available_extra_data"==a)for(var d in this.available_extra_data){if("undefined"!=typeof this.available_extra_data[d].form_fields)for(var f in this.available_extra_data[d].form_fields)"undefined"!=typeof this.available_extra_data[d].form_fields[f].options&&(this.available_extra_data[d].form_fields[f].options=convert_assoc_array(this.available_extra_data[d].form_fields[f].options))}else if("available_fields"==
a){var a=this.getAvailableSections(),g;for(g in a)if("misc"!=a[g])for(var i in this.available_fields[a[g]])this.available_all_fields[this.available_all_fields.length]={key:a[g]+"."+i,value:a[g].toUpperCase()+": "+this.available_fields[a[g]][i]};for(g in a)for(i in this.available_custom_fields[a[g]])this.available_all_fields[this.available_all_fields.length]={key:a[g]+"."+this.available_custom_fields[a[g]][i],value:a[g].toUpperCase()+": "+this.available_custom_fields[a[g]][i]}}},setAvailableFieldTypes:function(a){for(var b in a)if(this.available_fieldtypes.types[this.available_fieldtypes.types.length]=
{key:b,value:a[b].label},a[b].options){this.available_fieldtypes[b]=[];for(var c in a[b].options){this.available_fieldtypes[b][c]=[];for(var e in a[b].options[c])this.available_fieldtypes[b][c][this.available_fieldtypes[b][c].length]={key:e,value:a[b].options[c][e]}}}},getAvailableSections:function(){return"undefined"==typeof this.selected_post_type()?this.available_fields.field_sections:-1<jQuery.inArray(this.selected_post_type(),this.available_fields.field_sections)?"true"==this.acf_enabled?jQuery.merge(jQuery.merge([],
this.available_fields.field_sections),["acf"]):this.available_fields.field_sections:jQuery.merge(jQuery.merge([],this.available_fields.field_sections),[this.selected_post_type()])},addFieldtypeOption:function(a,b,c){viewModel.getDatafieldExtraData(a,"label")||(b=void 0);b="undefined"==typeof c?new fieldtype_option(a,b):c;if("undefined"!=typeof viewModel.available_extra_data[a]&&"undefined"!=typeof viewModel.available_extra_data[a].form_fields)for(var e in viewModel.available_extra_data[a].form_fields)"undefined"==
typeof c&&(b.extra_options[e]=new ko.observable),"undefined"!=typeof viewModel.available_extra_data[a].form_fields[e].append_field_type&&(b.temp_data[e]=new ko.observable,viewModel.temp_data[e]=new fieldtype_option("section.fieldname","label"));this.current_fieldtype_options.push(b)},displayFieldtypeOptionWindow:function(a,b,c){section_fieldname_splitted=a.split(".");viewModel.temp_data[b].name(a);viewModel.temp_data[b].label(section_fieldname_splitted[1]);jQuery("#cpvg-fieldtype-modal").dialog({dialogClass:"wp-dialog",
resizable:!0,width:"auto",height:"auto",modal:!0,buttons:{"Append Field":function(){jQuery(this).dialog("close");var e=" [["+a,e=e+(";"+viewModel.temp_data[b].type()),d;for(d in viewModel.temp_data[b])"options"==d.slice(0,7)&&(e+=";"+viewModel.temp_data[b][d]());e+="]]";void 0==c()&&c("");c(c()+e);viewModel.temp_data[b].type("cpvg_text")},Cancel:function(){jQuery(this).dialog("close");viewModel.temp_data[b].type("cpvg_text")}}})},formartFieldtypeOptionName:function(a){var b=a.split(".");return 2==
b.length?(a=b[1],"undefined"!=typeof viewModel.available_fields[b[0]]&&"undefined"!=typeof viewModel.available_fields[b[0]][b[1]]?a=viewModel.available_fields[b[0]][b[1]]:"undefined"!=typeof viewModel.available_custom_fields[b[0]]&&"undefined"!=typeof viewModel.available_custom_fields[b[0]][b[1]]&&(a=viewModel.available_custom_fields[b[0]][b[1]]),b[0].charAt(0).toUpperCase()+b[0].slice(1)+" - "+a):a},sendData:function(a){var b=new static_data({action:a,post_type:this.selected_post_type(),view_type:this.view_type,
post_type:this.selected_post_type()});"post"==this.view_type?b.setData("view_value",this.selected_post_type()):"list"==this.view_type&&b.setMutiple({view_value:this.selected_list.original_name,new_view_value:this.selected_list.name()});if("delete_layout"!=a)if(b.setMutiple({template:this.selected_post_template(),fields:this.current_fieldtype_options()}),"list"==this.view_type){b.setData("param",{});for(var c in this.current_params)b.param[c]=this.current_params[c];b.setData("template",this.selected_list.template())}else"post"==
this.view_type&&b.setData("template",this.selected_post_template());b=this.removeUnecessarySendData(ko.toJS(b));jQuery.post(this.siteurl+"/wp-admin/admin-ajax.php",b,function(b){if("save_layout"===a||"delete_layout"===a)jQuery(".action-message").html(b).show(1E3).delay(2E3).hide(1E3),"delete_layout"==a&&"undefined"!=typeof this.current_fieldtype_options&&this.current_fieldtype_options.removeAll();"generate_preview"==a&&jQuery("#cpvg-posttype-preview-content").html(b)})},removeUnecessarySendData:function(a){delete a.setData;
delete a.setMutiple;for(var b in a.fields){for(var c in a.fields[b])"function"==typeof a.fields[b][c]&&delete a.fields[b][c],"undefined"!=typeof a.fields[b].temp_data&&delete a.fields[b].temp_data;var e=a.fields[b].name;if("undefined"!=typeof viewModel.available_extra_data[e]&&"undefined"!=typeof viewModel.available_extra_data[e].options){if(!1==viewModel.available_extra_data[e].options)for(var d in a.fields[b])"options"==d.slice(0,7)&&delete a.fields[b][d];!1==viewModel.available_extra_data[e].type&&
delete a.fields[b].type;!1==viewModel.available_extra_data[e].label&&delete a.fields[b].label}}return a},getServerData:function(){var a=!1;"post"==this.view_type?a=this.selected_post_type():"list"==this.view_type&&(a=this.selected_list.name());a&&(a=new static_data({action:"get_"+this.view_type+"_view_data",view_value:a,view_type:this.view_type}),jQuery.post(this.siteurl+"/wp-admin/admin-ajax.php",ko.toJS(a),function(a){0!=a&&viewModel.parseServerData(jQuery.parseJSON(a.slice(0,a.length-1)))}))},
parseServerData:function(a){var b;this.current_fieldtype_options.removeAll();for(b in this.current_params)this.current_params[b].removeAll();if("list"==this.view_type&&"undefined"!=typeof a.post_type&&(this.selected_post_type(a.post_type),this.selected_list.template(a.template_file),"undefined"!=typeof a.param)){var c="";for(b in a.param)for(var e in a.param[b])"filter"==b?c=new basic_param(a.param[b][e]):"order"==b?c=new basic_param(a.param[b][e]):"pagination"==b?c=new basic_param(a.param[b][e]):
"usersorting"==b&&(c=new basic_param(a.param[b][e])),this.current_params[b].push(c)}"post"==this.view_type&&this.selected_post_template(a.template_file);for(var d in a.fields){"content-editor"==a.fields[d].type&&(a.fields[d].type="cpvg_text");b=new fieldtype_option(a.fields[d].name,a.fields[d].label);b.type(a.fields[d].type);"true"==a.fields[d].hide_empty&&b.hide_empty(!0);for(var f in a.fields[d])if("options"==f.slice(0,7))b[f]=ko.observable(a.fields[d][f]);else if("extra_options"==f)for(var g in a.fields[d][f])b[f][g]=
new ko.observable(a.fields[d][f][g]);this.addFieldtypeOption(a.fields[d].name,a.fields[d].label,b)}},getDatafieldExtraData:function(a,b){return"undefined"!=typeof viewModel.available_extra_data[a]?viewModel.available_extra_data[a][b]:"label"==b||"type"==b||"options"==b||"hide_empty"==b?!0:!1},listViewAction:function(a,b,c,e){switch(a){case "add":e={value:ko.observable("list"+(new Date).getTime())};this.available_list_views.push(e);this.current_fieldtype_options.removeAll();this.listViewAction("select",
this.available_list_views().length-1,e.value(),!0);this.listViewAction("save");break;case "select":this.selected_list.name(c);this.selected_list.original_name=c;this.selected_list.index(b);this.selected_param.type("filter");this.selected_param.index(0);this.selected_param.item="";this.selected_param.visible_form(!1);"undefined"==typeof e&&this.getServerData();break;case "delete":this.listViewAction("select",b,this.available_list_views()[b].value());this.sendData("delete_layout");this.selected_list.name(void 0);
this.selected_list.template(void 0);this.selected_post_type(void 0);this.available_list_views.remove(this.available_list_views()[b]);this.selected_list.name("");this.selected_list.original_name="";this.selected_list.index(-1);this.current_fieldtype_options.removeAll();break;case "save":this.sendData("save_layout"),this.selected_list.original_name=this.selected_list.name(),-1<this.selected_list.index()&&this.available_list_views()[this.selected_list.index()].value(this.selected_list.name())}},parseParamConfig:function(a,
b){var c=ko.toJS(b),e={sections:[],fields:[],choices:[],mutiple_choices:[],messages:{},operators:{},types:{}},d;for(d in c){e.sections[e.sections.length]=d;for(var f in c[d])if("message"==f)e.messages[d]=c[d][f];else if("mutiple_choices"==f)e.mutiple_choices=jQuery.merge(e.mutiple_choices,c[d][f]);else if("operator"==f||"type"==f){var g=c[d][f];e[f+"s"][d]=[];for(var i in g)e[f+"s"][d][e[f+"s"][d].length]={key:i,value:g[i]}}else for(var h in c[d][f])if("object"==typeof c[d][f][h]){"undefined"==typeof e.choices[d]&&
(e.choices[d]={});"undefined"==typeof e.choices[d][h]&&(e.choices[d][h]=[]);for(var j in c[d][f][h])e.choices[d][h][e.choices[d][h].length]={key:j,value:c[d][f][h][j]}}else"undefined"==typeof e.fields[d]&&(e.fields[d]=[]),e.fields[d][e.fields[d].length]={key:h,value:c[d][f][h]}}this.available_param_data[a]=e},getParamData:function(a,b){var c=this.selected_param.type(),e=this.selected_param.index();switch(a){case "current_records":return this.current_params[c];case "sections":if("undefined"!=typeof this.available_param_data[c].sections)return this.available_param_data[c].sections;
break;case "fields":var d=this.getParamData("section");if("undefined"!=typeof this.available_param_data[c].fields&&"undefined"!=typeof d&&"undefined"!=typeof this.available_param_data[c].fields[d()])return this.available_param_data[c][a][d()];break;case "types":case "operators":case "messages":return d=this.getParamData("section"),"undefined"!=typeof this.available_param_data[c][a]&&"undefined"!=typeof d&&typeof this.available_param_data[c][a][d()]?this.available_param_data[c][a][d()]:[];case "choices":var d=
this.getParamData("section"),f=this.getParamData("parameter");if("undefined"!=typeof this.available_param_data[c].choices&&"undefined"!=typeof d&&"undefined"!=typeof f&&"undefined"!=typeof this.available_param_data[c].choices[d()])return"undefined"!=typeof this.current_params[c]()[e]&&(-1<jQuery.inArray(f(),this.available_param_data[c].mutiple_choices)?jQuery(".cvpg-multi-select").attr("multiple",""):jQuery(".cvpg-multi-select").removeAttr("multiple")),this.available_param_data[c].choices[d()][f()];
break;default:if("undefined"!=typeof this.current_params[c]()[e])return b?this.current_params[c]()[e][a]():this.current_params[c]()[e][a]}},runParamAction:function(a,b){if("select"==a)this.selected_param.index(this.current_params[this.selected_param.type()].indexOf(b)),this.selected_param.item=b,this.selected_param.visible_form(!0),this.getParamData("choices");else if("remove"==a)this.selected_param.index(void 0),this.current_params[this.selected_param.type()].remove(b),this.selected_param.visible_form(!1);
else{var c="";if("filter"==this.selected_param.type()||"order"==this.selected_param.type()||"pagination"==this.selected_param.type()||"usersorting"==this.selected_param.type())c=new basic_param({name:this.selected_param.type()+(new Date).getTime()});this.current_params[this.selected_param.type()].push(c);this.runParamAction("select",c)}},createPostPage:function(a){this.sendData("save");a={action:"create_postpage",name:this.post_page_name(),object_type:a,list_view_name:this.selected_list.name()};jQuery.post(this.siteurl+
"/wp-admin/admin-ajax.php",ko.toJS(a),function(a){jQuery(".action-message").html(a.slice(0,a.length-1)).show(1E3).delay(2E3).hide(1E3)})},debugServerData:function(){}};viewModel.selected_post_type.subscribe(function(){this.current_fieldtype_options.removeAll();"post"==this.view_type&&this.getServerData();this.selected_post_template(this.available_template_files[0].key);jQuery("#cpvg-posttype-preview-content").html("")},viewModel);
viewModel.selected_param.type.subscribe(function(){this.selected_param.index(void 0);this.selected_param.visible_form(!1)},viewModel);viewModel.touchscreen.subscribe(function(){this.touchscreen()?(jQuery("#cpvg-fieldlist").droppable("destroy"),jQuery(".cpvg-field-draggable").draggable("destroy").click(function(){viewModel.addFieldtypeOption(jQuery(this).attr("id"),jQuery(this).text())})):(jQuery(".cpvg-field-draggable").unbind("click"),cvpg_set_draggable_droppable())},viewModel);
ko.bindingHandlers.sortableList={init:function(a,b){var c=b();jQuery(a).sortable({update:function(a,b){var f=b.item.tmplItem().data,g=ko.utils.arrayIndexOf(b.item.parent().children(),b.item[0]);0<=g&&(c.remove(f),c.splice(g,0,f))}})}};jQuery(document).ready(function(){ko.applyBindings(viewModel);cvpg_set_draggable_droppable();navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|android)/)?viewModel.touchscreen(!0):viewModel.touchscreen(!1)});
function cvpg_set_draggable_droppable(){jQuery(".cpvg-field-draggable").draggable({appendTo:"body",helper:"clone"});jQuery("#cpvg-fieldlist").droppable({activeClass:"ui-state-default",hoverClass:"ui-state-hover",accept:":not(.ui-sortable-helper)",drop:function(a,b){viewModel.addFieldtypeOption(b.draggable.attr("id"),b.draggable.text())}})}function convert_assoc_array(a){var b=[],c;for(c in a)b[b.length]={key:c,value:a[c]};return b};