1/** 2 * This module provides an api for handling content objects e.g. Posts, Polls... 3 * 4 * @type undefined|Function 5 */ 6 7humhub.module('content', function (module, require, $) { 8 var client = require('client'); 9 var util = require('util'); 10 var string = util.string; 11 var actions = require('action'); 12 var Component = actions.Component; 13 var event = require('event'); 14 var modal = require('ui.modal'); 15 var status = require('ui.status'); 16 17 var DATA_CONTENT_KEY = "content-key"; 18 var DATA_CONTENT_DELETE_URL = "content-delete-url"; 19 var DATA_ADMIN_DELETE_MODAL_URL = "admin-delete-modal-url"; 20 21 22 Component.addSelector('content-component'); 23 24 var Content = Component.extend('Content'); 25 26 /** 27 * Abstract loader function which can be used to activate or deactivate a 28 * loader within a content entry. 29 * 30 * If $show is undefined or true the loader animation should be rendered 31 * otherwise it should be removed. 32 * 33 * @param {type} $show 34 * @returns {undefined} 35 */ 36 Content.prototype.loader = function ($show) { /* Abstract loader function */ }; 37 Content.prototype.edit = function (successHandler) {/** Abstract edit function **/}; 38 39 Content.getNodeByKey = function (key) { 40 return $('[data-content-key="' + key + '"]'); 41 }; 42 43 Content.prototype.getKey = function () { 44 return this.$.data(DATA_CONTENT_KEY); 45 }; 46 47 Content.prototype.create = function (addContentHandler) { 48 //Note that this Content won't have an id, so the backend will create an instance 49 if (this.hasAction('create')) { 50 return; 51 } 52 53 this.edit(addContentHandler); 54 }; 55 56 Content.prototype.delete = function (options) { 57 options = options || {}; 58 59 var that = this; 60 return new Promise(function (resolve, reject) { 61 62 var modalOptions = options.modal || module.config.modal.deleteConfirm; 63 64 if(options.$trigger && options.$trigger.is('[data-action-confirm]')) { 65 that.deleteContent(resolve, reject); 66 } else { 67 modal.confirm(modalOptions).then(function ($confirmed) { 68 if (!$confirmed) { 69 resolve(false); 70 return; 71 } 72 73 that.deleteContent(resolve, reject); 74 }); 75 } 76 }); 77 }; 78 79 Content.prototype.adminDelete = function (options) { 80 var that = this; 81 82 var loadModalUrl = that.data(DATA_ADMIN_DELETE_MODAL_URL) || module.config.adminDeleteModalUrl; 83 84 if(!loadModalUrl) { 85 that.delete(options); 86 return; 87 } 88 89 return new Promise(function (resolve, reject) { 90 client.post(loadModalUrl, { 91 data: { 92 id: that.getKey() 93 } 94 }).then(function (response) { 95 modal.confirm(response).then(function ($confirmed) { 96 if (!$confirmed) { 97 resolve(false); 98 return; 99 } 100 101 var form = modal.globalConfirm.$.find('form')[0]; 102 103 that.deleteContent(resolve, reject, form); 104 }); 105 }).catch(function (err) { 106 reject(err); 107 }); 108 }); 109 }; 110 111 Content.prototype.deleteContent = function(resolve, reject, form) { 112 var that = this; 113 that.loader(); 114 var deleteUrl = that.data(DATA_CONTENT_DELETE_URL) || module.config.deleteUrl; 115 116 var postData = { 117 id: that.getKey() 118 }; 119 120 if(typeof form !== 'undefined') { 121 Object.assign(postData, { 122 ...$(form).serializeArray().reduce(function(a, e) { 123 a[e.name] = e.value; 124 return a; 125 }) 126 }); 127 } 128 129 if (deleteUrl) { 130 client.post(deleteUrl, { 131 data: postData 132 }).then(function (response) { 133 if (response.response.success) { 134 that.remove().then(function () { 135 resolve(true); 136 }); 137 } else { 138 status.error(response.response.error); 139 } 140 }).catch(function (err) { 141 reject(err); 142 }).finally(function () { 143 that.loader(false); 144 }); 145 } else {
146 reject('Content delete was called, but no url could be determined for ' + that.base); 147 that.loader(false); 148 } 149 }; 150 151 Content.prototype.remove = function () { 152 var that = this; 153 return new Promise(function (resolve, reject) { 154 that.$.animate({height: 'toggle', opacity: 'toggle'}, 'fast', function () { 155 that.$.remove(); 156 event.trigger('humhub:modules:content:afterRemove', that); 157 resolve(that); 158 }); 159 }); 160 }; 161 162 Content.prototype.permalink = function (evt) { 163 permalink(evt); 164 }; 165 166 var permalink = function(evt) { 167 var options = module.config.modal.permalink; 168 options.permalink = evt.$trigger.data('content-permalink'); 169 170 modal.global.set({ 171 header: evt.$trigger.data('content-permalink-title') || options.head, 172 body: string.template(module.templates.permalinkBody, options), 173 footer: string.template(module.templates.permalinkFooter, options), 174 size: 'normal' 175 }).show(); 176 177 modal.global.$.find('textarea').focus().select(); 178 179 // Make sure the modal is closed when pjax loads 180 event.one('humhub:ready', function () { 181 modal.global.close(); 182 }); 183 }; 184 185 var submitMove = function(evt) { 186 modal.submit(evt).then(function(response) { 187 if(response.success) { 188 if(response.message) { 189 module.log.success(response.message); 190 } 191 event.trigger('humhub:content:afterMove', response); 192 } 193 }); 194 }; 195 196 var templates = { 197 permalinkBody: '<div class="clearfix"><textarea rows="3" class="form-control permalink-txt" spellcheck="false" readonly>{permalink}</textarea><p class="help-block pull-right"><a href="#" data-action-click="copyToClipboard" data-action-target=".permalink-txt"><i class="fa fa-clipboard" aria-hidden="true"></i> {info}</a></p></div>', 198 permalinkFooter: '<button data-modal-close class="btn btn-default">{buttonClose}</button><a href="{permalink}" class="btn btn-primary" data-ui-loader>{buttonOpen}</a>' 199 }; 200 201 module.export({ 202 Content: Content, 203 templates: templates, 204 permalink: permalink, 205 submitMove: submitMove 206 }); 207});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.