canvas-lms/public/javascripts/jquery.instructure_misc_hel...

170 lines
5.5 KiB
JavaScript
Raw Normal View History

/*
* Copyright (C) 2011 - present Instructure, Inc.
*
* 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.
*
* 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/>.
*/
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
import INST from './INST'
import I18n from 'i18n!instructure'
import $ from 'jquery'
import htmlEscape from './str/htmlEscape'
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
// Return the first value which passes a truth test
$.detect = function(collection, callback) {
var result;
$.each(collection, function(index, value) {
if (callback.call(value, value, index, collection)) {
result = value;
return false; // we found it, break the $.each() loop iteration by returning false
}
});
return result;
};
$.encodeToHex = function(str) {
var hex = "";
var e = str.length;
var c = 0;
var h;
for (var i = 0; i < str.length; i++) {
var part = str.charCodeAt(i).toString(16);
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>...')})
// note that raw returns a SafeString object, so you may want to call toString
// if you're using it elsewhere
$.raw = function(str) {
return new htmlEscape.SafeString(str);
}
// 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);
}
});
$.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)
}
}
$.underscore = function(string) {
return (string || "").replace(/([A-Z])/g, "_$1").replace(/^_/, "").toLowerCase();
};
$.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";
}
};
$.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, " "));
};
$.capitalize = function(string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
};
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;
};