function ShouldRenderHeroButton(renderCtx) {
return true;
}

var CIBResources = {
'ru-ru': {
CalloutText: 'Создайте новый элемент'
},
CalloutText: 'Create a new item'
};

var NewItemCalloutData = function(){
var listId;
var CTList = [];
var _self = this;

function _init(){
var CustomEvent = {};
CustomEvent.OnPreRender = function(ctx){
AddStyles();
};

CustomEvent.OnPostRender = function(ctx){
var wpq = ctx.wpq;

var Hero = document.getElementById('Hero-'+wpq);
if (Hero){
Hero.setAttribute('class','display-none');

_self.listId = ctx.listName;
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
_self.MakeRequest(ctx);
}, 'sp.js');

}

};

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(CustomEvent);
}


function AddStyles(){
var css = '.display-none{display:none;}.display-inlineblock{display:inline-block;}',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');

style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.appendChild(style);
};

this.ResourcedValue = function (value) {
var returnValue = value;
if (CIBResources) {
var resource = CIBResources[_spPageContextInfo.currentCultureName.toLowerCase()];
if (!resource) {
resource = CIBResources;
};
if (resource) {
returnValue = resource[value];
}
};
return returnValue;
};
this.CreateCallout = function(ctx){
if (_self.CTList.length > 0){
var wpq = ctx.wpq;
var Hero = document.getElementById('Hero-'+wpq);
if (Hero){
var LinkButton = document.getElementById(ctx.heroId);
if (LinkButton){
LinkButton.setAttribute('href','javascript:;');
LinkButton.setAttribute('onclick','javascript:;');
Hero.setAttribute('class','display-inlineblock');

EnsureScript('callout.js', typeof CalloutManager, function() {
if (Boolean(LinkButton)) {
var callout = CalloutManager.getFromLaunchPointIfExists(LinkButton);

var content = "";
var foldersContent = "";
for (var i=0;i<_self.CTList.length;i++){
var CT = _self.CTList[i];
var href = ctx.newFormUrl+"&ContentTypeId="+CT.id;
if (CT.id.indexOf("0x0120") == 0){
foldersContent += String.format("<a class=\"ms-newdoc-callout-item ms-displayBlock\" onclick=\"CalloutManager.closeAll(); OpenLink('{2}'); return false;\" href=\"javascript:;\"><img src=\"/_layouts/15/images/mb_folder.png?rev=38\" class=\"ms-verticalAlignMiddle ms-newdoc-callout-img\"><h3 class=\"ms-displayInline ms-newdoc-callout-text ms-verticalAlignMiddle ms-soften\">{1}</h3></a>",
wpq, CT.name, href);
}else{
content += String.format("<a class=\"ms-newdoc-callout-item ms-displayBlock\" onclick=\"CalloutManager.closeAll(); OpenLink('{2}'); return false;\" href=\"javascript:;\"><img src=\"/_layouts/15/images/256_ICGEN.png?rev=38\" height=\"32px\" class=\"ms-verticalAlignMiddle ms-newdoc-callout-img\"><h3 class=\"ms-displayInline ms-newdoc-callout-text ms-verticalAlignMiddle ms-soften\">{1}</h3></a>",
wpq, CT.name, href);
}

};

var resultContent = "<div id=\"js-newdocWOPI-divMain-WPQ2\" class=\"ms-newdoc-callout-main\" style=\"margin-left: 20px; margin-right: 20px;\">";
resultContent += content;
if (content&&foldersContent){
resultContent += "<hr />";
}
resultContent += foldersContent;
resultContent += "</div>";


var calloutOptions = new CalloutOptions();
calloutOptions.ID = 'notificationcallout';
calloutOptions.launchPoint = LinkButton;
calloutOptions.beakOrientation = 'leftRight';
calloutOptions.content = resultContent;
calloutOptions.title = _self.ResourcedValue('CalloutText');
var callout = CalloutManager.createNew(calloutOptions);
}
});

}
}
}
};

this.MakeRequest = function(ctx){
JSRequest.EnsureSetup();
var RootFolderUrl = JSRequest.QueryString["RootFolder"];

var clientContext = new SP.ClientContext();
var web = clientContext.get_web();

var targetList = clientContext.get_web().get_lists().getById(_self.listId);

var contenttypes = targetList.get_contentTypes();

var folder = targetList.get_rootFolder();
var folderItem;
if (RootFolderUrl){
folder = web.getFolderByServerRelativeUrl(RootFolderUrl);
folderItem = folder.get_listItemAllFields();
}else{
folderItem = targetList;
}
clientContext.load(folder);
clientContext.load(folder,'ContentTypeOrder');
clientContext.load(folderItem, 'EffectiveBasePermissions');
clientContext.load(targetList);
clientContext.load(contenttypes);

clientContext.executeQueryAsync(
function (data) {
var returndata = [];
var permission = folderItem.get_effectiveBasePermissions();
if (permission.has(SP.PermissionKind.addListItems)) {
var contents = folder.get_contentTypeOrder();

var ctEnum = contenttypes.getEnumerator();
while (ctEnum.moveNext()) {
var CT = ctEnum.get_current();

if (checkInCTArray(contents,CT.get_stringId())){
var returnCT = {};
returnCT.name = CT.get_name();
returnCT.id = CT.get_stringId();
returndata.push(returnCT);
}

}
}

_self.CTList = returndata;
_self.CreateCallout(ctx);

},
clientContextError
);
};

function clientContextError(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function checkInCTArray(arr, value){
var inArray = false;
for (var i=0;i<arr.length;i++){
if (arr[i].get_stringValue() == value){
inArray = true;
continue;
}
}
return inArray;
}

_init();
};


function OpenLink(href){
window.location.href = href;
}


(function(){
ExecuteOrDelayUntilScriptLoaded(function () {
new NewItemCalloutData();
}, 'clienttemplates.js');
})();

Last edited Dec 3, 2014 at 8:30 AM by SergeiSnitko, version 1