2017-04-28 12:24:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 - present Instructure, Inc.
|
2011-09-28 02:51:26 +08:00
|
|
|
*
|
|
|
|
* This file is part of Canvas.
|
|
|
|
*
|
|
|
|
* Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
* Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
2017-04-28 12:24:56 +08:00
|
|
|
* You should have received a copy of the GNU Affero General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-09-28 02:51:26 +08:00
|
|
|
*/
|
2011-11-11 00:31:45 +08:00
|
|
|
|
2017-05-24 23:19:24 +08:00
|
|
|
import INST from './INST'
|
2019-05-21 02:49:30 +08:00
|
|
|
import I18n from 'i18n!instructure'
|
2017-03-23 00:15:02 +08:00
|
|
|
import $ from 'jquery'
|
2017-05-24 23:19:24 +08:00
|
|
|
import htmlEscape from './str/htmlEscape'
|
2011-11-11 00:31:45 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
// Return the first value which passes a truth test
|
|
|
|
$.detect = function(collection, callback) {
|
|
|
|
var result;
|
|
|
|
$.each(collection, function(index, value) {
|
2011-11-24 03:24:29 +08:00
|
|
|
if (callback.call(value, value, index, collection)) {
|
2011-09-28 02:51:26 +08:00
|
|
|
result = value;
|
|
|
|
return false; // we found it, break the $.each() loop iteration by returning false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.encodeToHex = function(str) {
|
|
|
|
var hex = "";
|
|
|
|
var e = str.length;
|
|
|
|
var c = 0;
|
|
|
|
var h;
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
2017-03-24 07:03:32 +08:00
|
|
|
var part = str.charCodeAt(i).toString(16);
|
2011-09-28 02:51:26 +08:00
|
|
|
while (part.length < 2) {
|
|
|
|
part = "0" + part;
|
|
|
|
}
|
|
|
|
hex += part;
|
|
|
|
}
|
|
|
|
return hex;
|
|
|
|
};
|
|
|
|
$.decodeFromHex = function(str) {
|
|
|
|
var r='';
|
|
|
|
var i = 0;
|
|
|
|
while(i < str.length){
|
|
|
|
r += unescape('%'+str.substring(i,i+2));
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
// useful for i18n, e.g. t('key', 'pick one: %{select}', {select: $.raw('<select><option>...')})
|
2014-10-17 17:34:43 +08:00
|
|
|
// note that raw returns a SafeString object, so you may want to call toString
|
2011-09-28 02:51:26 +08:00
|
|
|
// if you're using it elsewhere
|
|
|
|
$.raw = function(str) {
|
2014-10-17 17:34:43 +08:00
|
|
|
return new htmlEscape.SafeString(str);
|
2011-09-28 02:51:26 +08:00
|
|
|
}
|
2014-12-19 17:35:02 +08:00
|
|
|
// ensure the jquery html setters don't puke if given a SafeString
|
|
|
|
$.each(["html", "append", "prepend"], function(idx, method) {
|
|
|
|
var orig = $.fn[method];
|
|
|
|
$.fn[method] = function() {
|
|
|
|
var args = [].slice.call(arguments);
|
|
|
|
for (var i = 0, len = args.length; i < len; i++) {
|
|
|
|
if (args[i] instanceof htmlEscape.SafeString)
|
|
|
|
args[i] = args[i].toString();
|
|
|
|
}
|
|
|
|
return orig.apply(this, args);
|
|
|
|
}
|
|
|
|
});
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.replaceOneTag = function(text, name, value) {
|
|
|
|
if(!text) { return text; }
|
|
|
|
name = (name || "").toString();
|
|
|
|
value = (value || "").toString().replace(/\s/g, "+");
|
|
|
|
var itemExpression = new RegExp("(%7B|{){2}[\\s|%20|\+]*" + name + "[\\s|%20|\+]*(%7D|}){2}", 'g');
|
|
|
|
return text.replace(itemExpression, value);
|
|
|
|
};
|
|
|
|
// backwards compatible with only one tag
|
|
|
|
$.replaceTags = function(text, mapping_or_name, maybe_value) {
|
|
|
|
if (typeof mapping_or_name == 'object') {
|
|
|
|
for (var name in mapping_or_name) {
|
|
|
|
text = $.replaceOneTag(text, name, mapping_or_name[name])
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
} else {
|
|
|
|
return $.replaceOneTag(text, mapping_or_name, maybe_value)
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.underscore = function(string) {
|
|
|
|
return (string || "").replace(/([A-Z])/g, "_$1").replace(/^_/, "").toLowerCase();
|
|
|
|
};
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.titleize = function(string) {
|
|
|
|
var res = (string || "").replace(/([A-Z])/g, " $1").replace(/_/g, " ").replace(/\s+/, " ").replace(/^\s/, "");
|
|
|
|
return $.map(res.split(/\s/), function(word) { return (word[0] || "").toUpperCase() + word.substring(1); }).join(" ");
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fileSize = function(bytes) {
|
|
|
|
var factor = 1024;
|
|
|
|
if(bytes < factor) {
|
|
|
|
return parseInt(bytes, 10) + " bytes";
|
|
|
|
} else if(bytes < factor * factor) {
|
|
|
|
return parseInt(bytes / factor, 10) + "KB";
|
|
|
|
} else {
|
|
|
|
return (Math.round(10.0 * bytes / factor / factor) / 10.0) + "MB";
|
|
|
|
}
|
|
|
|
};
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.toSentence = function(array, options) {
|
|
|
|
if (typeof options == 'undefined') {
|
|
|
|
options = {};
|
|
|
|
} else if (options == 'or') {
|
|
|
|
options = {
|
|
|
|
two_words_connector: I18n.t('#support.array.or.two_words_connector'),
|
|
|
|
last_word_connector: I18n.t('#support.array.or.last_word_connector')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
options = $.extend({
|
|
|
|
words_connector: I18n.t('#support.array.words_connector'),
|
|
|
|
two_words_connector: I18n.t('#support.array.two_words_connector'),
|
|
|
|
last_word_connector: I18n.t('#support.array.last_word_connector')
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
switch (array.length) {
|
|
|
|
case 0:
|
|
|
|
return '';
|
|
|
|
case 1:
|
|
|
|
return '' + array[0];
|
|
|
|
case 2:
|
|
|
|
return array[0] + options.two_words_connector + array[1];
|
|
|
|
default:
|
|
|
|
return array.slice(0, -1).join(options.words_connector) + options.last_word_connector + array[array.length - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return query string parameter
|
|
|
|
// $.queryParam("name") => qs value or null
|
|
|
|
$.queryParam = function(name) {
|
|
|
|
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
|
|
|
|
var regex = new RegExp("[\\?&]"+name+"=([^&#]*)");
|
|
|
|
var results = regex.exec(window.location.search);
|
|
|
|
if(results == null)
|
|
|
|
return results;
|
|
|
|
else
|
|
|
|
return decodeURIComponent(results[1].replace(/\+/g, " "));
|
|
|
|
};
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
$.capitalize = function(string) {
|
|
|
|
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
|
|
|
|
};
|
2012-02-14 00:37:41 +08:00
|
|
|
|
2011-09-28 02:51:26 +08:00
|
|
|
INST.youTubeRegEx = /^https?:\/\/(www\.youtube\.com\/watch.*v(=|\/)|youtu\.be\/)([^&#]*)/;
|
|
|
|
$.youTubeID = function(path) {
|
|
|
|
var match = path.match(INST.youTubeRegEx);
|
|
|
|
if(match && match[match.length - 1]) {
|
|
|
|
return match[match.length - 1];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|