'use strict';
goog.provide('CustomFields.NestedDropDown');
goog.require('Blockly.Field');
var kendostyleSheet = document.createElement('link');
kendostyleSheet.rel='stylesheet';
document.getElementsByTagName('head')[0].appendChild(kendostyleSheet);
var jqueryScript = document.createElement('script');
jqueryScript.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(jqueryScript);
var KendoScript = document.createElement('script');
KendoScript.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(KendoScript);
var jqueryScriptnotused = document.createElement('script');
jqueryScriptnotused.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(jqueryScriptnotused);
var CustomFields = CustomFields || {};
CustomFields.NestedDropDown = function(opt_listinfo) {
var value = {};
var inputjson=["Root",{"text":"Root","items":[{"text":"Section000","items":[{"text":"ID","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Section000","id":"ID"},{"text":"PID","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Section000","id":"PID"},{"text":"ParentID","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Section000","id":"ParentID"},{"text":"AUR_ModifiedBy","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Section000","id":"AUR_ModifiedBy"},{"text":"AUR_ModifiedOn","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Section000","id":"AUR_ModifiedOn"}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section000","ParentId":"Root","id":"Section000","expanded":true},{"text":"CodeControl000","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"CodeControl000","ParentId":"Root","id":"CodeControl000"},{"text":"Section001","items":[{"text":"Section002","items":[{"text":"Tabs000","items":[{"text":"Tab001","items":[{"text":"Section003","items":[{"text":"TextBox004","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Section003","id":"TextBox004"},{"text":"TextBox000","items":[],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Section003","id":"TextBox000"}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Tab001","id":"Section003","expanded":true}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Tabs000","id":"Tab001","expanded":true}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Section002","id":"Tabs000","expanded":true}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Section001","id":"Section002","expanded":true}],"IsDBContainer":false,"IsFormControl":true,"SeniorParentElement":"Section001","ParentId":"Root","id":"Section001","expanded":true}],"IsDBContainer":false,"IsFormControl":false,"SeniorParentElement":null,"id":"Root","expanded":true}];
value.dropdownlist = inputjson[1].items || CustomFields.NestedDropdown.ListInfo;
CustomFields.NestedDropDown.superClass_.constructor.call(this, value);
this.setSpellcheck(false);
this.editorListeners_ = [];
};
Blockly.utils.object.inherits(CustomFields.NestedDropDown, Blockly.FieldTextInput);
CustomFields.NestedDropDown.fromJson = function(options) {
return new CustomFields.NestedDropDown(options['dropdownlist']);
};
CustomFields.NestedDropDown.ListInfo = [];
CustomFields.NestedDropDown.prototype.showEditor_ = function() {
CustomFields.NestedDropDown.superClass_.showEditor_.call(this);
var editor = this.dropdownCreate_();
Blockly.DropDownDiv.getContentDiv().appendChild(editor);
var myDataSource = new kendo.data.HierarchicalDataSource({
data:CustomFields.NestedDropDown.ListInfo
});
$("#multiselect").kendoMultiSelect({
dataTextField: "text",
dataValueField: "id"
});
$("#treeview").kendoTreeView({
loadOnDemand: false,
checkboxes: {
checkChildren: true
},
dataSource: myDataSource,
check: onCheck,
expand: onExpand,
//change: onselect,
});
function getCheckedNodes(nodes, checkedNodes) {
var node;
for (var i = 0; i < nodes.length; i++) {
node = nodes[i];
if (node.checked) {
checkedNodes.push({ text: node.text, id:
node.id ,IsDBContainer:node.IsDBContainer,IsFormControl:node.IsFormControl});
}
if (node.hasChildren) {
getCheckedNodes(node.children.view(), checkedNodes);
}
}
}
function onCheck(e) {
console.log('Onclick of check box');
var checkedNodes = [];
var treeView = $("#treeview").data("kendoTreeView");
getCheckedNodes(treeView.dataSource.view(), checkedNodes);
setMessage(checkedNodes.length);
let tv = $("#treeview").data("kendoTreeView"), // Gets the 2dn treeview reference
text = e.sender.dataItem(e.node).text; // Gets the 1st treeview selected text
let currentlyselectedIsDBContainer= checkedNodes.length==0?false:(checkedNodes[0].IsDBContainer);
let currentlyselectedIsFormControl= checkedNodes.length==0?false:(checkedNodes[0].IsFormControl);
// Iterates over the 2nd treeview items
tv.items().toArray().forEach(item => {
let dataItem = tv.dataItem(item);
let disabled=false; // Get the item related dataItem
if(!currentlyselectedIsDBContainer && currentlyselectedIsFormControl)
{
if((dataItem.IsDBContainer && dataItem.IsFormControl)||(!dataItem.IsDBContainer && !dataItem.IsFormControl))
{
disabled=true;
}
}else if((currentlyselectedIsDBContainer && currentlyselectedIsFormControl)||(!currentlyselectedIsDBContainer && !currentlyselectedIsFormControl)){
if(!dataItem.IsDBContainer && dataItem.IsFormControl){
disabled=true;
}
}
// Gets the current item's checkbox
$(item).find('input[type="checkbox"]')
// .prop('checked', false) // Uncheck it by default
.prop('disabled', disabled); // Disable/enable based on above condition
if(checkedNodes.length==0){
$(item).find('input[type="checkbox"]').prop('checked', false).prop('disabled', false);
}
});
}
function onExpand(e) {
if ($("#filterText").val() == "") {
$(e.node).find("li").show();
}
}
function setMessage(checkedNodes) {
var message;
if (checkedNodes > 0) {
message = checkedNodes + " categories selected";
}
else {
message = "0 categories selected";
}
$("#result").html(message);
var note = CustomFields.NestedDropDown.prototype.getSelectedCheckBoxValues();
CustomFields.NestedDropDown.prototype.doClassValidation_(message);
}
$("#filterText").keyup(function (e) {
var filterText = $(this).val();
if (filterText !== "") {
$(".selectAll").css("visibility", "hidden");
$("#treeview .k-group .k-group .k-in").closest("li").hide();
$("#treeview .k-group").closest("li").hide();
$("#treeview .k-in:contains(" + filterText + ")").each(function () {
$(this).parents("ul, li").each(function () {
var treeView = $("#treeview").data("kendoTreeView");
treeView.expand($(this).parents("li"));
$(this).show();
});
});
$("#treeview .k-group .k-in:contains(" + filterText + ")").each(function () {
$(this).parents("ul, li").each(function () {
$(this).show();
});
});
}
else {
$("#treeview .k-group").find("li").show();
var nodes = $("#treeview > .k-group > li");
$.each(nodes, function (i, val) {
if (nodes[i].getAttribute("data-expanded") == null) {
$(nodes[i]).find("li").hide();
}
});
$(".selectAll").css("visibility", "visible");
}
});
function checkUncheckAllNodes(nodes, checked) {
for (var i = 0; i < nodes.length; i++) {
nodes[i].set("checked", checked);
if (nodes[i].hasChildren) {
checkUncheckAllNodes(nodes[i].children.view(), checked);
}
}
}
// function chbAllOnChange() {
// var treeView = $("#treeview").data("kendoTreeView");
// var isAllChecked = $('#chbAll').prop("checked");
// checkUncheckAllNodes(treeView.dataSource.view(), isAllChecked)
// if (isAllChecked) {
// setMessage($('#treeview input[type="checkbox"]').length);
// }
// else {
// setMessage(0);
// }
// }
$("#chbAll").click(function(e){
var treeView = $("#treeview").data("kendoTreeView");
var isAllChecked = $('#chbAll').prop("checked");
checkUncheckAllNodes(treeView.dataSource.view(), isAllChecked)
if (isAllChecked) {
setMessage($('#treeview input[type="checkbox"]').length);
}
else {
setMessage(0);
}
});
Blockly.DropDownDiv.setColour(this.sourceBlock_.style.colourPrimary,
this.sourceBlock_.style.colourTertiary);
Blockly.DropDownDiv.showPositionedByField(
this, this.dropdownDispose_.bind(this));
};
CustomFields.NestedDropDown.prototype.dropdownCreate_ = function() {
console.log($().jquery);
// console.log($j().jquery);
var CreateDiv = function(id,classname){
var divElement= document.createElement('div');
if(id!='')
divElement.id=id;
if(classname!='')
divElement.className=classname;
return divElement;
}
var CreateInputTag=function (id,type,placeholder,classname) {
var InputElement=document.createElement('input');
if(id!='')
InputElement.id=id;
if(type!='')
InputElement.type=type;
if(placeholder!='')
InputElement.placeholder=placeholder;
if(classname!='')
InputElement.className=classname;
return InputElement;
}
var CreateLabel=function (classname) {
var LabelElement=document.createElement('label');
if(classname!='')
LabelElement.className=classname;
LabelElement.textContent='Select All';
return LabelElement;
}
var CreateSpanElement=function (id) {
var SpanElement=document.createElement('span');
SpanElement.id=id;
SpanElement.textContent='0 categories selected';
return SpanElement;
}
var CreateSelectElement= function (id) {
var SelectElement=document.createElement('select');
if(id!='')
SelectElement.id=id;
return SelectElement;
}
var examplediv=CreateDiv('example','');
var demosectiondiv=CreateDiv('','demo-section k-content');
var dialogdiv=CreateDiv('dialog','');
var dialogcontentdiv=CreateDiv('','dialogContent');
var filterTextinput=CreateInputTag('filterText','text','Search categories','','');
var selectalldiv=CreateDiv('','selectAll');
var checkboxinput=CreateInputTag('chbAll','checkbox','','k-checkbox');
var LabelTag=CreateLabel('k-checkbox-label');
var spanTag=CreateSpanElement('result');
var multiSelectTag=CreateSelectElement('multiselect');
var treeViewDiv=CreateDiv('treeview','');
selectalldiv.appendChild(checkboxinput);
selectalldiv.appendChild(LabelTag);
//selectalldiv.appendChild(spanTag);
dialogcontentdiv.appendChild(filterTextinput);
//dialogcontentdiv.appendChild(selectalldiv);
dialogcontentdiv.appendChild(CreateDiv()).appendChild(spanTag);
dialogcontentdiv.appendChild(treeViewDiv);
dialogdiv.appendChild(dialogcontentdiv);
demosectiondiv.appendChild(dialogdiv);
demosectiondiv.appendChild(multiSelectTag);
examplediv.appendChild(demosectiondiv);
// var Kendojs=document.createElement('script');
// Kendojs.src="C:/Tasks/Blockly/blockly-samples-master/blockly-samples-master/examples/pitch-field-demo - Copy - Library/KendoTreeView.js"
// Kendojs.type = 'text/javascript';
// document.getElementsByTagName('body')[0].appendChild(Kendojs);
return examplediv;
};
CustomFields.NestedDropDown.prototype.initView = function() {
if (this.shouldAddBorderRect_()) {
this.createBorderRect_();
} else {
this.clickTarget_ = this.sourceBlock_.getSvgRoot();
}
this.createTextElement_();
this.imageElement_ = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.IMAGE, {}, this.fieldGroup_);
if (this.getConstants().FIELD_DROPDOWN_SVG_ARROW) {
this.createSVGArrow_();
} else {
this.createTextArrow_();
}
if (this.borderRect_) {
Blockly.utils.dom.addClass(this.borderRect_, 'blocklyDropdownRect');
}
};
CustomFields.NestedDropDown.prototype.shouldAddBorderRect_ = function() {
return !this.getConstants().FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW ||
(this.getConstants().FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW &&
!this.sourceBlock_.isShadow());
};
CustomFields.NestedDropDown.prototype.createTextArrow_ = function() {
this.arrow_ = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.TSPAN, {}, this.textElement_);
this.arrow_.appendChild(document.createTextNode(
this.sourceBlock_.RTL ?
Blockly.FieldDropdown.ARROW_CHAR + ' ' :
' ' + Blockly.FieldDropdown.ARROW_CHAR));
if (this.sourceBlock_.RTL) {
this.textElement_.insertBefore(this.arrow_, this.textContent_);
} else {
this.textElement_.appendChild(this.arrow_);
}
};
CustomFields.NestedDropDown.prototype.dropdownDispose_ = function() {
for (var i = this.editorListeners_.length, listener;
listener = this.editorListeners_[i]; i--) {
Blockly.unbindEvent_(listener);
this.editorListeners_.pop();
}
};
CustomFields.NestedDropDown.prototype.hide_ = function(event) {
Blockly.DropDownDiv.hideWithoutAnimation();
this.setEditorValue_(event.currentTarget.outerText);
};
// CustomFields.NestedDropDown.prototype.valueToNote = function(value) {
// var array= CustomFields.NestedDropDown.ListInfo;
// for (let index = 0; index < array.length; index++) {
// if(this.searchTree(array[index],value)){
// return true;
// }
// }
// };
// CustomFields.NestedDropDown.prototype.noteToValue = function(text) {
// var array= CustomFields.NestedDropDown.ListInfo;
// for (let index = 0; index < array.length; index++) {
// if(this.searchTree(array[index],text)){
// return text;
// }
// }
// };
// CustomFields.NestedDropDown.prototype.searchTree=function (element, matchingTitle) {
// if(element.id.toLowerCase() == matchingTitle.toLowerCase()){
// return true;
// }else if (element.children != null){
// var i;
// var result = null;
// for(i=0; result == null && i < element.children.length; i++){
// result = this.searchTree(element.children[i], matchingTitle);
// }
// return result;
// }
// return false;
// }
CustomFields.NestedDropDown.prototype.getText_ = function() {
if (this.isBeingEdited_) {
return CustomFields.NestedDropDown.superClass_.getText_.call(this);
}
return null;
};
CustomFields.NestedDropDown.prototype.getEditorText_ = function(value) {
return value;
};
CustomFields.NestedDropDown.prototype.getValueFromEditorText_ = function(text) {
return this.noteToValue(text);
};
CustomFields.NestedDropDown.prototype.render_ = function() {
CustomFields.NestedDropDown.superClass_.render_.call(this);
};
CustomFields.NestedDropDown.prototype.doClassValidation_ = function(newValue) {
if (newValue == undefined) {
var note = this.getSelectedCheckBoxValues();
if (note.length>0) {
return note;
}else{
return null;
}
}
else if(newValue.dropdownlist!=undefined){
CustomFields.NestedDropDown.ListInfo= newValue.dropdownlist ;
}else{
return newValue
}
if (newValue.dropdownlist !=undefined) {
return newValue.dropdownlist[0].id;
}
return null;
};
CustomFields.NestedDropDown.prototype.getSelectedCheckBoxValues=function () {
var array = []
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')
for (var i = 0; i < checkboxes.length; i++) {
let item={};
item.children=[];
if(checkboxes[i].parentElement.previousElementSibling==null){
array.push(checkboxes[i].parentElement.nextElementSibling.textContent)
}
}
var responseArray=[];
let tv = $("#treeview").data("kendoTreeView")
tv.items().toArray().forEach(item => {
let dataItem = tv.dataItem(item);
if($(item).find('input[type="checkbox"]:checked').length==1){
var item={};
item.ParentId=dataItem.SeniorParentElement;
item.Id=dataItem.text;
if(item.Id!=item.ParentId){
responseArray.push(item);
}
}
});
var format=[];
var item={};
item.children=[];
FormatInput(responseArray)
function FormatInput(jsoninputarray) {
for (let index = 0; index < jsoninputarray.length; index++) {
if(format.some(code => code.ParentId === jsoninputarray[index].ParentId)){
item.children.push(jsoninputarray[index].Id);
}else{
if(array.includes(jsoninputarray[index].Id)){
var item={};
item.children=[];
item.ParentId=jsoninputarray[index].ParentId;
format.push(item);
}
}
}
}
console.log(JSON.stringify(format));
}
Blockly.fieldRegistry.register('nested_dropdown', CustomFields.NestedDropDown);