1 /*global jQuery:true, GCN: true */ 2 (function (GCN) { 3 4 'use strict'; 5 6 /** 7 * Checks whether or not a tag container instance has data for a tag of the 8 * given name. 9 * 10 * @param {TagContainerAPI} container The container in which to look 11 * for the tag. 12 * @param {string} tagname The name of the tag to find. 13 * @return {boolean} True if the container contains a data for the tag of 14 * the given name. 15 */ 16 function hasTagData(container, tagname) { 17 return !!container._data.tags[tagname]; 18 } 19 20 /** 21 * Extends the internal taglist with data from the given collection of tags. 22 * Will overwrite the data of any tag whose name is matched in the `tags' 23 * associative array. 24 * 25 * @param {TagContainerAPI} container The container in which to look 26 * for the tag. 27 * @param {object} tags Associative array of tag data, mapped against the 28 * data's corresponding tag name. 29 */ 30 function extendTags(container, tags) { 31 jQuery.extend(container._data.tags, tags); 32 } 33 34 /** 35 * Gets the construct matching the given keyword. 36 * 37 * @param {string} keyword Construct keyword. 38 * @param {NodeAPI} node The node inwhich to search for the construct. 39 * @param {function(object)} success Callback function to receive the 40 * successfully found construct. 41 * @param {function(GCNError):boolean} error Optional custom error handler. 42 */ 43 function getConstruct(keyword, node, success, error) { 44 node.constructs(function (constructs) { 45 if (constructs[keyword]) { 46 success(constructs[keyword]); 47 } else { 48 var err = GCN.createError( 49 'CONSTRUCT_NOT_FOUND', 50 'Cannot find construct `' + keyword + '\'', 51 constructs 52 ); 53 GCN.handleError(err, error); 54 } 55 }, error); 56 } 57 58 /** 59 * Creates an new tag via the GCN REST-API. 60 * 61 * @param {TagAPI} tag A representation of the tag which will be created in 62 * the GCN backend. 63 * @param {object} data The request body that will be serialized into json. 64 * @param {function(TagAPI)} success Callback function to receive the 65 * successfully created tag. 66 * @param {function(GCNError):boolean} error Optional custom error handler. 67 */ 68 function newTag(tag, data, success, error) { 69 var obj = tag.parent(); 70 var url = GCN.settings.BACKEND_PATH + '/rest/' + obj._type 71 + '/newtag/' + obj._data.id; 72 obj._authAjax({ 73 type: 'POST', 74 url: url, 75 json: data, 76 error: function (xhr, status, msg) { 77 GCN.handleHttpError(xhr, msg, error); 78 tag._vacate(); 79 }, 80 success: function (response) { 81 obj._handleCreateTagResponse(tag, response, success, 82 error); 83 } 84 }, error); 85 } 86 87 /** 88 * Checks whether exactly one of the following combination of options is 89 * provided: 90 * 91 * 1. `keyword' alone 92 * or 93 * 2. `constructId' alone 94 * or 95 * 3. `sourcePageId' and `sourceTagname' together. 96 * 97 * Each of these options are mutually exclusive. 98 * 99 * @param {Object} options 100 * @return {boolean} True if only one combination of the possible options 101 * above is contained in the given options object. 102 */ 103 function isValidCreateTagOptions(options) { 104 // If the sum is 0, it means that no options was specified. 105 // 106 // If the sum is greater than 0 but less than 2, it means that either 107 // `sourcePageId' or `sourceTagname' was specified, but not both as 108 // required. 109 // 110 // If the sum is greater than 2, it means that more than one 111 // combination of settings was provided, which is one too many. 112 return 2 === (options.sourcePageId ? 1 : 0) + 113 (options.sourceTagname ? 1 : 0) + 114 (options.keyword ? 2 : 0) + 115 (options.constructId ? 2 : 0); 116 } 117 118 /** 119 * Parse the arguments passed into createTag() into a normalized object. 120 * 121 * @param {Arguments} createTagArgumenents An Arguments object. 122 * @parma {object} Normalized map of arguments. 123 */ 124 function parseCreateTagArguments(createTagArguments) { 125 var args = Array.prototype.slice.call(createTagArguments); 126 if (0 === args.length) { 127 return { 128 error: '`createtag()\' requires at least one argument. See ' + 129 'documentation.' 130 }; 131 } 132 133 var options; 134 135 // The first argument must either be a string, number or an object. 136 switch (jQuery.type(args[0])) { 137 case 'string': 138 options = {keyword: args[0]}; 139 break; 140 case 'number': 141 options = {constructId: args[0]}; 142 break; 143 case 'object': 144 if (!isValidCreateTagOptions(args[0])) { 145 return { 146 error: 'createTag() requires exactly one of the ' + 147 'following, mutually exclusive, settings to be' + 148 'used: either `keyword\', `constructId\' or a ' + 149 'combination of `sourcePageId\' and ' + 150 '`sourceTagname\'.' 151 }; 152 } 153 options = args[0]; 154 break; 155 default: 156 options = {}; 157 } 158 159 // Determine success() and error(): arguments 2-3. 160 var i; 161 for (i = 1; i < args.length; i++) { 162 if (jQuery.type(args[i]) === 'function') { 163 if (options.success) { 164 options.error = args[i]; 165 } else { 166 options.success = args[i]; 167 } 168 } 169 } 170 171 return { 172 options: options 173 }; 174 } 175 176 /** 177 * Given an object containing information about a tag, determines whether 178 * or not we should treat a tag as a editabled block. 179 * 180 * Relying on `onlyeditables' property to determine whether or not a given 181 * tag is a block or an editable is unreliable since it is possible to have 182 * a block which only contains editables: 183 * 184 * { 185 * "tagname":"content", 186 * "editables":[{ 187 * "element":"GENTICS_EDITABLE_1234", 188 * "readonly":false, 189 * "partname":"editablepart" 190 * }], 191 * "element":"GENTICS_BLOCK_1234", 192 * "onlyeditables":true 193 * "tagname":"tagwitheditable" 194 * } 195 * 196 * In the above example, even though `onlyeditable' is true the tag is 197 * still a block, since the tag's element and the editable's element are 198 * not the same. 199 * 200 * @param {object} tag A object holding the sets of blocks and editables 201 * that belong to a tag. 202 * @return {boolean} True if the tag 203 */ 204 function isBlock(tag) { 205 if (!tag.editables || tag.editables.length > 1) { 206 return true; 207 } 208 return ( 209 (1 === tag.editables.length) 210 && 211 (tag.editables[0].element !== tag.element) 212 ); 213 } 214 215 /** 216 * Given a data object received from a REST API "/rest/page/render" 217 * call maps the blocks and editables into a list of each. 218 * 219 * The set of blocks and the set of editables that are returned are not 220 * mutually exclusive--if a tag is determined to be both an editable 221 * and a block, it will be included in both sets. 222 * 223 * @param {object} data 224 * @return {object<string, Array.<object>>} A map containing a set of 225 * editables and a set of 226 * blocks. 227 */ 228 function getEditablesAndBlocks(data) { 229 if (!data || !data.tags) { 230 return { 231 blocks: [], 232 editables: [] 233 }; 234 } 235 236 var tag; 237 var tags = data.tags; 238 var blocks = []; 239 var editables = []; 240 var i; 241 var j; 242 243 for (i = 0; i < tags.length; i++) { 244 tag = tags[i]; 245 if (tag.editables) { 246 for (j = 0; j < tag.editables.length; j++) { 247 tag.editables[j].tagname = tag.tagname; 248 } 249 editables = editables.concat(tag.editables); 250 } 251 if (isBlock(tag)) { 252 blocks.push(tag); 253 } 254 } 255 256 return { 257 blocks: blocks, 258 editables: editables 259 }; 260 } 261 262 /** 263 * Abstract class that is implemented by tag containers such as 264 * {@link PageAPI} or {@link TemplateAPI} 265 * 266 * @class 267 * @name TagContainerAPI 268 */ 269 GCN.TagContainerAPI = GCN.defineChainback({ 270 /** @lends TagContainerAPI */ 271 272 /** 273 * @private 274 * @type {object<number, string>} Hash, mapping tag ids to their 275 * corresponding names. 276 */ 277 _tagIdToNameMap: null, 278 279 /** 280 * @private 281 * @type {object<number, string>} Hash, mapping tag ids to their 282 * corresponding names for newly created 283 * tags. 284 */ 285 _createdTagIdToNameMap: {}, 286 287 /** 288 * @private 289 * @type {Array.<object>} A set of blocks that are are to be removed 290 * from this content object when saving it. 291 * This array is populated during the save 292 * process. It get filled just before 293 * persisting the data to the server, and gets 294 * emptied as soon as the save operation 295 * succeeds. 296 */ 297 _deletedBlocks: [], 298 299 /** 300 * @private 301 * @type {Array.<object>} A set of tags that are are to be removed from 302 * from this content object when it is saved. 303 */ 304 _deletedTags: [], 305 306 /** 307 * Searching for a tag of a given id from the object structure that is 308 * returned by the REST API would require O(N) time. This function, 309 * builds a hash that maps the tag id with its corresponding name, so 310 * that it can be mapped in O(1) time instead. 311 * 312 * @private 313 * @return {object<number,string>} A hash map where the key is the tag 314 * id, and the value is the tag name. 315 */ 316 '!_mapTagIdsToNames': function () { 317 var name; 318 var map = {}; 319 var tags = this._data.tags; 320 for (name in tags) { 321 if (tags.hasOwnProperty(name)) { 322 map[tags[name].id] = name; 323 } 324 } 325 return map; 326 }, 327 328 /** 329 * Retrieves data for a tag from the internal data object. 330 * 331 * @private 332 * @param {string} name The name of the tag. 333 * @return {!object} The tag data, or null if a there if no tag 334 * matching the given name. 335 */ 336 '!_getTagData': function (name) { 337 return (this._data.tags && this._data.tags[name]) || 338 (this._shadow.tags && this._shadow.tags[name]); 339 }, 340 341 /** 342 * Get the tag whose id is `id'. 343 * Builds the `_tagIdToNameMap' hash map if it doesn't already exist. 344 * 345 * @todo: Should we deprecate this? 346 * @private 347 * @param {number} id Id of tag to retrieve. 348 * @return {object} The tag's data. 349 */ 350 '!_getTagDataById': function (id) { 351 if (!this._tagIdToNameMap) { 352 this._tagIdToNameMap = this._mapTagIdsToNames(); 353 } 354 return this._getTagData(this._tagIdToNameMap[id] || 355 this._createdTagIdToNameMap[id]); 356 }, 357 358 /** 359 * Extracts the editables and blocks that have been rendered from the 360 * REST API render call's response data. 361 * 362 * @param {object} data The response object received from the 363 * renderTemplate() call. 364 * @return {object} An object containing two properties: an array of 365 * blocks, and an array of editables. 366 */ 367 '!_processRenderedTags': getEditablesAndBlocks, 368 369 // !!! 370 // WARNING adding to folder is neccessary as jsdoc will report a 371 // name confict otherwise 372 // !!! 373 /** 374 * Get this content object's node. 375 * 376 * @function 377 * @name node 378 * @memberOf TagContainerAPI 379 * @param {funtion(NodeAPI)=} success Optional callback to receive a 380 * {@link NodeAPI} object as the 381 * only argument. 382 * @param {function(GCNError):boolean=} error Optional custom error 383 * handler. 384 * @return {NodeAPI} This object's node. 385 */ 386 '!node': function (success, error) { 387 return this.folder().node(); 388 }, 389 390 // !!! 391 // WARNING adding to folder is neccessary as jsdoc will report a 392 // name confict otherwise 393 // !!! 394 /** 395 * Get this content object's parent folder. 396 * 397 * @function 398 * @name folder 399 * @memberOf TagContainerAPI 400 * @param {funtion(FolderAPI)=} 401 * success Optional callback to receive a {@link FolderAPI} 402 * object as the only argument. 403 * @param {function(GCNError):boolean=} 404 * error Optional custom error handler. 405 * @return {FolderAPI} This object's parent folder. 406 */ 407 '!folder': function (success, error) { 408 var id = this._fetched ? this.prop('folderId') : null; 409 return this._continue(GCN.FolderAPI, id, success, error); 410 }, 411 412 /** 413 * Gets a tag of the specified id, contained in this content object. 414 * 415 * @name tag 416 * @function 417 * @memberOf TagContainerAPI 418 * @param {function} success 419 * @param {function} error 420 * @return TagAPI 421 */ 422 '!tag': function (id, success, error) { 423 return this._continue(GCN.TagAPI, id, success, error); 424 }, 425 426 /** 427 * Retrieves a collection of tags from this content object. 428 * 429 * @name tags 430 * @function 431 * @memberOf TagContainerAPI 432 * @param {object|string|number} settings (Optional) 433 * @param {function} success callback 434 * @param {function} error (Optional) 435 * @return TagContainerAPI 436 */ 437 '!tags': function () { 438 var args = Array.prototype.slice.call(arguments); 439 440 if (args.length === 0) { 441 return; 442 } 443 444 var i; 445 var j = args.length; 446 var filter = {}; 447 var filters; 448 var hasFilter = false; 449 var success; 450 var error; 451 452 // Determine `success', `error', `filter' 453 for (i = 0; i < j; ++i) { 454 switch (jQuery.type(args[i])) { 455 case 'function': 456 if (success) { 457 error = args[i]; 458 } else { 459 success = args[i]; 460 } 461 break; 462 case 'number': 463 case 'string': 464 filters = [args[i]]; 465 break; 466 case 'array': 467 filters = args[i]; 468 break; 469 } 470 } 471 472 if (jQuery.type(filters) === 'array') { 473 var k = filters.length; 474 while (k) { 475 filter[filters[--k]] = true; 476 } 477 hasFilter = true; 478 } 479 480 var that = this; 481 482 if (success) { 483 this._read(function () { 484 var tags = that._data.tags; 485 var tag; 486 var list = []; 487 488 for (tag in tags) { 489 if (tags.hasOwnProperty(tag)) { 490 if (!hasFilter || filter[tag]) { 491 list.push(that._continue(GCN.TagAPI, tags[tag], 492 null, error)); 493 } 494 } 495 } 496 497 that._invoke(success, [list]); 498 }, error); 499 } 500 }, 501 502 /** 503 * Internal method to create a tag of a given tagtype in this content 504 * object. 505 * 506 * Not all tag containers allow for new tags to be created on them, 507 * therefore this method will only be surfaced by tag containers which 508 * do allow this. 509 * 510 * @private 511 * @param {string|number|object} construct either the keyword of the 512 * construct, or the ID of the construct 513 * or an object with the following 514 * properties 515 * <ul> 516 * <li><i>keyword</i> keyword of the construct</li> 517 * <li><i>constructId</i> ID of the construct</li> 518 * <li><i>magicValue</i> magic value to be filled into the tag</li> 519 * <li><i>sourcePageId</i> source page id</li> 520 * <li><i>sourceTagname</i> source tag name</li> 521 * </ul> 522 * @param {function(TagAPI)=} success Optional callback that will 523 * receive the newly created tag as 524 * its only argument. 525 * @param {function(GCNError):boolean=} error Optional custom error 526 * handler. 527 * @return {TagAPI} The newly created tag. 528 */ 529 '!_createTag': function () { 530 var args = parseCreateTagArguments(arguments); 531 532 if (args.error) { 533 GCN.handleError( 534 GCN.createError('INVALID_ARGUMENTS', args.error, arguments), 535 args.error 536 ); 537 return; 538 } 539 540 var obj = this; 541 542 // We use a uniqueId to avoid a fetus being created. 543 // This is to avoid the following scenario: 544 // 545 // var tag1 = container.createTag(...); 546 // var tag2 = container.createTag(...); 547 // tag1 === tag2 // is true which is wrong 548 // 549 // However, for all other cases, where we get an existing object, 550 // we want this behaviour: 551 // 552 // var folder1 = page(1).folder(...); 553 // var folder2 = page(1).folder(...); 554 // folder1 === folder2 // is true which is correct 555 // 556 // So, createTag() is different from other chainback methods in 557 // that each invokation must create a new instance, while other 558 // chainback methods must return the same. 559 // 560 // The id will be reset as soon as the tag object is realized. 561 // This happens below as soon as we get a success response with the 562 // correct tag id. 563 var newId = GCN.uniqueId('TagApi-unique-'); 564 565 // Create a new TagAPI instance linked to this tag container. Also 566 // acquire a lock on the newly created tag object so that any 567 // further operations on it will be queued until the tag object is 568 // fully realized. 569 var tag = obj._continue(GCN.TagAPI, newId)._procure(); 570 571 var options = args.options; 572 var copying = !!(options.sourcePageId && options.sourceTagname); 573 574 var onCreate = function () { 575 if (options.success) { 576 obj._invoke(options.success, [tag]); 577 } 578 tag._vacate(); 579 }; 580 581 if (copying) { 582 newTag(tag, { 583 copyPageId: options.sourcePageId, 584 copyTagname: options.sourceTagname 585 }, onCreate, options.error); 586 } else { 587 if (options.constructId) { 588 newTag(tag, { 589 magicValue: options.magicValue, 590 constructId: options.constructId 591 }, onCreate, options.error); 592 } else { 593 // ASSERT(options.keyword) 594 getConstruct(options.keyword, obj.node(), function (construct) { 595 newTag(tag, { 596 magicValue: options.magicValue, 597 constructId: construct.constructId 598 }, onCreate, options.error); 599 }, options.error); 600 } 601 } 602 603 return tag; 604 }, 605 606 /** 607 * Internal helper method to handle the create tag response. 608 * 609 * @private 610 * @param {TagAPI} tag 611 * @param {object} response response object from the REST call 612 * @param {function(TagContainerAPI)=} success optional success handler 613 * @param {function(GCNError):boolean=} error optional error handler 614 */ 615 '!_handleCreateTagResponse': function (tag, response, success, error) { 616 var obj = this; 617 618 if (GCN.getResponseCode(response) === 'OK') { 619 var data = response.tag; 620 tag._name = data.name; 621 tag._data = data; 622 tag._fetched = true; 623 624 // The tag's id is still the temporary unique id that was given 625 // to it in _createTag(). We have to realize the tag so that 626 // it gets the correct id. The new id changes its hash, so it 627 // must also be removed and reinserted from the caches. 628 tag._removeFromTempCache(tag); 629 tag._setHash(data.id)._addToCache(); 630 631 // Add this tag into the tag's container `_shadow' object, and 632 // `_tagIdToNameMap hash'. 633 var shouldCreateObjectIfUndefined = true; 634 obj._update('tags.' + GCN.escapePropertyName(data.name), 635 data, error, shouldCreateObjectIfUndefined); 636 637 // TODO: We need to store the tag inside the `_data' object for 638 // now. A change should be made so that when containers are 639 // saved, the data in the `_shadow' object is properly 640 // transfered into the _data object. 641 obj._data.tags[data.name] = data; 642 643 if (!obj.hasOwnProperty('_createdTagIdToNameMap')) { 644 obj._createdTagIdToNameMap = {}; 645 } 646 647 obj._createdTagIdToNameMap[data.id] = data.name; 648 649 if (success) { 650 success(); 651 } 652 } else { 653 tag._die(GCN.getResponseCode(response)); 654 GCN.handleResponseError(response, error); 655 } 656 }, 657 658 /** 659 * Internal method to delete the specified tag from this content 660 * object. 661 * 662 * @private 663 * @param {string} keyword The keyword of the tag to be deleted. 664 * @param {function(TagContainerAPI)=} success Optional callback that 665 * receive this object as 666 * its only argument. 667 * @param {function(GCNError):boolean=} error Optional custom error 668 * handler. 669 */ 670 '!_removeTag': function (keyword, success, error) { 671 this.tag(keyword).remove(success, error); 672 }, 673 674 /** 675 * Internal method to delete a set of tags from this content object. 676 * 677 * @private 678 * @param {Array.<string>} keywords The keywords of the set of tags to be 679 * deleted. 680 * @param {function(TagContainerAPI)=} success Optional callback that 681 * receive this object as 682 * its only argument. 683 * @param {function(GCNError):boolean=} error Optional custom error 684 * handler. 685 */ 686 '!_removeTags': function (keywords, success, error) { 687 var that = this; 688 this.tags(keywords, function (tags) { 689 var j = tags.length; 690 while (j--) { 691 tags[j].remove(null, error); 692 } 693 if (success) { 694 that.save(success, error); 695 } 696 }, error); 697 } 698 699 }); 700 701 GCN.TagContainerAPI.hasTagData = hasTagData; 702 GCN.TagContainerAPI.extendTags = extendTags; 703 GCN.TagContainerAPI.getEditablesAndBlocks = getEditablesAndBlocks; 704 705 }(GCN)); 706