"use strict";function _toConsumableArray(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e0&&void 0!==arguments[0]?arguments[0]:{};this.options=t,this.userName=this.getCookie("UserName"),this.unCollectIds=[],this.needCollectId=0,this.curFolderId=0,this.curCheckId=0,this.checkOptions()&&this.init()}!function(t){var e=document.createElement("link");e.rel="stylesheet",e.type="text/css",e.href=t,document.getElementsByTagName("head")[0].appendChild(e)}("https://g.csdnimg.cn/collection-box/2.1.0/collection-box.css"),window.csdn&&window.csdn.userLogin||function(t,e){var n=document.createElement("script");n.type="text/javascript",n.readyState?n.onreadystatechange=function(){"loaded"!=n.readyState&&"complete"!=n.readyState||(n.onreadystatechange=null,e&&e())}:n.onload=function(){e&&e()},n.src=t,document.getElementsByTagName("head")[0].appendChild(n)}("https://g.csdnimg.cn/user-login/3.0.1/user-login.js"),t.prototype.init=function(){this.render().bindEvent().getList(this.renderList),this.data={url:this.options.url,source:this.options.source,sourceId:Number(this.options.source_id,10),author:this.options.author,title:this.options.title,description:this.options.description||"",fromType:"PC",username:this.userName}},t.prototype.sortListByTime=function(t){var e=this;t=t.sort(function(t,e){return+new Date(e.UpdatedAt)-+new Date(t.UpdatedAt)});var n=this.getNearItemFolderId(),o=e.findNearCollectionItem(n,t),i=t.filter(function(t){return t.ID!==o.ID});o&&i.unshift(o);var s=i.find(function(t){return"默认收藏夹"===t.Name}),c=i.filter(function(t){return"默认收藏夹"!==t.Name}),l=s?[].concat(s,[].concat(_toConsumableArray(c))):[].concat([].concat(_toConsumableArray(c)));return void 0,l},t.prototype.filterCollectionItems=function(t){var e=0,n=this;return t=t.map(function(t){return e=t.CheckStatus&&0===e?t.CheckStatus:e,t.CheckStatus!==e&&0!==t.CheckStatus&&(n.unCollectIds.push(t.CheckStatus),t.CheckStatus=0),t}),void 0,n.deleteCollectionItems(),t},t.prototype.findNearCollectionItem=function(t,e){var n="";return e.forEach(function(e){n=e.ID===t?e:n}),n},t.prototype.checkOptions=function(){var t=!0,e=this.options;return e?"url"in e&&""!==e.url?"source"in e&&""!==e.source?"source_id"in e&&""!==e.source_id?"author"in e&&""!==e.author?"title"in e&&""!==e.title||(void 0,t=!1):(void 0,t=!1):(void 0,t=!1):(void 0,t=!1):(void 0,t=!1):(void 0,t=!1),t},t.prototype.render=function(){var t=this.getThemeClass(),e=$('
\n
\n
添加收藏夹x
\n
\n
\n \n 新建文件夹\n
\n \n \n
\n
\n

\n
    \n
    \n
    \n
    ');return this.box=e,this.list=this.box.find(".csdn-collection-items"),this.toggleBtn=this.box.find(".csdn-collection-btn"),this.inputBox=this.box.find(".csdn-collection-input"),this.input=this.box.find(".csdn-collection-input input"),this.addBtn=this.box.find(".csdn-collection-input button"),this.submitBtn=this.box.find(".csdn-collection-submit"),this.closeBtn=this.box.find(".csdn-collection-close"),this.tip=this.box.find(".csdn-collection-tip"),this.msg=this.box.find(".csdn-collection-msg"),$("body").append(e),this},t.prototype.bindEvent=function(){var t=this;return this.list.on("click","li",function(e){t.clickItemHandler.call(t,$(this))}),this.toggleBtn.on("click",function(e){t.needCollectId=0,t.clickToggleBtnHandler.call(t,e)}),this.input.on("keyup",function(e){t.keyupInputHandler.call(t,e)}).on("focus",function(e){!t.getTip()&&t.setTip()&&t.tip.show()}),t.box.on("click",function(e){if(!t.inputBox.is(":hidden")){$(e.target).closest(".csdn-collection-input").length||t.hideInputBox().hideMsg()&&t.tip.hide()}return $(e.target).closest(".csdn-collection-container").length||t.clickCloseBtnHandler(),e.stopPropagation(),!1}),this.addBtn.on("click",function(e){window.csdn.userLogin.loadAjax(t.createCollectionItems.bind(t,e))}),this.closeBtn.on("click",t.clickCloseBtnHandler.bind(t)),this.submitBtn.on("click",t.clickSubmitBtnHandler.bind(t)),this},t.prototype.getThemeClass=function(){var t="csdn-collection-theme-default";return("black"===this.options.theme||window.csdn&&window.csdn.toolbarIsBlack)&&(t="csdn-collection-theme-dark"),t},t.prototype.trim=function(t){return t.replace(/^\s*/g,"").replace(/\s*$/g,"")},t.prototype.getList=function(t){var e=this,n=e.options.url;return $.ajax({type:"get",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/folderListWithCheck?url="+n,crossDomain:!0,xhrFields:{withCredentials:!0},success:function(n){if(200===n.code){var o=e.sortListByTime(n.data.result);t&&t.call(e,o||[])}else e.showMsg(reponse.msg)},error:function(t){function e(e){return t.apply(this,arguments)}return e.toString=function(){return t.toString()},e}(function(t){if(t&&t.responseText){var n=JSON.parse(t.responseText).message;n&&e.showMsg(n)}void 0})}),this},t.prototype.renderList=function(t){function e(t){return t.replace(//g,">")}var n=this.getNearItemFolderId(),o=""+t.map(function(t){return'
  • "+(n===t.ID?'':"")+""+e(t.Name)+""+(t.IsPrivate?'':"")+''+(t.CheckStatus?"已收藏":"收藏")+"
  • "}).join("");return this.list.html(o),this},t.prototype.setDafaultItem=function(){var t=this.list.find(".select");0===t.length&&this.list.find("li").first().click()||(this.curFolderId=1*t.attr("data-folder-id"),this.curCheckId=1*t.attr("data-check-id")),void 0},t.prototype.renderItem=function(t){var e='
  • ':"")+''+(t.CheckStatus?"已收藏":"收藏")+"
  • ",n=this.list.find("li:first");return $(e).insertAfter(n),this.list.scrollTop(0),this},t.prototype.getNearItemFolderId=function(){return+localStorage.getItem("csdn_collection_"+this.userName)},t.prototype.setNearItemFolderId=function(t){return localStorage.setItem("csdn_collection_"+this.userName,t),this},t.prototype.setTip=function(){return localStorage.setItem("csdn_collection_tip",(new Date).getTime()),this},t.prototype.getTip=function(){return localStorage.getItem("csdn_collection_tip")},t.prototype.getCookie=function(t){var e={};return document.cookie.replace(/([^=;\s]+)=([^=;\s]+)/g,function(){for(var t=arguments.length,n=Array(t),o=0;o=2),""===t?n="名称不能为空":t&&t.length>20?n="名称不能超过20个字":t&&t.length<2&&(n="名称在2到20个字之间"),void 0,n&&this.showMsg(n)||this.hideMsg(),e},t.prototype.checkCollectionStatus=function(t){var e=this,n=this.options.url;$.ajax({type:"get",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/checkFavoriteByUrl?url="+n,crossDomain:!0,xhrFields:{withCredentials:!0},success:function(n){if(200===n.code){var o=!1,i=n.data;for(var s in i)i.hasOwnProperty(s)&&i[s]>0&&(o=!0);e.options.collectionCallBack&&e.options.collectionCallBack(o),t&&t()}else e.showMsg(n.msg)},error:function(t){void 0}})},t.prototype.clickItemHandler=function(t){var e=this,n=1*t.attr("data-folder-id"),o=1*(t.attr("data-check-id")||0);e.needCollectId=n,e.unCollectIds=0===o?[]:[o],void 0,window.csdn.userLogin.loadAjax(function(){e.unCollectIds.length>0&&e.deleteCollectionItems(),!e.unCollectIds.length>0&&e.needCollectId&&e.createCollectionItems()})},t.prototype.clickToggleBtnHandler=function(t){var e=this;t.stopPropagation(),e.showInputBox()},t.prototype.clickAddBtnHandler=function(t){var e=this,n=e.trim(e.input.val()),o={name:n,source:e.options.source,description:"",isPrivate:0,username:e.userName};e.checkName(n)&&$.ajax({type:"post",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/createFolder",data:JSON.stringify(o),dataType:"json",contentType:"application/json",crossDomain:!0,xhrFields:{withCredentials:!0},success:function(t){200===t.code?e.renderItem(t.data).hideInputBox().hideMsg():e.showMsg(t.msg)},error:function(t){if(t&&t.responseText){var n=JSON.parse(t.responseText).message;n&&e.showMsg(n)}void 0}})},t.prototype.keyupInputHandler=function(t){var e=t.keyCode;27===e&&this.hideInputBox().hideMsg(),13===e&&this.addBtn.click()},t.prototype.clickCloseBtnHandler=function(){this.box.remove()},t.prototype.clickSubmitBtnHandler=function(){var t=this;window.csdn.userLogin.loadAjax(function(){void 0,t.unCollectIds.length>0&&t.deleteCollectionItems(),t.needCollectId&&t.createCollectionItems()}),t.clickCloseBtnHandler()},t.prototype.createCollectionItems=function(){var t=this,e=t.trim(t.input.val()),n=Object.assign({},this.data,{folderId:t.needCollectId,newFolderName:e});if(0!==t.needCollectId||t.checkName(e))return $.ajax({type:"post",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/addFavorite",contentType:"application/json",data:JSON.stringify(n),crossDomain:!0,xhrFields:{withCredentials:!0},success:function(e){if(200===e.code){var n=t.needCollectId?t.needCollectId:e.data.folderId;void 0,t.clickCloseBtnHandler(),t.setNearItemFolderId(n).checkCollectionStatus()}else t.showMsg(e.msg)},error:function(e){if(e&&e.responseText){var n=JSON.parse(e.responseText).message;n&&t.showMsg(n)}void 0}}),this},t.prototype.deleteCollectionItems=function(){var t=this,e=t.unCollectIds,n={source:t.options.source,ids:e,username:t.userName,fromType:"PC"};if(e.length)return $.ajax({type:"post",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/cancelFavorite",data:JSON.stringify(n),contentType:"application/json",crossDomain:!0,xhrFields:{withCredentials:!0},success:function(n){200===n.code?(t.unCollectIds=[],void 0,t.checkCollectionStatus(),t.clickCloseBtnHandler()):t.showMsg(n.msg)},error:function(e){if(e&&e.responseText){var n=JSON.parse(e.responseText).message;n&&t.showMsg(n)}void 0}}),this},window.csdn=window.csdn||{},window.csdn.collectionBox=window.csdn.collectionBox||{},window.csdn.collectionBox.show=function(e){window.csdn.userLogin.loadAjax(function(){window.csdn.collectionBox.box=new t(e)})},window.csdn.collectionBox.close=function(){window.csdn.collectionBox.box&&window.csdn.collectionBox.box.clickCloseBtnHandler()},$(document).on("click","[data-bind-collection=true]",function(t){try{var e=window.csdn.collectionBox.params;e&&window.csdn.collectionBox.show(e)}catch(t){void 0}})}();