| 1 |
- "use strict";function _toConsumableArray(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}!function(){function t(t){var e={};return document.cookie.replace(/([^=;\s]+)=([^=;\s]+)/g,function(){for(var t=arguments.length,n=Array(t),o=0;o<t;o++)n[o]=arguments[o];var i=n[1],s=n[2];e[i]=s}),t?e[t]:e}function e(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,this.userName=t("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.1/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"),e.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}},e.prototype.sortListByTime=function(t){var e=this;if(!t.length)return t;var n=t.length?t[0]:"";t=t.slice(1);var o=this.getNearItemFolderId(),i=e.findNearCollectionItem(o,t),s=t.filter(function(t){return t.ID!==i.ID}),c=s.filter(function(t){return t.CheckStatus}),l=s.filter(function(t){return!t.CheckStatus});return[n,i].concat(_toConsumableArray(c),_toConsumableArray(l)).filter(function(t){return t})},e.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},e.prototype.findNearCollectionItem=function(t,e){var n="";return e.forEach(function(e){n=e.ID===t?e:n}),n},e.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},e.prototype.render=function(){var t=this.renderCreateFolder(),e=this.getThemeClass(),n=$('<div id="csdn-collection" class="'+e+'">\n <div class="csdn-collection-mask"></div>\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 <div class="csdn-collection-folder">\n <span class="csdn-collection-btn"><i></i>创建收藏夹</span>\n <div class="csdn-collection-input">'+t+'</div>\n </div>\n <p class="csdn-collection-msg"></p>\n <ul class="csdn-collection-items"></ul>\n </div>\n </div>\n </div>');return this.box=n,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.inputTitle=this.box.find('.csdn-collection-input input[name="title"]'),this.inputDesc=this.box.find('.csdn-collection-input textarea[name="desc"]'),this.addBtn=this.box.find(".csdn-collection-input button.submit"),this.cancelBtn=this.box.find(".csdn-collection-input button.cancel"),this.privateSwitch=this.box.find(".switch-component #privateSwitch"),this.defaultSwitch=this.box.find(".switch-component #defaultSwitch"),this.closeBtn=this.box.find(".csdn-collection-close"),this.msg=this.box.find(".csdn-collection-msg"),$("body").append(n),this},e.prototype.bindEvent=function(){var t=this;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)}),t.box.on("click",function(t){return t.stopPropagation(),!1}),this.addBtn.on("click",function(e){t.addBtn.hasClass("disable")||window.csdn.userLogin.loadAjax(t.clickAddBtnHandler.bind(t))}),this.cancelBtn.on("click",function(e){t.hideInputBox().hideMsg()}),this.closeBtn.on("click",t.clickCloseBtnHandler.bind(t)),this.box.find(".switch-component label").on("click",function(){var t=$(this).attr("for"),e=$("#"+t).prop("checked");$("#"+t).prop("checked",!e)});var e=function(){var e=$(this),n=e.val(),o=e.parent().find("span.num-limit"),i=+e.attr("maxlength");"title"===e.attr("name")&&(n?t.addBtn.removeClass("disable"):t.addBtn.addClass("disable")),o.text(i-n.length>0?i-n.length:0)};return this.inputTitle.on("input",e),this.inputDesc.on("input",e),this},e.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},e.prototype.trim=function(t){return t.replace(/^\s*/g,"").replace(/\s*$/g,"")},e.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},e.prototype.renderCreateFolder=function(){return'<div>\n <div class="input-wrap">\n <input class="text" name="title" type="text" placeholder="收藏夹标题" maxlength="15" />\n <span class="num-limit">15</span>\n </div>\n <div class="textarea-wrap">\n <textarea class="text" name="desc" rows="3" placeholder="收藏夹描述(可选)" maxlength="200"></textarea> \n <span class="num-limit">200</span>\n </div>\n\n <div class="switch-wrap open-folder-switch">\n <div class="switch-wrap_left">\n <span>私密</span>\n <i class="question">\n <div class="tip">公开的收藏夹可被其他人关注,当该收藏夹被关注后,无法修改为私密</div>\n </i>\n </div>\n <div class="switch-wrap_right">\n <span></span>\n <div class="switch-component">\n <input type="checkbox" id="privateSwitch" class="switch">\n <label for="privateSwitch"></label>\n </div> \n </div>\n </div>\n\n <div class="switch-wrap default-folder-switch">\n <div class="switch-wrap_left">\n <span>默认</span>\n <i class="question">\n <div class="tip">所有收藏操作会默认收藏至该收藏夹,且同时仅存在一个默认收藏夹</div>\n </i>\n </div>\n <div class="switch-wrap_right">\n <span></span>\n <div class="switch-component">\n <input type="checkbox" id="defaultSwitch" class="switch">\n <label for="defaultSwitch"></label>\n </div> \n </div>\n </div>\n\n <div class="collect-box-btn-groups">\n <button class="cancel">返回</button>\n <button class="submit disable">确认创建</button>\n </div>\n </div>'},e.prototype.renderList=function(t){function e(t){return t?t.replace(/</g,"<").replace(/>/g,">"):""}var n=this.getNearItemFolderId(),o=""+t.map(function(t){return'\n <li class="csdn-collection-item '+(t.CheckStatus?"collected":"")+'" '+(t.CheckStatus?"data-check-id="+t.CheckStatus:"")+" data-folder-id="+t.ID+'>\n <div class="csdn-collection-item_left">\n <div class="csdn-collection-item_title">\n <span class="title-m">'+e(t.Name)+"</span>\n "+(t.isDefault?'<span class="def">默认</span>':"")+"\n "+(n===t.ID?'<span class="near">最近使用</span>':"")+'\n </div>\n <span class="csdn-collection-item_ext">\n <span>'+t.FavoriteNum+'条内容</span>\n <span class="dot">・</span>\n <span>'+(t.IsPrivate?"私密":"公开")+'</span>\n </span>\n </div>\n <span class="collect-btn">'+(t.CheckStatus?"已收藏":"收藏")+"\n </span></li>"}).join("");return this.list.html(o),this},e.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},e.prototype.renderItem=function(t){var e='<li class="csdn-collection-item" data-folder-id='+t.id+'>\n <div class="csdn-collection-item_left">\n <div class="csdn-collection-item_title">\n <span class="title-m">'+function(t){return t.replace(/</g,"<").replace(/>/g,">")}(t.name)+'</span>\n </div>\n <span class="csdn-collection-item_ext">\n <span>'+t.favoriteNum+'条内容</span>\n <span class="dot">・</span>\n <span>'+(t.isPrivate?"私密":"公开")+'</span>\n </span>\n </div>\n <span class="collect-btn">收藏</span>\n </li>',n=this.list.find("li:first");return $(e).insertAfter(n),this.list.scrollTop(0),this},e.prototype.getNearItemFolderId=function(){return+localStorage.getItem("csdn_collection_"+this.userName)},e.prototype.setNearItemFolderId=function(t){return localStorage.setItem("csdn_collection_"+this.userName,t),this},e.prototype.setTip=function(){return localStorage.setItem("csdn_collection_tip",(new Date).getTime()),this},e.prototype.getTip=function(){return localStorage.getItem("csdn_collection_tip")},e.prototype.showInputBox=function(){return this.toggleBtn.hide(),this.list.hide(),this.inputBox.show(),this.inputTitle.focus(),this},e.prototype.hideInputBox=function(){return this.inputBox.hide(),this.inputTitle.val(""),this.toggleBtn.show(),this.list.show(),this.privateSwitch&&this.privateSwitch.prop("checked",!1),this.defaultSwitch&&this.defaultSwitch.prop("checked",!1),this},e.prototype.showMsg=function(t){var e=this;return t&&this.msg.html(t).show(),setTimeout(function(){e.hideMsg()},1500),this},e.prototype.hideMsg=function(t){return this.msg.hide().html(),this},e.prototype.checkName=function(t){var e=!0,n="";return e=!!(t&&t.length<=15&&t.length>=2),""===t?n="收藏夹标题不能为空":t&&t.length>15?n="收藏夹标题不能超过15个字":t&&t.length<2&&(n="收藏夹标题在2到15个字之间"),void 0,n&&this.showMsg(n)||this.hideMsg(),e},e.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}})},e.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()})},e.prototype.clickToggleBtnHandler=function(t){var e=this;t.stopPropagation(),e.showInputBox()},e.prototype.clickAddBtnHandler=function(t){var e=this,n=e.trim(e.inputTitle.val()),o={name:n,source:e.options.source,description:e.inputDesc.val()||"",isPrivate:+e.privateSwitch.prop("checked"),isDefault:+e.defaultSwitch.prop("checked"),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.getList(e.renderList).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}})},e.prototype.keyupInputHandler=function(t){var e=t.keyCode;27===e&&this.hideInputBox().hideMsg(),13===e&&this.addBtn.click()},e.prototype.clickCloseBtnHandler=function(){this.box.remove()},e.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()},e.prototype.createCollectionItems=function(){var t=this,e=t.trim(t.inputTitle.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},e.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(t){window.csdn.userLogin.loadAjax(function(){window.csdn.collectionBox.box=new e(t)})},window.csdn.collectionBox.collect=function(e,n){window.csdn.userLogin.loadAjax(function(){var o={url:e.url,source:e.source,sourceId:Number(e.source_id,10),author:e.author,title:e.title,description:e.description||"",fromType:"PC",username:t("UserName")};$.ajax({type:"post",url:"https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/addFavorite",contentType:"application/json",data:JSON.stringify(o),crossDomain:!0,xhrFields:{withCredentials:!0},success:function(t){n&&n(t)},error:function(t){n&&n(t)}})})},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}})}();
|