<commit_before>/*!
 * SigTrello
 *
 * Copyright (C) 2014 Signal Studios
 * Released under the MIT license
 * See LICENSE.txt
 */

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || null;

function error( message ) {
	//alert( message );
}

var spamLimitCounter = 1000; // Safety
function spamLimit( ) {
	//return --limit < 0;
	return false;
}

function getBestByNameIndex( name, index, items ) {
	if( items.length > index && items[ index ].name == name )
		return items[ index ];

	var matchingName = $.grep( items, function(i) { return i.name == name });
	if( matchingName.length == 1 )
		return matchingName[ 0 ];
	
	return null; // Ambiguous due to move of duplicately named list item, or no such item.
}

function authorize( ) {
	Trello.authorize({
		type: "popup",
		name: "SigTrello",
		persist: true,
		interactive: true,
		scope: { read: true, write: true, account: false },
		expiration: "never"
	});
}

function replaceWithLink( ) {
	var toLink = this;
	if( !Trello.authorized() ) {
		authorize( );
		if( !Trello.authorized() )
			return;
	}

	var $this				= $(this);

	var $checklistItem		= $this.parent('.checklist-item');
	var $checklistItemText 	= $checklistItem.find('.checklist-item-details-text')
	var hasLinks			= $checklistItemText.find('a').length > 0;
	if( hasLinks )
		return;

	var baseURI				= window.document.baseURI;
	var cardShortId			= baseURI.replace( /^http[s]?:\/\/trello.com\/c\/([^\/]+)\/.*/, "$1" ); // Parse "mbdChsJG" out of "https://trello.com/c/mbdChsJG/185-test"
	var $checklistItemsList	= $checklistItem.parent('.checklist-items-list');
	var $checklist			= $checklistItemsList.parent('.checklist');
	var $checklistList		= $checklist.parent('.checklist-list');
	
	var checklistItemTitle	= $checklistItemText.text( );
	var checklistItemIndex	= $checklistItemsList.find('.checklist-item').index($checklistItem);
	var checklistTitle		= $checklist.find('.checklist-title h3').text( );
	var checklistIndex		= $checklistList.find('.checklist').index($checklist);
	
	// TODO: Learn how to tame JavaScript callback hell.
	
	// 1.  Identify the trello list
	Trello
        .get( "cards/"+cardShortId+"/list", { fields: "" } )
        .done( function( list ) {

			// 2. Identify the checklist item being replaced.
			Trello
				.get( "cards/"+cardShortId+"/checklists" )
				.done( function( cardInfo ) {
					var cardChecklist = getBestByNameIndex( checklistTitle, checklistIndex, cardInfo );
					if( cardChecklist == null ) { error( "Checklist ambiguous or missing!" ); return; }

					var cardChecklistItem = getBestByNameIndex( checklistItemTitle, checklistItemIndex, cardChecklist.checkItems );
					if( cardChecklistItem == null ) { error( "Checklist item ambiguous or missing!" ); return; }

					// 3.  Post new card to the list
					Trello
						.post( "lists/"+list.id+"/cards", { name: checklistItemTitle, desc: "Parent: "+baseURI+" "+checklistTitle, due: null } )
						.done( function( newCard ) {

							// 4.  Replace checklist item with new card
							Trello<commit_msg>Allow the link to work if the card isn't simply a single URL already.  Fixes #6.<commit_after>/*!
 * SigTrello
 *
 * Copyright (C) 2014 Signal Studios
 * Released under the MIT license
 * See LICENSE.txt
 */

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || null;

function error( message ) {
	//alert( message );
}

var spamLimitCounter = 1000; // Safety
function spamLimit( ) {
	//return --limit < 0;
	return false;
}

function getBestByNameIndex( name, index, items ) {
	if( items.length > index && items[ index ].name == name )
		return items[ index ];

	var matchingName = $.grep( items, function(i) { return i.name == name });
	if( matchingName.length == 1 )
		return matchingName[ 0 ];

	return null; // Ambiguous due to move of duplicately named list item, or no such item.
}

function authorize( ) {
	Trello.authorize({
		type: "popup",
		name: "SigTrello",
		persist: true,
		interactive: true,
		scope: { read: true, write: true, account: false },
		expiration: "never"
	});
}

function getFlatName( $node ) {
	var $elements = $node.contents( );
	var s = "";
	for( var i = 0; i < $elements.length; ++i ) {
		var element = $elements.get(i);
		if( element.nodeName == 'A' ) {
			s += $(element).attr("href");
		} else {
			s += $(element).text( );
		}
	}
	return s;
}

function replaceWithLink( ) {
	var toLink = this;
	if( !Trello.authorized() ) {
		authorize( );
		if( !Trello.authorized() )
			return;
	}

	var $this				= $(this);

	var $checklistItem		= $this.parent('.checklist-item');
	var $checklistItemText 	= $checklistItem.find('.checklist-item-details-text')
	var $links				= $checklistItemText.find('a');
	if( $links.length == 1 && $links.text == $this.text )
		return;

	var baseURI				= window.document.baseURI;
	var cardShortId			= baseURI.replace( /^http[s]?:\/\/trello.com\/c\/([^\/]+)\/.*/, "$1" ); // Parse "mbdChsJG" out of "https://trello.com/c/mbdChsJG/185-test"
	var $checklistItemsList	= $checklistItem.parent('.checklist-items-list');
	var $checklist			= $checklistItemsList.parent('.checklist');
	var $checklistList		= $checklist.parent('.checklist-list');
	
	var checklistItemTitle	= $checklistItemText.text( );
	var checklistItemName	= getFlatName( $checklistItemText );
	var checklistItemIndex	= $checklistItemsList.find('.checklist-item').index($checklistItem);
	var checklistTitle		= $checklist.find('.checklist-title h3').text( );
	var checklistIndex		= $checklistList.find('.checklist').index($checklist);
	
	// TODO: Learn how to tame JavaScript callback hell.
	
	// 1.  Identify the trello list
	Trello
        .get( "cards/"+cardShortId+"/list", { fields: "" } )
        .done( function( list ) {

			// 2. Identify the checklist item being replaced.
			Trello
				.get( "cards/"+cardShortId+"/checklists" )
				.done( function( cardInfo ) {
					var cardChecklist = getBestByNameIndex( checklistTitle, checklistIndex, cardInfo );
					if( cardChecklist == null ) { error( "Checklist ambiguous or missing!" ); return; }

					var cardChecklistItem = getBestByNameIndex( checklistItemName, checklistItemIndex, cardChecklist.checkItems );
					if( cardChecklistItem == null ) { error( "Checklist item ambiguous or missing!" ); return; }

					// 3.  Post new card to the list
					Trello
						.post( "lists/"+list.id+"/cards", { name: checklistItemTitle, desc: "Parent: "+baseURI+" "+checklistTitle, due: null } )
						.done( function( newCard ) {

							// 4.  Replace checklist item with new card
							Trello
{'fraction_non_alphanumeric': 0.15174920490686053, 'fraction_numerical': 0.0066636377404210205, 'mean_word_length': 3.6376404494382024, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5018683', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2172, 'n_words': 558}