foldershare-8.x-1.2/js/foldershare.ui.utility.js
js/foldershare.ui.utility.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /** * @file * Implements the FolderShare utility functions. * * The utility functions are shared among multiple UI scripts for the module. * They provide string handling and error message printing. * * @ingroup foldershare */ ( function ($, Drupal) { // Define Drupal.foldershare if it hasn't been defined yet. if ( "foldershare" in Drupal === false ) { Drupal.foldershare = {}; } Drupal.foldershare.utility = { /*-------------------------------------------------------------------- * * String utilities. * *--------------------------------------------------------------------*/ /** * Returns the title-case form of a string. * * @param {string} text * The string to convert to title-case. * * @return {string} * Returns the converted string. */ getTitleCase(text) { return text.replace( /\w\S*/g, t => t.charAt(0).toUpperCase() + t.substr(1).toLowerCase()); }, /** * Returns the translated singular entity kind name in title-case. * * The kind name (e.g. "file", "folder", etc.) is looked up in a list * of translated kinds provided by the server. The value is converted to * singular title-case and returned. * * @param {object} terminology * A terminology object containing a 'kinds' property that is an array * with kind name keys. Each entry in the array is an object with * 'plural' and 'singular' properties that provide the translated * plural and singular forms of the kind name. * @param {string} kind * The kind name to look up. * * @return {string} * The title-case translated singular kind. */ getKindSingular(terminology, kind) { if ( "kinds" in terminology === true && kind in terminology.kinds === true ) { kind = terminology.kinds[kind].singular; } return Drupal.foldershare.utility.getTitleCase(kind); }, /** * Returns the translated plural entity kind name in title-case. * * The kind name (e.g. "file", "folder", etc.) is looked up in a list * of translated kinds provided by the server. The value is converted to * plural title-case and returned. * * @param {object} terminology * A terminology object containing a 'kinds' property that is an array * with kind name keys. Each entry in the array is an object with * 'plural' and 'singular' properties that provide the translated * plural and singular forms of the kind name. * @param {string} kind * The kind name to look up. * * @return {string} * The title-case translated plural kind. */ getKindPlural(terminology, kind) { if ( "kinds" in terminology === true && kind in terminology.kinds === true ) { kind = terminology.kinds[kind].plural; } return Drupal.foldershare.utility.getTitleCase(kind); }, /** * Returns the translated term in title-case or lower-case. * * The term is looked up in the list of translated terms provided by * the server. The term is converted to title case and returned. * * @param {object} terminology * A terminology object containing a 'text' property that is an array * with strings as keys, and the translated form of the string as * values. * @param {string} term * The term to look up. * @param {boolean} titleCase * (optional, default = true) When true, the returned term uses * title-case. When false, it is entirely lower case. * * @return {string} * The title case translated term. */ getTerm(terminology, term, titleCase = true ) { // Find the translated singular or plural term. if ( "text" in terminology === true && term in terminology.text === true ) { term = terminology.text[term]; } if (titleCase === true ) { return Drupal.foldershare.utility.getTitleCase(term); } // Map to lower case. term = term.toLowerCase(); return term; }, /*-------------------------------------------------------------------- * * Print utilities. * *--------------------------------------------------------------------*/ /** * Prints a malformed page error message to the console. * * @param {string} body * The body of the message. */ printMalformedError(body = "" ) { Drupal.foldershare.utility.printMessage( "Malformed page" , `${body} The user interface cannot be enabled.`); }, /** * Prints a message to the console. * * The title is printed in bold, followed by optional body text * in a normal weight font, indented below the title. * * @param {string} title * The short title of the message. * @param {string} body * The body of the message. */ printMessage(title, body = "" ) { console.log( `%cFolderShare: ${title}:%c\n%c${body}%c`, "font-weight: bold" , "font-weight: normal" , "padding-left: 2em" , "padding-left: 0" ); }, /** * Prints a message to the console, followed by the selection. * * The title is printed in bold, followed by optional body text * in a normal weight font, indented below the title. Below this, * the current selection's IDs are listed. * * @param {string} title * The short title of the message. * @param {string} body * The body of the message. * @param {object} selection * A selection object with one property for each kind for which a * selected item is present. The value for each property is an array * of integer entity IDs of that kind. */ printSelection(title, body, selection) { Drupal.foldershare.utility.printMessage(title, body); if (selection.length === 0) { console.log( "%cSelection: none%c" , "padding-left: 2em" , "padding-left: 0" ); return ; } Object.keys(selection).forEach(kind => { const n = selection[kind].length; const ids = []; for (let i = 0; i < n; ++i) { const entry = selection[kind][i]; ids.push(Number(entry.id)); } console.log( `%cSelection of ${kind}: ${ids.toString()}%c`, "padding-left: 2em" , "padding-left: 0" ); }); }, /** * Prints a message to the console, followed by the file list. * * The title is printed in bold, followed by optional body text * in a normal weight font, indented below the title. Below this, * the file list's file names, sizes, and MIME types are listed. * * @param {string} title * The short title of the message. * @param {string} body * The body of the message. * @param {FileList} files * The file list. */ printFileList(title, body, files) { Drupal.foldershare.utility.printMessage(title, body); // Output the file list. for (let i = 0; i < files.length; ++i) { const file = files.item(i); console.log( `%c${file.name} (${file.size} bytes, ${file.type})%c`, "padding-left: 2em" , "padding-left: 0" ); } } }; })(jQuery, Drupal); |