1 |
- "use strict";!function(){function t(){var t=arguments.length>0&&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.0.3/collection-box.css"),function(t,e){var o=document.createElement("script");o.type="text/javascript",o.readyState?o.onreadystatechange=function(){"loaded"!=o.readyState&&"complete"!=o.readyState||(o.onreadystatechange=null,e&&e())}:o.onload=function(){e&&e()},o.src=t,document.getElementsByTagName("head")[0].appendChild(o)}("https://g.csdnimg.cn/user-login/2.3.2/user-login.js"),t.prototype.init=function(){this.render().bindEvent().getList(this.renderList),this.data={url:this.options.url,source:this.options.source,source_id:Number(this.options.source_id,10),author:this.options.author,title:this.options.title,description:this.options.description||""}},t.prototype.sortListByTime=function(t){var e=this;t=t.sort(function(t,e){return+new Date(e.UpdatedAt)-+new Date(t.UpdatedAt)});var o=this.getNearItemFolderId(),n=e.findNearCollectionItem(o,t),i=t.filter(function(t){return t.ID!==n.ID});return n&&i.unshift(n),void 0,i},t.prototype.filterCollectionItems=function(t){var e=0,o=this;return t=t.map(function(t){return e=t.CheckStatus&&0===e?t.CheckStatus:e,t.CheckStatus!==e&&0!==t.CheckStatus&&(o.unCollectIds.push(t.CheckStatus),t.CheckStatus=0),t}),void 0,o.deleteCollectionItems(),t},t.prototype.findNearCollectionItem=function(t,e){var o="";return e.forEach(function(e){o=e.ID===t?e:o}),o},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=$('<div id="csdn-collection" class="'+t+'">\n <div class="csdn-collection-container">\n <div class="csdn-collection-header">添加收藏夹<span class="csdn-collection-close">x</span></div>\n <div class="csdn-collection-body">\n <ul class="csdn-collection-items"></ul>\n <div class="csdn-collection-folder">\n <span class="csdn-collection-tip"></span>\n <span class="csdn-collection-btn"><i></i>新建文件夹</span>\n <div class="csdn-collection-input">\n <input type="text" placeholder="最多可输入20个字">\n <button>新建</button>\n </div>\n </div>\n <p class="csdn-collection-msg"></p>\n </div>\n <div class="csdn-collection-footer">\n <button class="csdn-collection-submit">确 定</button>\n </div>\n </div>\n </div>');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.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.clickAddBtnHandler.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,o=e.options.url;return $.ajax({type:"get",url:"https://me.csdn.net/api/favorite/folderListWithCheck?url="+o,crossDomain:!0,xhrFields:{withCredentials:!0},success:function(o){if(200===o.code){var n=e.sortListByTime(o.data.result);t&&t.call(e,e.filterCollectionItems(n))}},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 o=JSON.parse(t.responseText).message;o&&e.showMsg(o)}void 0})}),this},t.prototype.renderList=function(t){var e=this.getNearItemFolderId(),o=""+t.map(function(t){return'<li class="csdn-collection-item '+(t.CheckStatus?"select":"")+'" '+(t.CheckStatus?"data-check-id="+t.CheckStatus:"")+" data-folder-id="+t.ID+">"+(e===t.ID?'<i class="near"></i>':"")+"<span>"+t.Name+"</span>"+(t.IsPrivate?'<i class="lock"></i>':"")+"</li>"}).join("");return this.list.html(o),this.setDafaultItem(),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='<li class="csdn-collection-item '+(t.CheckStatus?"select":"")+'" data-folder-id='+t.ID+"><span>"+t.Name+"</span>"+(t.IsPrivate?'<i class="lock"></i>':"")+"</li>";return this.list.prepend(e),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,o=Array(t),n=0;n<t;n++)o[n]=arguments[n];var i=o[1],s=o[2];e[i]=s}),t?e[t]:e},t.prototype.showInputBox=function(){return this.toggleBtn.hide(),this.inputBox.show(),this.input.focus(),this},t.prototype.hideInputBox=function(){return this.inputBox.hide(),this.input.val(""),this.toggleBtn.show(),this},t.prototype.showMsg=function(t){return t&&this.msg.html(t).show(),this},t.prototype.hideMsg=function(t){return this.msg.hide().html(),this},t.prototype.checkName=function(t){var e=!0,o="";return e=!!(t&&t.length<=20&&t.length>=2),""===t?o="名称不能为空":t&&t.length>20?o="名称不能超过20个字":t&&t.length<2&&(o="名称在2到20个字之间"),void 0,o&&this.showMsg(o)||this.hideMsg(),e},t.prototype.checkCollectionStatus=function(t){var e=this,o=this.options.url;$.ajax({type:"get",url:"https://me.csdn.net/api/favorite/check?url="+o,crossDomain:!0,xhrFields:{withCredentials:!0},success:function(o){if(200===o.code){var n=!1,i=o.data;for(var s in i)i.hasOwnProperty(s)&&i[s]>0&&(n=!0);e.options.collectionCallBack&&e.options.collectionCallBack(n),t&&t()}},error:function(t){void 0}})},t.prototype.clickItemHandler=function(t){var e=this;t.toggleClass("select").siblings().removeClass("select");var o=e.list.find(".select"),n=1*o.attr("data-folder-id"),i=1*(o.attr("data-check-id")||0);o.length?(e.needCollectId=e.curFolderId===n?0:n,e.unCollectIds=e.curCheckId===i?[]:[e.curCheckId]):(e.unCollectIds=0===e.curCheckId?[]:[e.curCheckId],e.needCollectId=0),void 0},t.prototype.clickToggleBtnHandler=function(t){var e=this;t.stopPropagation(),e.showInputBox()},t.prototype.clickAddBtnHandler=function(t){var e=this,o=e.trim(e.input.val()),n={name:o,source:e.options.source,description:"",isPrivate:0};e.checkName(o)&&$.ajax({type:"post",url:"https://me.csdn.net/api/favorite/createFolder",data:JSON.stringify(n),dataType:"json",crossDomain:!0,xhrFields:{withCredentials:!0},success:function(t){200===t.code&&e.renderItem(t.data).hideInputBox().hideMsg()},error:function(t){if(t&&t.responseText){var o=JSON.parse(t.responseText).message;o&&e.showMsg(o)}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=Object.assign({},this.data,{folder_id:t.needCollectId});if(t.needCollectId)return $.ajax({type:"post",url:"https://me.csdn.net/api/favorite/create",data:JSON.stringify(e),crossDomain:!0,xhrFields:{withCredentials:!0},success:function(e){200===e.code&&(void 0,t.setNearItemFolderId(t.needCollectId).checkCollectionStatus())},error:function(e){if(e&&e.responseText){var o=JSON.parse(e.responseText).message;o&&t.showMsg(o)}void 0}}),this},t.prototype.deleteCollectionItems=function(){var t=this,e=t.unCollectIds,o={source:t.options.source,ids:e};if(e.length)return $.ajax({type:"post",url:"https://me.csdn.net/api/favorite/delete",data:JSON.stringify(o),crossDomain:!0,xhrFields:{withCredentials:!0},success:function(o){200===o.code&&(t.unCollectIds=[],void 0,t.checkCollectionStatus())},error:function(e){if(e&&e.responseText){var o=JSON.parse(e.responseText).message;o&&t.showMsg(o)}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}})}();
|